-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.go
248 lines (201 loc) · 5.3 KB
/
shape.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
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package pi
import "math"
// RectFill draws a filled rectangle on screen between points x0,y0 and x1,y1 (inclusive).
//
// RectFill takes into account camera position, clipping region and draw palette.
func RectFill(x0, y0, x1, y1 int, color byte) {
col := Pal[color]
x0 -= Camera.X
x1 -= Camera.X
y0 -= Camera.Y
y1 -= Camera.Y
screen.RectFill(x0, y0, x1, y1, col)
}
// RectFill draws a filled rectangle between points x0,y0 and x1,y1 (inclusive).
func (p PixMap) RectFill(x0 int, y0 int, x1 int, y1 int, col byte) {
if x0 > x1 {
x0, x1 = x1, x0
}
if y0 > y1 {
y0, y1 = y1, y0
}
ptr, ok := p.Pointer(x0, y0, x1-x0+1, y1-y0+1)
if !ok {
return
}
line := p.lineOfColor(col, ptr.RemainingPixels)
copy(ptr.Pix, line)
for y := 1; y < ptr.RemainingLines; y++ {
ptr.Pix = ptr.Pix[p.width:]
copy(ptr.Pix, line)
}
}
// Rect draws a rectangle on screen between points x0,y0 and x1,y1 (inclusive).
//
// Rect takes into account camera position, clipping region and draw palette.
func Rect(x0, y0, x1, y1 int, color byte) {
color = Pal[color]
x0, x1 = x0-Camera.X, x1-Camera.X
y0, y1 = y0-Camera.Y, y1-Camera.Y
screen.Rect(x0, y0, x1, y1, color)
}
// Rect draws a rectangle between points x0,y0 and x1,y1 (inclusive).
func (p PixMap) Rect(x0, y0, x1, y1 int, col byte) {
p.horizontalLine(y0, x0, x1, col)
p.horizontalLine(y1, x0, x1, col)
p.verticalLine(x0, y0, y1, col)
p.verticalLine(x1, y0, y1, col)
}
// Line draws a line on screen between points x0,y0 and x1,y1 (inclusive).
//
// Line takes into account camera position, clipping region and draw palette.
func Line(x0, y0, x1, y1 int, color byte) {
color = Pal[color]
x0 -= Camera.X
x1 -= Camera.X
y0 -= Camera.Y
y1 -= Camera.Y
screen.Line(x0, y0, x1, y1, color)
}
// Line draws a line between points x0,y0 and x1,y1 (inclusive).
func (p PixMap) Line(x0, y0, x1, y1 int, color byte) {
// Bresenham algorithm: https://www.youtube.com/watch?v=IDFB5CDpLDE
run := float64(x1 - x0)
if run == 0 {
p.verticalLine(x0, y0, y1, color)
return
}
rise := float64(y1 - y0)
if rise == 0 {
p.horizontalLine(y0, x0, x1, color)
return
}
slope := rise / run
adjust := 1
if slope < 0 {
adjust = -1
}
offset := 0.0 // performance could be better if offset was an integer instead
threshold := 0.5 // performance could be better if threshold was an integer instead
if slope >= -1 && slope <= 1 {
delta := math.Abs(slope)
y := y0
if x1 < x0 {
x0, x1 = x1, x0
y = y1
}
for x := x0; x <= x1; x++ {
p.Set(x, y, color)
offset += delta
if offset >= threshold {
y += adjust
threshold += 1
}
}
} else {
delta := math.Abs(run / rise)
x := x0
if y0 > y1 {
y0, y1 = y1, y0
x = x1
}
for y := y0; y <= y1; y++ {
p.Set(x, y, color)
offset += delta
if offset >= threshold {
x += adjust
threshold += 1
}
}
}
}
// verticalLine draws a vertical line between y0-y1 inclusive
func (p PixMap) verticalLine(x, y0, y1 int, color byte) {
if y0 > y1 {
y0, y1 = y1, y0
}
ptr, ok := p.Pointer(x, y0, 1, y1-y0+1)
if !ok {
return
}
index := 0
for i := 0; i < ptr.RemainingLines; i++ {
ptr.Pix[index] = color
index += p.width
}
}
// horizontalLine draws a vertical line between x0-x1 inclusive
func (p PixMap) horizontalLine(y, x0, x1 int, color byte) {
if x0 > x1 {
x0, x1 = x1, x0
}
ptr, ok := p.Pointer(x0, y, x1-x0+1, 1)
if !ok {
return
}
pix := ptr.Pix[:ptr.RemainingPixels]
for i := 0; i < len(pix); i++ {
pix[i] = color
}
}
// Circ draws a circle on screen.
//
// Circ takes into account camera position, clipping region and draw palette.
func Circ(centerX, centerY, radius int, color byte) {
color = Pal[color]
centerX = centerX - Camera.X
centerY = centerY - Camera.Y
screen.Circ(centerX, centerY, radius, color)
}
// Circ draws a circle.
func (p PixMap) Circ(centerX int, centerY int, radius int, color byte) {
// Code based on Frédéric Goset work: http://fredericgoset.ovh/mathematiques/courbes/en/bresenham_circle.html
x := 0
y := radius
m := 5 - 4*radius
for x <= y {
p.Set(centerX+x, centerY+y, color)
p.Set(centerX+x, centerY-y, color)
p.Set(centerX-x, centerY+y, color)
p.Set(centerX-x, centerY-y, color)
p.Set(centerX+y, centerY+x, color)
p.Set(centerX+y, centerY-x, color)
p.Set(centerX-y, centerY+x, color)
p.Set(centerX-y, centerY-x, color)
if m > 0 {
y--
m -= 8 * y
}
x++
m += 8*x + 4
}
}
// CircFill draws a filled circle
//
// CircFill takes into account camera position, clipping region and draw palette.
func CircFill(centerX, centerY, radius int, color byte) {
color = Pal[color]
centerX = centerX - Camera.X
centerY = centerY - Camera.Y
// Code based on Frédéric Goset work: http://fredericgoset.ovh/mathematiques/courbes/en/filled_circle.html
screen.CircFill(centerX, centerY, radius, color)
}
func (p PixMap) CircFill(centerX int, centerY int, radius int, color byte) {
x := 0
y := radius
m := 5 - 4*radius
for x <= y {
p.RectFill(centerX-y, centerY-x, centerX+y, centerY-x, color)
p.RectFill(centerX-y, centerY+x, centerX+y, centerY+x, color)
if m > 0 {
p.RectFill(centerX-x, centerY-y, centerX+x, centerY-y, color)
p.RectFill(centerX-x, centerY+y, centerX+x, centerY+y, color)
y--
m -= 8 * y
}
x++
m += 8*x + 4
}
}