-
Notifications
You must be signed in to change notification settings - Fork 4
/
line.go
386 lines (341 loc) · 8.08 KB
/
line.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package primitive
import (
"fmt"
"image"
)
type Line struct {
P1, P2 Point2D
}
func (l Line) Draw(dst *image.Gray, col byte) {
w, h := dst.Rect.Dx(), dst.Rect.Dy()
if !l.clip(w, h) {
return
}
// Find which direction is longer
yLonger := false
shortLen := l.P2.Y - l.P1.Y
longLen := l.P2.X - l.P1.X
// Swap if we have to
if absf(shortLen) > absf(longLen) {
shortLen, longLen = longLen, shortLen
yLonger = true
}
// Find how much we should increment on every pixel.
var decInc float32
if longLen != 0 {
decInc = shortLen / longLen
}
// Small rounding adjustment to avoid accumulated errors.
const sigma = 0.001
if yLonger {
if longLen > 0 {
// Top to bottom, one y per loop
l.P2.Y += sigma
for j := l.P1.X; l.P1.Y <= l.P2.Y; l.P1.Y++ {
setGray(dst, j, l.P1.Y, col)
j += decInc
}
return
}
// Bottom to top, one y per loop
l.P2.Y -= sigma
for j := l.P1.X; l.P1.Y >= l.P2.Y; l.P1.Y-- {
setGray(dst, j, l.P1.Y, col)
j -= decInc
}
return
}
if longLen > 0 {
// Left to right, one X per loop
l.P2.X += sigma
for j := l.P1.Y; l.P1.X <= l.P2.X; l.P1.X++ {
setGray(dst, l.P1.X, j, col)
j += decInc
}
return
} else {
l.P2.X -= sigma
// Right to left, one X per loop
for j := l.P1.Y; l.P1.X >= l.P2.X; l.P1.X-- {
setGray(dst, l.P1.X, j, col)
j -= decInc
}
}
}
func (l Line) DrawAA(dst *image.Gray, col byte) {
w, h := dst.Rect.Dx(), dst.Rect.Dy()
if !l.clip(w, h) {
return
}
yLonger := false
shortLen := l.P2.Y - l.P1.Y
longLen := l.P2.X - l.P1.X
if absf(shortLen) > absf(longLen) {
shortLen, longLen = longLen, shortLen
yLonger = true
}
var decInc float32
if longLen != 0 {
decInc = shortLen / longLen
}
const sigma = 0.001
if yLonger {
if longLen > 0 {
// Top to bottom, one y per loop
l.P2.Y += sigma
for j := l.P1.X; l.P1.Y <= l.P2.Y; l.P1.Y++ {
setGrayHorizontalAA(dst, j, l.P1.Y, col)
j += decInc
}
return
}
// Bottom to top, one y per loop
l.P2.Y -= sigma
for j := l.P1.X; l.P1.Y >= l.P2.Y; l.P1.Y-- {
setGrayHorizontalAA(dst, j, l.P1.Y, col)
j -= decInc
}
return
}
if longLen > 0 {
// Left to right, one X per loop
l.P2.X += sigma
for j := l.P1.Y; l.P1.X <= l.P2.X; l.P1.X++ {
setGrayVerticalAA(dst, l.P1.X, j, col)
j += decInc
}
return
}
// Right to left, one X per loop
l.P2.X -= sigma
for j := l.P1.Y; l.P1.X >= l.P2.X; l.P1.X-- {
setGrayVerticalAA(dst, l.P1.X, j, col)
j -= decInc
}
}
// setGray will set a pixel.
// It is assumed that X and y are clipped.
func setGray(img *image.Gray, x, y float32, col byte) {
img.Pix[roundP(x)+roundP(y)*img.Stride] = col
}
// setGray will set a pixel.
// It is assumed that X and y are clipped.
func setGrayAA(img *image.Gray, x, y float32, col byte) {
// Convert to fixed point
xx, yy := int(256*x), int(256*y)
// Calculate weights
x1, y1 := xx&255, yy&255
x0, y0 := 256-x1, 256-y1
// Apply weighted color to pixel
weight := func(pix *byte, w int) {
wOrg := 256 - w
p := int(*pix)*wOrg + int(col)*w
*pix = byte(p >> 8)
}
// Remove fraction from pixels coordinates.
xx >>= 8
yy >>= 8
// Check if we can write to the next pixel
xOK, yOK := xx < img.Rect.Dx()-1, yy < img.Rect.Dy()-1
// Pre-multiply with image stride
yy *= img.Stride
// draw topleft pixel.
p := &img.Pix[xx+yy]
weight(p, (x0*y0)>>8)
if xOK {
p = &img.Pix[1+xx+yy]
weight(p, (x1*y0)>>8)
}
if yOK {
yy += img.Stride
p = &img.Pix[xx+yy]
weight(p, (x0*y1)>>8)
if xOK {
p = &img.Pix[1+xx+yy]
weight(p, (x1*y1)>>8)
}
}
}
// setGrayHorizontalAA will set a pixel, but only do AA vertically.
// This can be used when each pixel is drawn horizontally.
// It is assumed that X and y are clipped.
func setGrayHorizontalAA(img *image.Gray, x, y float32, col byte) {
// Convert to fixed point
xx, yy := int(256*x), int(256*y)
// Calculate weights
x1 := xx & 255
x0 := 256 - x1
// Apply weighted color to pixel.
// w0 is color weight, w1 is existing weight.
// Sum of w0 and w1 must be <= 256
weight := func(pix *byte, w0, w1 int) {
p := int(*pix)*w1 + int(col)*w0
*pix = byte(p >> 8)
}
// Remove fraction from pixels coordinates.
xx >>= 8
yy >>= 8
// Check if we can write to the next pixel
xOK := xx < img.Rect.Dx()-1
// Pre-multiply with image stride
yy *= img.Stride
// draw topleft pixel.
p := &img.Pix[xx+yy]
weight(p, x0, x1)
if xOK {
p = &img.Pix[1+xx+yy]
weight(p, x1, x0)
}
}
// setGrayVerticalAA will set a pixel, but only do AA horizontally.
// This can be used when each pixel is drawn vertically.
// It is assumed that X and y are clipped.
func setGrayVerticalAA(img *image.Gray, x, y float32, col byte) {
// Convert to fixed point
xx, yy := int(256*x), int(256*y)
// Calculate weights
y1 := yy & 255
y0 := 256 - y1
// Apply weighted color to pixel.
// w0 is color weight, w1 is existing weight.
// Sum of w0 and w1 must be <= 256
weight := func(pix *byte, w0, w1 int) {
p := int(*pix)*w1 + int(col)*w0
*pix = byte(p >> 8)
}
// Remove fraction from pixels coordinates.
xx >>= 8
yy >>= 8
// Check if we can write to the next pixel
yOK := yy < img.Rect.Dy()-1
// Pre-multiply with image stride
yy *= img.Stride
// draw topleft pixel.
p := &img.Pix[xx+yy]
weight(p, y0, y1)
if yOK {
yy += img.Stride
p = &img.Pix[xx+yy]
weight(p, y1, y0)
}
}
// roundP will round positive numbers towards nearest integer.
func roundP(x float32) int {
return int(x + 0.5)
}
func absf(x float32) float32 {
switch {
case x < 0:
return -x
case x == 0:
return 0 // return correctly abs(-0)
}
return x
}
// line clipping converted from C++ on
// https://www.geeksforgeeks.org/line-clipping-set-1-cohen-sutherland-algorithm/
// Will also ensure that Y1 is smaller or equal to Y2.
func (l *Line) clip(w, h int) bool {
// Defining region codes
const (
INSIDE = 0
LEFT = 1 << iota
RIGHT
BOTTOM
TOP
)
if l.P1.Y > l.P2.Y {
// Swap
l.P1.X, l.P2.X = l.P2.X, l.P1.X
l.P1.Y, l.P2.Y = l.P2.Y, l.P1.Y
}
fw, fh := float32(w)-0.51, float32(h)-0.51
// Function to compute region code for a point(X, y)
var computeCode = func(p Point2D) int {
// initialized as being inside
code := INSIDE
if p.X < 0 {
// to the left of rectangle
code |= LEFT
} else if p.X > fw {
// to the right of rectangle
code |= RIGHT
}
if p.Y < 0 {
// below the rectangle
code |= BOTTOM
} else if p.Y > fh {
// above the rectangle
code |= TOP
}
return code
}
code1 := computeCode(l.P1)
code2 := computeCode(l.P2)
const minLen = (0.1 * 0.1) * 2
for n := 0; ; n++ {
if false && n > 5 {
code1 = computeCode(l.P1)
code2 = computeCode(l.P2)
fmt.Println("Could not clip", l.P1, code1, "->", l.P2, code2)
return false
}
if l.P1.DistSq(l.P2) < minLen {
// Line too small to draw.
return false
}
if (code1 == 0) && (code2 == 0) {
// If both endpoints lie within rectangle
return true
}
if (code1 & code2) != 0 {
// If both endpoints are outside rectangle,
// in same region
return false
}
// Some segment of line lies within the
// rectangle
var codeOut int
var x, y float32
// At least one endpoint is outside the
// rectangle, pick it.
if code1 != 0 {
codeOut = code1
} else {
codeOut = code2
}
// Find intersection point;
// using formulas y = Y1 + slope * (X - X1),
// X = X1 + (1 / slope) * (y - Y1)
switch {
case (codeOut & TOP) != 0:
// point is above the clip rectangle
x = l.P1.X + (l.P2.X-l.P1.X)*(fh-l.P1.Y)/(l.P2.Y-l.P1.Y)
y = fh
case (codeOut & BOTTOM) != 0:
// point is below the rectangle
x = l.P1.X + (l.P2.X-l.P1.X)*(-l.P1.Y)/(l.P2.Y-l.P1.Y)
y = 0
case (codeOut & RIGHT) != 0:
// point is to the right of rectangle
y = l.P1.Y + (l.P2.Y-l.P1.Y)*(fw-l.P1.X)/(l.P2.X-l.P1.X)
x = fw
case (codeOut & LEFT) != 0:
// point is to the left of rectangle
y = l.P1.Y + (l.P2.Y-l.P1.Y)*(-l.P1.X)/(l.P2.X-l.P1.X)
x = 0
}
// Now intersection point X,y is found
// We replace point outside rectangle
// by intersection point
if codeOut == code1 {
l.P1.X = x
l.P1.Y = y
code1 = computeCode(l.P1)
} else {
l.P2.X = x
l.P2.Y = y
code2 = computeCode(l.P2)
}
}
}