Debian Codegraph @

vendor/github.com/xyproto/gfx/draw_int.go

raw
1 package gfx
2
3 import (
4 "image/color"
5 "image/draw"
6 )
7
8 // DrawIntLine draws a line between two points
9 func DrawIntLine(dst draw.Image, x0, y0, x1, y1 int, c color.Color) {
10 if x0 == x1 {
11 if y0 > y1 {
12 y0, y1 = y1, y0
13 }
14
15 for ; y0 <= y1; y0++ {
16 dst.Set(x0, y0, c)
17 }
18 } else if y0 == y1 {
19 if x0 > x1 {
20 x0, x1 = x1, x0
21 }
22
23 for ; x0 <= x1; x0++ {
24 dst.Set(x0, y0, c)
25 }
26 } else { // Bresenham
27 dx := x1 - x0
28
29 if dx < 0 {
30 dx = -dx
31 }
32
33 dy := y1 - y0
34
35 if dy < 0 {
36 dy = -dy
37 }
38
39 steep := dy > dx
40
41 if steep {
42 x0, x1, y0, y1 = y0, y1, x0, x1
43 }
44
45 if x0 > x1 {
46 x0, x1, y0, y1 = x1, x0, y1, y0
47 }
48
49 dx = x1 - x0
50 dy = y1 - y0
51
52 ystep := 1
53
54 if dy < 0 {
55 dy = -dy
56 ystep = -1
57 }
58
59 err := dx / 2
60
61 for ; x0 <= x1; x0++ {
62 if steep {
63 dst.Set(y0, x0, c)
64 } else {
65 dst.Set(x0, y0, c)
66 }
67
68 err -= dy
69 if err < 0 {
70 y0 += ystep
71 err += dx
72 }
73 }
74 }
75 }
76
77 // DrawIntRectangle draws a rectangle given a point, width and height
78 func DrawIntRectangle(dst draw.Image, x, y, w, h int, c color.Color) {
79 if w <= 0 || h <= 0 {
80 return
81 }
82
83 DrawIntLine(dst, x, y, x+w-1, y, c)
84 DrawIntLine(dst, x, y, x, y+h-1, c)
85 DrawIntLine(dst, x+w-1, y, x+w-1, y+h-1, c)
86 DrawIntLine(dst, x, y+h-1, x+w-1, y+h-1, c)
87
88 return
89 }
90
91 // DrawIntFilledRectangle draws a filled rectangle given a point, width and height
92 func DrawIntFilledRectangle(dst draw.Image, x, y, w, h int, c color.Color) {
93 if w <= 0 || h <= 0 {
94 return
95 }
96
97 for i := x; i < x+w; i++ {
98 DrawIntLine(dst, i, y, i, y+h-1, c)
99 }
100
101 return
102 }
103
104 // DrawIntCircle draws a circle given a point and radius
105 func DrawIntCircle(dst draw.Image, x0, y0, r int, c color.Color) {
106 f := 1 - r
107
108 ddfx := 1
109 ddfy := -2 * r
110
111 x := 0
112 y := r
113
114 dst.Set(x0, y0+r, c)
115 dst.Set(x0, y0-r, c)
116 dst.Set(x0+r, y0, c)
117 dst.Set(x0-r, y0, c)
118
119 for x < y {
120 if f >= 0 {
121 y--
122 ddfy += 2
123 f += ddfy
124 }
125
126 x++
127 ddfx += 2
128 f += ddfx
129
130 dst.Set(x0+x, y0+y, c)
131 dst.Set(x0-x, y0+y, c)
132 dst.Set(x0+x, y0-y, c)
133 dst.Set(x0-x, y0-y, c)
134 dst.Set(x0+y, y0+x, c)
135 dst.Set(x0-y, y0+x, c)
136 dst.Set(x0+y, y0-x, c)
137 dst.Set(x0-y, y0-x, c)
138 }
139 }
140
141 // DrawIntFilledCircle draws a filled circle given a point and radius
142 func DrawIntFilledCircle(dst draw.Image, x0, y0, r int, c color.Color) {
143 f := 1 - r
144
145 ddfx := 1
146 ddfy := -2 * r
147
148 x := 0
149 y := r
150
151 DrawIntLine(dst, x0, y0-r, x0, y0+r, c)
152
153 for x < y {
154 if f >= 0 {
155 y--
156 ddfy += 2
157 f += ddfy
158 }
159
160 x++
161 ddfx += 2
162 f += ddfx
163
164 DrawIntLine(dst, x0+x, y0-y, x0+x, y0+y, c)
165 DrawIntLine(dst, x0+y, y0-x, x0+y, y0+x, c)
166 DrawIntLine(dst, x0-x, y0-y, x0-x, y0+y, c)
167 DrawIntLine(dst, x0-y, y0-x, x0-y, y0+x, c)
168 }
169 }
170
171 // DrawIntTriangle draws a triangle given three points
172 func DrawIntTriangle(dst draw.Image, x0, y0, x1, y1, x2, y2 int, c color.Color) {
173 DrawIntLine(dst, x0, y0, x1, y1, c)
174 DrawIntLine(dst, x0, y0, x2, y2, c)
175 DrawIntLine(dst, x1, y1, x2, y2, c)
176 }
177
178 // DrawIntFilledTriangle draws a filled triangle given three points
179 func DrawIntFilledTriangle(dst draw.Image, x0, y0, x1, y1, x2, y2 int, c color.Color) {
180 if y0 > y1 {
181 x0, y0, x1, y1 = x1, y1, x0, y0
182 }
183
184 if y1 > y2 {
185 x1, y1, x2, y2 = x2, y2, x1, y1
186 }
187
188 if y0 > y1 {
189 x0, y0, x1, y1 = x1, y1, x0, y0
190 }
191
192 if y0 == y2 {
193 a := x0
194 b := x0
195
196 if x1 < a {
197 a = x1
198 } else if x1 > b {
199 b = x1
200 }
201
202 if x2 < a {
203 a = x2
204 } else if x2 > b {
205 b = x2
206 }
207
208 DrawIntLine(dst, a, y0, b, y0, c)
209
210 return
211 }
212
213 dx01 := x1 - x0
214 dy01 := y1 - y0
215 dx02 := x2 - x0
216 dy02 := y2 - y0
217 dx12 := x2 - x1
218 dy12 := y2 - y1
219
220 sa := 0
221 sb := 0
222 a := 0
223 b := 0
224
225 last := y1 - 1
226
227 if y1 == y2 {
228 last = y1
229 }
230
231 for y := y0; y <= last; y++ {
232 a = x0 + sa/dy01
233 b = x0 + sb/dy02
234
235 sa += dx01
236 sb += dx02
237
238 DrawIntLine(dst, a, y, b, y, c)
239 }
240
241 sa = dx12 * (last - y1)
242 sb = dx02 * (last - y0)
243
244 for y := last; y <= y2; y++ {
245 a = x1 + sa/dy12
246 b = x0 + sb/dy02
247
248 sa += dx12
249 sb += dx02
250
251 DrawIntLine(dst, a, y, b, y, c)
252 }
253 }