-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
155 lines (129 loc) · 2.41 KB
/
util.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
package lin3dmath
import "math"
type Direction int
const (
UP = Direction(1)
NORTH = UP
DOWN = Direction(2)
SOUTH = DOWN
LEFT = Direction(4)
WEST = LEFT
RIGHT = Direction(8)
EAST = RIGHT
UP_RIGHT = UP | RIGHT
DOWN_RIGHT = DOWN | RIGHT
UP_LEFT = UP | LEFT
DOWN_LEFT = DOWN | LEFT
)
func (s Direction) ToVector2f() Vector2f {
switch s {
case UP:
return Vector2f{0, 1}
case DOWN:
return Vector2f{0, -1}
case LEFT:
return Vector2f{-1, 0}
case RIGHT:
return Vector2f{1, 0}
}
panic("Unknown direction")
}
func (s Direction) ToVector2i() Vector2i {
switch s {
case UP:
return Vector2i{0, 1}
case DOWN:
return Vector2i{0, -1}
case LEFT:
return Vector2i{-1, 0}
case RIGHT:
return Vector2i{1, 0}
}
panic("Unknown direction")
}
func (s Direction) IsVertical() bool {
return s == UP || s == DOWN
}
func (s Direction) IsHorizontal() bool {
return s == LEFT || s == RIGHT
}
func AlignDivUpI(v, align int) int {
return (v + align - 1) / align
}
func AlignUpF(v, align int) int {
return (v + align - 1) / align
}
func AlignDivDown2FToI(v Vector2f, align_x, align_y int) (x, y int) {
x = int(v.X / float32(align_x))
y = int(v.Y / float32(align_y))
return
}
func ValidateCoordRangeI(x, y, w, h int) bool {
return x >= 0 && y >= 0 && x < w && y < h
}
func RoundF(f float32) float32 {
fi := float32(int(f))
dif := f - fi
return fi + dif*2
}
func DistToNextCell(x, y float32, dir Direction) float32 {
ix := float32(int(x))
iy := float32(int(y))
switch dir {
case UP:
return iy + 1 - y
case RIGHT:
return ix + 1 - x
case DOWN:
if y == iy {
return -1
}
return iy - y
case LEFT:
if x == ix {
return -1
}
return ix - x
}
panic("Unknown dir")
}
func AlignToCell(val float32, dir Direction) int {
iv := int(val)
fiv := float32(iv)
if (dir == UP || dir == RIGHT) || fiv != val {
return iv
}
return iv - 1
}
func Abs32(v float32) float32 {
if v > 0 {
return v
}
return -v
}
func Sin32(v float32) float32 {
return float32(math.Sin(float64(v)))
}
func Cos32(v float32) float32 {
return float32(math.Cos(float64(v)))
}
func Sqrt32(v float32) float32 {
return float32(math.Sqrt(float64(v)))
}
func Acos32(v float32) float32 {
return float32(math.Acos(float64(v)))
}
func FastFloor64(v float64) float64 {
i := float64(int(v))
if v < i {
return i-1
}
return i
}
func FastFloor32(v float32) float32 {
i := float32(int(v))
if v < i {
return i-1
}
return i
}