-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshape.go
362 lines (296 loc) · 8.75 KB
/
shape.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
package geometry
import (
"math"
"math/rand"
"github.com/EliCDavis/vector/vector2"
)
// Shape is a flat (2D) arrangement of points.
type Shape []vector2.Float64
func (s Shape) GetBounds() (vector2.Float64, vector2.Float64) {
bottomLeftX := math.Inf(1)
bottomLeftY := math.Inf(1)
topRightX := math.Inf(-1)
topRightY := math.Inf(-1)
for _, p := range s {
if p.X() < bottomLeftX {
bottomLeftX = p.X()
}
if p.Y() < bottomLeftY {
bottomLeftY = p.Y()
}
if p.X() > topRightX {
topRightX = p.X()
}
if p.Y() > topRightY {
topRightY = p.Y()
}
}
return vector2.New(bottomLeftX, bottomLeftY), vector2.New(topRightX, topRightY)
}
func (s Shape) GetBoundingBoxDimensions() (width, height float64) {
bottomLeft, topRight := s.GetBounds()
return topRight.X() - bottomLeft.X(), topRight.Y() - bottomLeft.Y()
}
// RandomPointInShape returns a random point inside of the shape
func (s Shape) RandomPointInShape() vector2.Float64 {
bottomLeftBounds, topRightBounds := s.GetBounds()
for {
point := vector2.New(
bottomLeftBounds.X()+(rand.Float64()*(topRightBounds.X()-bottomLeftBounds.X())),
bottomLeftBounds.Y()+(rand.Float64()*(topRightBounds.Y()-bottomLeftBounds.Y())),
)
if s.IsInside(point) {
return point
}
}
}
// Split figures out which points land on which side of the vertical line and
// builds new shapes from that
func (s Shape) Split(vericalLine float64) ([]Shape, []Shape) {
return s.shapesOnSide(vericalLine, -1), s.shapesOnSide(vericalLine, 1)
}
func (s Shape) startinPointForSideShape(vericalLine float64, side int) (bool, int) {
startingPointIndex := 0
lowestPointHeight := 1000000.0
lastSide := 0
crossed := false
if s[len(s)-1].X() < vericalLine {
lastSide = -1
} else {
lastSide = 1
}
for i := 0; i < len(s); i++ {
n := i * -1 * side
if n < 0 {
n += len(s)
}
if lastSide == side*-1 && s[n].Y() < lowestPointHeight {
if (side == -1 && s[n].X() < vericalLine) || (side == 1 && s[n].X() > vericalLine) {
lowestPointHeight = s[n].Y()
startingPointIndex = n
}
}
newSide := 0
if s[n].X() <= vericalLine {
newSide = -1
} else {
newSide = 1
}
if lastSide != newSide {
crossed = true
}
lastSide = newSide
}
if crossed == false {
if side == lastSide {
return true, -1
}
return false, -1
}
return false, startingPointIndex
}
func (s Shape) shapesOnSide(vericalLineX float64, side int) []Shape {
onOurSide, startingPointIndex := s.startinPointForSideShape(vericalLineX, side)
if startingPointIndex == -1 {
if onOurSide {
return []Shape{s}
}
return []Shape{}
}
type region struct {
highestPoint float64
lowestPoint float64
points []vector2.Float64
started bool
}
pointBefore := startingPointIndex + side
if pointBefore >= len(s) {
pointBefore -= len(s)
} else if pointBefore < 0 {
pointBefore += len(s)
}
verticalLine := NewLine2D(vector2.New(vericalLineX, -1000000), vector2.New(vericalLineX, 1000000))
curLine := NewLine2D(s[startingPointIndex], s[pointBefore])
intersection, err := verticalLine.Intersection(curLine)
if err == ErrNoIntersection {
panic("Intersection is nil!")
}
regions := []region{{-100000, 100000, make([]vector2.Float64, 1), false}}
regions[0].points[0] = intersection
regions[0].lowestPoint = intersection.Y()
currentRegion := 0
// -1 for left, +1 for right, 0 for unset
lastPointsSide := side
for i := 0; i < len(s); i++ {
n := (i * -1 * side) + startingPointIndex
if n >= len(s) {
n -= len(s)
} else if n < 0 {
n += len(s)
}
var currentSide int
if s[n].X() <= vericalLineX {
currentSide = -1
} else {
currentSide = 1
}
// Change the region we're working with.
if currentSide != lastPointsSide {
pointBefore = n + side
if pointBefore >= len(s) {
pointBefore -= len(s)
} else if pointBefore < 0 {
pointBefore += len(s)
}
intersection, err := NewLine2D(s[n], s[pointBefore]).Intersection(verticalLine)
if err == ErrNoIntersection {
panic("Intersection is nil!")
}
if currentRegion != -1 {
if regions[currentRegion].started == false {
regions[currentRegion].highestPoint = intersection.Y()
}
regions[currentRegion].started = true
}
if currentSide == side {
foundRegion := false
// Find region we're in.
for regionIndex := range regions {
if regions[regionIndex].lowestPoint <= s[n].Y() &&
regions[regionIndex].highestPoint >= s[n].Y() {
currentRegion = regionIndex
foundRegion = true
break
}
}
// If can't find one, create one.
if foundRegion == false {
regions = append(regions, region{-100000, 100000, make([]vector2.Float64, 0), false})
currentRegion = len(regions) - 1
regions[currentRegion].lowestPoint = intersection.Y()
}
regions[currentRegion].points = append(regions[currentRegion].points, intersection)
} else {
regions[currentRegion].points = append(regions[currentRegion].points, intersection)
currentRegion = -1
}
}
if currentRegion != -1 {
if regions[currentRegion].started == false {
regions[currentRegion].highestPoint = s[n].Y()
}
regions[currentRegion].points = append(regions[currentRegion].points, s[n])
}
lastPointsSide = currentSide
}
resultingShapes := make([]Shape, 0)
for r := range regions {
if len(regions[r].points) > 2 {
resultingShapes = append(resultingShapes, Shape(regions[r].points))
}
}
return resultingShapes
}
// IsInside returns true if the point p lies inside the polygon[] with n vertices
func (s Shape) IsInside(p vector2.Float64) bool {
// There must be at least 3 vertices in polygon[]
if len(s) < 3 {
return false
}
// Create a point for line segment from p to infinite
extreme := vector2.New(math.MaxFloat64, p.Y())
// Count intersections of the above line with sides of polygon
count := 0
i := 0
for {
next := (i + 1) % len(s)
// Check if the line segment from 'p' to 'extreme' intersects
// with the line segment from 'polygon[i]' to 'polygon[next]'
if NewLine2D(s[i], s[next]).Intersects(NewLine2D(p, extreme)) {
// If the point 'p' is colinear with line segment 'i-next',
// then check if it lies on segment. If it lies, return true,
// otherwise false
if calculateOrientation(s[i], p, s[next]) == Colinear {
return onSegment(s[i], p, s[next])
}
count++
}
i = next
if i == 0 {
break
}
}
// log.Print(count)
// Return true if count is odd, false otherwise
return count%2 == 1
}
// Translate Moves all points over by the specified amount
func (s Shape) Translate(amount vector2.Float64) Shape {
newShapePonts := make([]vector2.Float64, len(s))
for i, point := range s {
newShapePonts[i] = point.Add(amount)
}
return Shape(newShapePonts)
}
// Rotate will rotate all points in the shape around the pivot by the passed in amount
func (s Shape) Rotate(amount float64, pivot vector2.Float64) Shape {
newPoints := make([]vector2.Float64, s.Len())
for p, point := range s {
// https://play.golang.org/p/qWUotd3Lb56
directionWithMag := point.Sub(pivot)
magnitude := directionWithMag.Length()
newRot := math.Atan2(directionWithMag.Y(), directionWithMag.X()) + amount
newPoints[p] = vector2.New(
math.Cos(newRot)*magnitude,
math.Sin(newRot)*magnitude,
).Add(pivot)
}
return Shape(newPoints)
}
// Scale shifts all points towards or away from the origin
func (s Shape) Scale(amount float64, origin vector2.Float64) Shape {
newShapePonts := make([]vector2.Float64, len(s))
for i, point := range s {
newShapePonts[i] = origin.Add(point.Sub(origin).Normalized().Scale(amount * origin.Distance(point)))
}
return newShapePonts
}
// Len returns the number of points in the polygon
func (s Shape) Len() int {
return len(s)
}
// Swap switches two points indeces so the polygon is ordered a different way
func (s Shape) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less determines which point is more oriented more clockwise from the center than the other
func (s Shape) Less(i, j int) bool {
center := vector2.Zero[float64]()
a := s[i]
b := s[j]
if a.X()-center.X() >= 0 && b.X()-center.X() < 0 {
return true
}
if a.X()-center.X() < 0 && b.X()-center.X() >= 0 {
return false
}
if a.X()-center.X() == 0 && b.X()-center.X() == 0 {
if a.Y()-center.Y() >= 0 || b.Y()-center.Y() >= 0 {
return a.Y() > b.Y()
}
return b.Y() > a.Y()
}
// compute the cross product of vectors (center -> a) x (center -> b)
det := (a.X()-center.X())*(b.Y()-center.Y()) - (b.X()-center.X())*(a.Y()-center.Y())
if det < 0 {
return true
}
if det > 0 {
return false
}
// points a and b are on the same line from the center
// check which point is closer to the center
d1 := (a.X()-center.X())*(a.X()-center.X()) + (a.Y()-center.Y())*(a.Y()-center.Y())
d2 := (b.X()-center.X())*(b.X()-center.X()) + (b.Y()-center.Y())*(b.Y()-center.Y())
return d1 > d2
}