-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
258 lines (205 loc) · 5.42 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
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
package cirno
import (
"fmt"
"math"
)
const (
// RadToDeg is a factor to transfrom radians to degrees.
RadToDeg float64 = 180.0 / math.Pi
// DegToRad is a factor to transform degrees to radians.
DegToRad float64 = math.Pi / 180.0
// Epsilon is the constant for approximate comparisons.
Epsilon float64 = 0.000001
// CollinearityThreshold is the constant to detect if two vectors
// are effectively collinear.
CollinearityThreshold float64 = 10.0
)
type none struct{}
// Sign returns the sign of the number
// according to Epsilon.
func Sign(number float64) float64 {
if number < 0 {
return -1
} else if number < Epsilon {
return 0
} else {
return 1
}
}
// Distance returns the value of distance
// between two points represented as vectors.
func Distance(a, b Vector) float64 {
return b.Subtract(a).Magnitude()
}
// SquaredDistance returns the value of distance in the power of 2
// between two points represented as vectors.
func SquaredDistance(a, b Vector) float64 {
return b.Subtract(a).SquaredMagnitude()
}
// Approximate attempts to move the shape in the specified direction
// to detect the closest point until the shape collides other shapes.
func Approximate(shape Shape, moveDiff Vector, turnDiff float64, shapes Shapes, intensity int, useTags bool) (Vector, float64, Shape, error) {
if shape == nil {
return Zero(), -1, nil,
fmt.Errorf("the shape being approximated is nil")
}
if shape == nil {
return Zero(), -1, nil,
fmt.Errorf("the set of shapes is nil")
}
var foundShape Shape
if intensity < 0 {
return Zero(), 0, nil,
fmt.Errorf("the value of intensity must be positive")
}
step := 1.0 / float64(intensity)
originalPos := shape.Center()
originalAngle := shape.Angle()
prevPos := originalPos
prevAngle := originalAngle
for i := 0; i < intensity; i++ {
currentPos := prevPos.Add(moveDiff.MultiplyByScalar(step))
currentAngle := prevAngle + turnDiff*step
shape.SetPosition(currentPos)
shape.SetAngle(currentAngle)
collisionFound := false
for other := range shapes {
id := shape.TypeName() + "_" + other.TypeName()
// Make sure lines will collide.
if id == "Line_Line" {
line := shape.(*Line)
otherLine := other.(*Line)
// Compare line tags.
shouldCollide, err := line.ShouldCollide(otherLine)
if err != nil {
return Zero(), 0, nil, err
}
if useTags && !shouldCollide {
continue
}
// Vice versa.
movement := currentPos.Subtract(prevPos)
turn := currentAngle - prevAngle
linesWouldIntersect, err := linesWouldCollide(
prevPos, prevAngle, movement, turn, line, otherLine)
if err != nil {
return Zero(), -1, nil, err
}
if linesWouldIntersect {
collisionFound = true
foundShape = otherLine
break
}
}
overlapped, err := ResolveCollision(shape, other, useTags)
if err != nil {
return Zero(), -1, nil, err
}
if overlapped {
collisionFound = true
foundShape = other
break
}
}
if collisionFound {
break
}
prevPos = currentPos
prevAngle = currentAngle
}
shape.SetPosition(originalPos)
shape.SetAngle(originalAngle)
if math.IsNaN(prevPos.X) || math.IsNaN(prevPos.Y) {
return Zero(), 0.0, nil, fmt.Errorf("couldn't approximate the shape")
}
return prevPos, prevAngle, foundShape, nil
}
// AdjustAngle adjusts the value of the angle so it
// is bettween 0 and 360.
func AdjustAngle(angle float64) float64 {
// Adjust the angle so its value is between 0 and 360.
if angle >= 360 {
angle = angle - float64(int64(angle/360))*360
} else if angle < 0 {
if angle <= -360 {
angle = angle - float64(int64(angle/360))*360
}
angle += 360
if angle >= 360 {
angle = angle - float64(int64(angle/360))*360
}
}
return angle
}
// linesWouldCollide returns true if the first line moved in the specified
// direction from its original position would collide the second line on the way.
func linesWouldCollide(originalPos Vector, originalAngle float64, moveDiff Vector, turnDiff float64, line, otherLine *Line) (bool, error) {
if line == nil {
return false, fmt.Errorf("the first line is nil")
}
if otherLine == nil {
return false, fmt.Errorf("the second line is nil")
}
tmpPos := line.Center()
tmpAngle := line.Angle()
line.SetPosition(originalPos)
line.SetAngle(originalAngle)
origP := line.p
origQ := line.q
otherTmpPos := otherLine.Center()
otherTmpAngle := otherLine.Angle()
otherLine.Move(moveDiff.MultiplyByScalar(-1))
otherLine.Rotate(-turnDiff)
movedP := otherLine.p
movedQ := otherLine.q
otherLine.SetPosition(otherTmpPos)
otherLine.SetAngle(otherTmpAngle)
pp := &Line{
p: movedP,
q: otherLine.p,
}
qq := &Line{
p: movedQ,
q: otherLine.q,
}
ppIntersects, err := IntersectionLineToLine(pp, line)
if err != nil {
return false, err
}
qqIntersects, err := IntersectionLineToLine(qq, line)
if err != nil {
return false, err
}
if ppIntersects ||
qqIntersects {
return true, nil
}
line.SetPosition(tmpPos)
line.SetAngle(tmpAngle)
pp = &Line{
p: origP,
q: line.p,
}
qq = &Line{
p: origQ,
q: line.q,
}
ppIntersects, err = IntersectionLineToLine(pp, otherLine)
if err != nil {
return false, err
}
qqIntersects, err = IntersectionLineToLine(qq, otherLine)
if err != nil {
return false, err
}
linesIntersect, err := IntersectionLineToLine(line, otherLine)
if err != nil {
return false, err
}
if ppIntersects ||
qqIntersects ||
linesIntersect {
return true, nil
}
return false, nil
}