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