-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbresenham.go
More file actions
53 lines (42 loc) · 750 Bytes
/
bresenham.go
File metadata and controls
53 lines (42 loc) · 750 Bytes
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
package gfx
import (
"image/color"
"image/draw"
"math"
)
// DrawLineBresenham draws a line using Bresenham's line algorithm.
//
// http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
func DrawLineBresenham(dst draw.Image, from, to Vec, c color.Color) {
x0, y0 := from.XY()
x1, y1 := to.XY()
steep := math.Abs(y0-y1) > math.Abs(x0-x1)
if steep {
x0, y0 = y0, x0
x1, y1 = y1, x1
}
if x0 > x1 {
x0, x1 = x1, x0
y0, y1 = y1, y0
}
dx := x1 - x0
dy := math.Abs(y1 - y0)
e := dx / 2
y := y0
var ystep float64 = -1
if y0 < y1 {
ystep = 1
}
for x := x0; x <= x1; x++ {
if steep {
Mix(dst, int(y), int(x), c)
} else {
Mix(dst, int(x), int(y), c)
}
e -= dy
if e < 0 {
y += ystep
e += dx
}
}
}