-
Notifications
You must be signed in to change notification settings - Fork 125
/
polygon.go
112 lines (90 loc) · 2.81 KB
/
polygon.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
// Also added other functions and some tests related to geo based polygons.
package geo
import (
"math"
)
// A Polygon is carved out of a 2D plane by a set of (possibly disjoint) contours.
// It can thus contain holes, and can be self-intersecting.
type Polygon struct {
points []*Point
}
// NewPolygon: Creates and returns a new pointer to a Polygon
// composed of the passed in points. Points are
// considered to be in order such that the last point
// forms an edge with the first point.
func NewPolygon(points []*Point) *Polygon {
return &Polygon{points: points}
}
// Points returns the points of the current Polygon.
func (p *Polygon) Points() []*Point {
return p.points
}
// Add: Appends the passed in contour to the current Polygon.
func (p *Polygon) Add(point *Point) {
p.points = append(p.points, point)
}
// IsClosed returns whether or not the polygon is closed.
// TODO: This can obviously be improved, but for now,
// this should be sufficient for detecting if points
// are contained using the raycast algorithm.
func (p *Polygon) IsClosed() bool {
if len(p.points) < 3 {
return false
}
return true
}
// Contains returns whether or not the current Polygon contains the passed in Point.
func (p *Polygon) Contains(point *Point) bool {
if !p.IsClosed() {
return false
}
start := len(p.points) - 1
end := 0
contains := p.intersectsWithRaycast(point, p.points[start], p.points[end])
for i := 1; i < len(p.points); i++ {
if p.intersectsWithRaycast(point, p.points[i-1], p.points[i]) {
contains = !contains
}
}
return contains
}
// Using the raycast algorithm, this returns whether or not the passed in point
// Intersects with the edge drawn by the passed in start and end points.
// Original implementation: http://rosettacode.org/wiki/Ray-casting_algorithm#Go
func (p *Polygon) intersectsWithRaycast(point *Point, start *Point, end *Point) bool {
// Always ensure that the the first point
// has a y coordinate that is less than the second point
if start.lng > end.lng {
// Switch the points if otherwise.
start, end = end, start
}
// Move the point's y coordinate
// outside of the bounds of the testing region
// so we can start drawing a ray
for point.lng == start.lng || point.lng == end.lng {
newLng := math.Nextafter(point.lng, math.Inf(1))
point = NewPoint(point.lat, newLng)
}
// If we are outside of the polygon, indicate so.
if point.lng < start.lng || point.lng > end.lng {
return false
}
if start.lat > end.lat {
if point.lat > start.lat {
return false
}
if point.lat < end.lat {
return true
}
} else {
if point.lat > end.lat {
return false
}
if point.lat < start.lat {
return true
}
}
raySlope := (point.lng - start.lng) / (point.lat - start.lat)
diagSlope := (end.lng - start.lng) / (end.lat - start.lat)
return raySlope >= diagSlope
}