-
Notifications
You must be signed in to change notification settings - Fork 11
/
tinydraw.go
218 lines (199 loc) · 4.38 KB
/
tinydraw.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
package tinydraw // import "tinygo.org/x/tinydraw"
import (
"image/color"
"errors"
"tinygo.org/x/drivers"
)
// Line draws a line between two points
func Line(display drivers.Displayer, x0 int16, y0 int16, x1 int16, y1 int16, color color.RGBA) {
if x0 == x1 {
if y0 > y1 {
y0, y1 = y1, y0
}
for ; y0 <= y1; y0++ {
display.SetPixel(x0, y0, color)
}
} else if y0 == y1 {
if x0 > x1 {
x0, x1 = x1, x0
}
for ; x0 <= x1; x0++ {
display.SetPixel(x0, y0, color)
}
} 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 := int16(1)
if dy < 0 {
dy = -dy
ystep = -1
}
err := dx / 2
for ; x0 <= x1; x0++ {
if steep {
display.SetPixel(y0, x0, color)
} else {
display.SetPixel(x0, y0, color)
}
err -= dy
if err < 0 {
y0 += ystep
err += dx
}
}
}
}
// Rectangle draws a rectangle given a point, width and height
func Rectangle(display drivers.Displayer, x int16, y int16, w int16, h int16, color color.RGBA) error {
if w <= 0 || h <= 0 {
return errors.New("empty rectangle")
}
Line(display, x, y, x+w-1, y, color)
Line(display, x, y, x, y+h-1, color)
Line(display, x+w-1, y, x+w-1, y+h-1, color)
Line(display, x, y+h-1, x+w-1, y+h-1, color)
return nil
}
// FilledRectangle draws a filled rectangle given a point, width and height
func FilledRectangle(display drivers.Displayer, x int16, y int16, w int16, h int16, color color.RGBA) error {
if w <= 0 || h <= 0 {
return errors.New("empty rectangle")
}
for i := x; i < x+w; i++ {
Line(display, i, y, i, y+h-1, color)
}
return nil
}
// Circle draws a circle given a point and radius
func Circle(display drivers.Displayer, x0 int16, y0 int16, r int16, color color.RGBA) {
f := 1 - r
ddfx := int16(1)
ddfy := -2 * r
x := int16(0)
y := r
display.SetPixel(x0, y0+r, color)
display.SetPixel(x0, y0-r, color)
display.SetPixel(x0+r, y0, color)
display.SetPixel(x0-r, y0, color)
for x < y {
if f >= 0 {
y--
ddfy += 2
f += ddfy
}
x++
ddfx += 2
f += ddfx
display.SetPixel(x0+x, y0+y, color)
display.SetPixel(x0-x, y0+y, color)
display.SetPixel(x0+x, y0-y, color)
display.SetPixel(x0-x, y0-y, color)
display.SetPixel(x0+y, y0+x, color)
display.SetPixel(x0-y, y0+x, color)
display.SetPixel(x0+y, y0-x, color)
display.SetPixel(x0-y, y0-x, color)
}
}
// FilledCircle draws a filled circle given a point and radius
func FilledCircle(display drivers.Displayer, x0 int16, y0 int16, r int16, color color.RGBA) {
f := 1 - r
ddfx := int16(1)
ddfy := -2 * r
x := int16(0)
y := r
Line(display, x0, y0-r, x0, y0+r, color)
for x < y {
if f >= 0 {
y--
ddfy += 2
f += ddfy
}
x++
ddfx += 2
f += ddfx
Line(display, x0+x, y0-y, x0+x, y0+y, color)
Line(display, x0+y, y0-x, x0+y, y0+x, color)
Line(display, x0-x, y0-y, x0-x, y0+y, color)
Line(display, x0-y, y0-x, x0-y, y0+x, color)
}
}
// Triangle draws a triangle given three points
func Triangle(display drivers.Displayer, x0 int16, y0 int16, x1 int16, y1 int16, x2 int16, y2 int16, color color.RGBA) {
Line(display, x0, y0, x1, y1, color)
Line(display, x0, y0, x2, y2, color)
Line(display, x1, y1, x2, y2, color)
}
// FilledTriangle draws a filled triangle given three points
func FilledTriangle(display drivers.Displayer, x0 int16, y0 int16, x1 int16, y1 int16, x2 int16, y2 int16, color color.RGBA) {
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 { // y0 = y1 = y2 : it's a line
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
}
Line(display, a, y0, b, y0, color)
return
}
dx01 := x1 - x0
dy01 := y1 - y0
dx02 := x2 - x0
dy02 := y2 - y0
dx12 := x2 - x1
dy12 := y2 - y1
sa := int16(0)
sb := int16(0)
a := int16(0)
b := int16(0)
last := y1 - 1
if y1 == y2 {
last = y1
}
y := y0
for ; y <= last; y++ {
a = x0 + sa/dy01
b = x0 + sb/dy02
sa += dx01
sb += dx02
Line(display, a, y, b, y, color)
}
sa = dx12 * (y - y1)
sb = dx02 * (y - y0)
for ; y <= y2; y++ {
a = x1 + sa/dy12
b = x0 + sb/dy02
sa += dx12
sb += dx02
Line(display, a, y, b, y, color)
}
}