forked from emprcl/runal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.go
More file actions
355 lines (315 loc) · 7.26 KB
/
shape.go
File metadata and controls
355 lines (315 loc) · 7.26 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package runal
import (
"math"
"sort"
)
// Text renders a string at the given canvas coordinates.
func (c *Canvas) Text(text string, x, y int) {
if !c.cellMode.enabled() {
for i, r := range text {
c.char(r, x+i, y)
}
return
}
runes := []rune(text)
padChar := ' '
ix := 0
for i := 0; i < len(runes); i += 2 {
if i+1 < len(runes) {
padChar = runes[i+1]
}
c.write(cell{
char: runes[i],
padChar: padChar,
background: c.strokeBg,
foreground: c.strokeFg,
}, x+ix, y, 1)
ix += 1
padChar = ' '
}
}
// Point draws a single point at the given position.
func (c *Canvas) Point(x, y int) {
c.char(c.nextStrokeRune(), x, y)
}
// Line draws a straight line between two points.
func (c *Canvas) Line(x1, y1, x2, y2 int) {
// Bresenham algorithm
// https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
dx := absInt(x2 - x1)
dy := absInt(y2 - y1)
sx := 1
sy := 1
if x1 > x2 {
sx = -1
}
if y1 > y2 {
sy = -1
}
d := dx - dy
char := 0
for {
runes := []rune(c.strokeText)
c.char(runes[char], x1, y1)
if x1 == x2 && y1 == y2 {
break
}
char = (char + 1) % len(runes)
e2 := 2 * d
if e2 > -dy {
d -= dy
x1 += sx
}
if e2 < dx {
d += dx
y1 += sy
}
}
}
// Square draws a square with the given top-left corner and side length.
func (c *Canvas) Square(x, y, size int) {
c.Rect(x, y, size, size)
}
// Rect draws a rectangle starting at (x, y) with width w and height h.
func (c *Canvas) Rect(x, y, w, h int) {
if c.fill {
c.toggleFill()
for dy := 1; dy < h; dy++ {
if w > 2 {
c.Line(x+1, y+dy, x+w-1, y+dy)
}
}
c.toggleFill()
}
c.Line(x, y, x+w, y)
c.Line(x+w, y, x+w, y+h)
c.Line(x+w, y+h, x, y+h)
c.Line(x, y+h, x, y)
}
// Quad draws a quadrilateral defined by four points.
func (c *Canvas) Quad(x1, y1, x2, y2, x3, y3, x4, y4 int) {
if c.fill {
vertices := [][2]int{{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}}
minY := min(y1, min(y2, min(y3, y4)))
maxY := max(y1, max(y2, max(y3, y4)))
scanlineIntersections := map[int][]int{}
for i := 0; i < 4; i++ {
xStart, yStart := vertices[i][0], vertices[i][1]
xEnd, yEnd := vertices[(i+1)%4][0], vertices[(i+1)%4][1]
if yStart == yEnd {
continue
}
if yStart > yEnd {
yStart, yEnd = yEnd, yStart
xStart, xEnd = xEnd, xStart
}
for y := max(yStart, minY+1); y < min(yEnd, maxY); y++ {
if yEnd != yStart {
t := float64(y-yStart) / float64(yEnd-yStart)
x := int(math.Round(float64(xStart) + t*float64(xEnd-xStart)))
scanlineIntersections[y] = append(scanlineIntersections[y], x)
}
}
}
c.toggleFill()
for y, xs := range scanlineIntersections {
if len(xs) < 2 {
continue
}
sort.Ints(xs)
for i := 0; i < len(xs)-1; i += 2 {
startX := xs[i]
endX := xs[i+1]
if startX < endX-1 {
c.Line(startX+1, y, endX-1, y)
}
}
}
c.toggleFill()
}
// Draw outline lines
c.Line(x1, y1, x2, y2)
c.Line(x2, y2, x3, y3)
c.Line(x3, y3, x4, y4)
c.Line(x4, y4, x1, y1)
}
// Ellipse draws an ellipse centered at (x, y) with radiuses rx and ry.
func (c *Canvas) Ellipse(xCenter, yCenter, rx, ry int) {
x := 0
y := ry
rx2 := rx * rx
ry2 := ry * ry
tworx2 := 2 * rx2
twory2 := 2 * ry2
p := 0
px := 0
py := tworx2 * y
outlinePoints := make([][2]int, 0)
outlinePointSet := make(map[[2]int]bool)
fillRows := make(map[int][]int)
p = ry2 - (rx2 * ry) + (rx2 / 4)
for px < py {
points := [][2]int{
{xCenter + x, yCenter + y},
{xCenter - x, yCenter + y},
{xCenter + x, yCenter - y},
{xCenter - x, yCenter - y},
}
for _, pt := range points {
if !outlinePointSet[pt] {
outlinePoints = append(outlinePoints, pt)
outlinePointSet[pt] = true
fillRows[pt[1]] = append(fillRows[pt[1]], pt[0])
}
}
if p < 0 {
x++
px += twory2
p += ry2 + px
} else {
x++
y--
px += twory2
py -= tworx2
p += ry2 + px - py
}
}
p = ry2*(x*x+x) + rx2*(y-1)*(y-1) - rx2*ry2
for y >= 0 {
points := [][2]int{
{xCenter + x, yCenter + y},
{xCenter - x, yCenter + y},
{xCenter + x, yCenter - y},
{xCenter - x, yCenter - y},
}
for _, pt := range points {
if !outlinePointSet[pt] {
outlinePoints = append(outlinePoints, pt)
outlinePointSet[pt] = true
fillRows[pt[1]] = append(fillRows[pt[1]], pt[0])
}
}
if p > 0 {
y--
py -= tworx2
p += rx2 - py
} else {
y--
x++
px += twory2
py -= tworx2
p += rx2 - py + px
}
}
if c.fill {
c.toggleFill()
minY, maxY := yCenter-ry, yCenter+ry
for y, xs := range fillRows {
if y <= minY || y >= maxY {
continue
}
if len(xs) < 2 {
continue
}
sort.Ints(xs)
leftX := xs[0]
rightX := xs[len(xs)-1]
if rightX-leftX > 2 {
c.Line(leftX+1, y, rightX-1, y)
}
}
c.toggleFill()
}
for _, pt := range outlinePoints {
c.Point(pt[0], pt[1])
}
}
// Circle draws a circle centered at (x, y) with the given radius.
func (c *Canvas) Circle(xCenter, yCenter, r int) {
// TODO: check if this is more efficient
// than using ellipse() directly.
x := 0
y := r
d := 1 - r
char := 0
for x <= y {
char = char + 8
if c.fill {
c.toggleFill()
if y <= r-1 {
if x > 0 {
c.Line(xCenter-x+1, yCenter+y, xCenter+x-1, yCenter+y)
c.Line(xCenter-x+1, yCenter-y, xCenter+x-1, yCenter-y)
}
}
if y > 0 {
c.Line(xCenter-y+1, yCenter+x, xCenter+y-1, yCenter+x)
c.Line(xCenter-y+1, yCenter-x, xCenter+y-1, yCenter-x)
}
c.toggleFill()
}
c.char(strIndex(c.strokeText, char), xCenter+x, yCenter+y)
c.char(strIndex(c.strokeText, char+1), xCenter-x, yCenter+y)
c.char(strIndex(c.strokeText, char+2), xCenter+x, yCenter-y)
c.char(strIndex(c.strokeText, char+3), xCenter-x, yCenter-y)
c.char(strIndex(c.strokeText, char+4), xCenter+y, yCenter+x)
c.char(strIndex(c.strokeText, char+5), xCenter-y, yCenter+x)
c.char(strIndex(c.strokeText, char+6), xCenter+y, yCenter-x)
c.char(strIndex(c.strokeText, char+7), xCenter-y, yCenter-x)
x++
if d < 0 {
d += 2*x + 1
} else {
y--
d += 2*(x-y) + 1
}
}
}
// Triangle draws a triangle using three vertex points.
func (c *Canvas) Triangle(x1, y1, x2, y2, x3, y3 int) {
if c.fill {
c.toggleFill()
c.fillTriangle(x1, y1, x2, y2, x3, y3)
c.toggleFill()
}
c.Line(x1, y1, x2, y2)
c.Line(x2, y2, x3, y3)
c.Line(x3, y3, x1, y1)
}
func (c *Canvas) fillTriangle(x1, y1, x2, y2, x3, y3 int) {
minX := min(x1, min(x2, x3))
maxX := max(x1, max(x2, x3))
minY := min(y1, min(y2, y3))
maxY := max(y1, max(y2, y3))
for y := minY; y <= maxY; y++ {
for x := minX; x <= maxX; x++ {
if pointInTriangle(x, y, x1, y1, x2, y2, x3, y3) {
c.Point(x, y)
}
}
}
}
func pointInTriangle(px, py, x1, y1, x2, y2, x3, y3 int) bool {
ax, ay := float64(x1), float64(y1)
bx, by := float64(x2), float64(y2)
cx, cy := float64(x3), float64(y3)
pxf, pyf := float64(px), float64(py)
v0x, v0y := cx-ax, cy-ay
v1x, v1y := bx-ax, by-ay
v2x, v2y := pxf-ax, pyf-ay
d00 := v0x*v0x + v0y*v0y
d01 := v0x*v1x + v0y*v1y
d02 := v0x*v2x + v0y*v2y
d11 := v1x*v1x + v1y*v1y
d12 := v1x*v2x + v1y*v2y
denom := d00*d11 - d01*d01
if denom == 0 {
return false
}
invDenom := 1 / denom
u := (d11*d02 - d01*d12) * invDenom
v := (d00*d12 - d01*d02) * invDenom
// Return true only for strictly interior points (exclude boundary)
epsilon := 1e-10
return u > epsilon && v > epsilon && u+v < 1-epsilon
}