-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.go
More file actions
218 lines (165 loc) · 3.28 KB
/
Copy pathgeometry.go
File metadata and controls
218 lines (165 loc) · 3.28 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
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
package geometry
import (
"sort"
)
/**
* -----
* Point
* -----
*/
type Point struct {
X, Y float64
}
func (p Point) Start() Point {
return Point{0, 0}
}
func (p Point) End() Point {
return p
}
/**
* ------
* Vector
* ------
*/
type Vector interface {
Start() Point
End() Point
}
type vector [2]Point
func (v vector) Start() Point {
return v[0]
}
func (v vector) End() Point {
return v[1]
}
func VectorLength(v Vector) float64 {
// TODO: Implement
return 0
}
func VectorAngle(v Vector) float64 {
// TODO: Implement
return 0
}
func AddVectors(vs ... Vector) Vector {
// TODO: Implement
return nil
}
func ReverseVector(v Vector) Vector {
return vector{
v.End(),
v.Start(),
}
}
/**
* -------
* Polygon
* -------
*/
type Polygon interface {
Coords() []Point
}
func PolygonLen(p Polygon) int {
return len(p.Coords())
}
func PolygonCenter(p Polygon) Point {
pts := p.Coords()
c := Point{0, 0}
for _, pt := range pts {
c.X += pt.X
c.Y += pt.Y
}
c.X /= float64(len(pts))
c.Y /= float64(len(pts))
return c
}
// Smallest Enclosing Rectangle (Minimum Bounding Box)
func PolygonEnvelope(p Polygon) Polygon {
// TODO: Implement
return nil
}
func PolygonArea(p Polygon) float64 {
// Check if p is not actually a point or line
if PolygonLen(p) < 3 {
return 0
}
sp := ClockwiseSortedPolygon(p)
pts := sp.Coords()
dArea := 0.0
for j, i := len(pts)-1, 0; i < len(pts); j, i = i, i+1 {
dArea += (pts[j].X + pts[i].X) * (pts[j].Y - pts[i].Y)
}
return dArea / 2
}
/**
* --------------
* Simple Polygon
* --------------
*/
type SimplePolygon []Point
func (sp SimplePolygon) Coords() []Point {
return []Point(sp)
}
/**
* ---------
* Rectangle
* ---------
*/
// TODO: Implement a rotated rectangle
/**
* ----------------
* Sortable Polygon
* ----------------
*/
type SortablePolygon struct {
coords []Point
center Point
}
func NewSortablePolygon(p Polygon) SortablePolygon {
return SortablePolygon{p.Coords(), PolygonCenter(p)}
}
func (sp SortablePolygon) Coords() []Point {
return sp.coords
}
func (sp SortablePolygon) Center() Point {
return sp.center
}
func (sp SortablePolygon) Len() int {
return len(sp.coords)
}
func (sp SortablePolygon) Less(i, j int) bool {
a, b, center := sp.coords[i], sp.coords[j], sp.center
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)
if det := (a.X-center.X)*(b.Y-center.Y) - (b.X-center.X)*(a.Y-center.Y); 0 != det {
return det < 0
}
// 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
}
func (sp *SortablePolygon) Swap(i, j int) {
sp.coords[i], sp.coords[j] = sp.coords[j], sp.coords[i]
}
func ClockwiseSortedPolygon(p Polygon) Polygon {
sp := NewSortablePolygon(p)
sort.Sort(&sp)
return sp
}
func CounterClockwiseSortedPolygon(p Polygon) Polygon {
sp := NewSortablePolygon(p)
sort.Sort(sort.Reverse(&sp))
return sp
}