-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
77 lines (65 loc) · 1.3 KB
/
utils.go
File metadata and controls
77 lines (65 loc) · 1.3 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
package main
import "math/rand"
type Vector2i struct {
X, Y int
}
// abs returns the absolute int value
func abs(x int) int {
switch {
case x < 0:
return -x
case x == 0:
return 0
}
return x
}
// rng returns a random number in a given range
func rng(min, max int) int {
return rand.Intn(max-min+1) + min
}
// BresenhamLine returns a slice containing all the positions between two positions using Bresenham's Line Algorithm.
// @see http://www.roguebasin.com/index.php/Bresenham%27s_Line_Algorithm#Go
func BresenhamLine(pos1, pos2 Vector2i) (points []Vector2i) {
x1, y1 := pos1.X, pos1.Y
x2, y2 := pos2.X, pos2.Y
isSteep := abs(y2-y1) > abs(x2-x1)
if isSteep {
x1, y1 = y1, x1
x2, y2 = y2, x2
}
reversed := false
if x1 > x2 {
x1, x2 = x2, x1
y1, y2 = y2, y1
reversed = true
}
deltaX := x2 - x1
deltaY := abs(y2 - y1)
err := deltaX / 2
y := y1
var ystep int
if y1 < y2 {
ystep = 1
} else {
ystep = -1
}
for x := x1; x < x2+1; x++ {
if isSteep {
points = append(points, Vector2i{y, x})
} else {
points = append(points, Vector2i{x, y})
}
err -= deltaY
if err < 0 {
y += ystep
err += deltaX
}
}
if reversed {
// Reverse the slice
for i, j := 0, len(points)-1; i < j; i, j = i+1, j-1 {
points[i], points[j] = points[j], points[i]
}
}
return
}