-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpolygon.go
122 lines (110 loc) · 3.53 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
113
114
115
116
117
118
119
120
121
122
package collision2d
import (
"fmt"
)
//Polygon struct represents a polygon with position and edges in a counter-clockwise fashion.
type Polygon struct {
Pos, Offset Vector
Angle float64
Points, CalcPoints, Edges, Normals []Vector
}
func (polygon Polygon) String() string {
return fmt.Sprintf("{Pos:%sOffset:%sAngle: %f\nPoints: %s\nCalcPoints: %s}", polygon.Pos, polygon.Offset, polygon.Angle, polygon.Points, polygon.CalcPoints)
}
//NewPolygon creates a new polygon with pos, offset, angle and points.
//Points is an array of pairs of float64 values, that are mapped into Vectors with X and Y.
//The first value is X and the second is Y. See test to understand better.
func NewPolygon(pos, offset Vector, angle float64, points []float64) Polygon {
var vectorPoints = make([]Vector, len(points)/2)
for i := 0; i < len(points); i += 2 {
vectorPoints[i/2] = NewVector(points[i], points[i+1])
}
polygon := Polygon{Pos: pos, Offset: offset, Angle: angle}
return polygon.SetPoints(vectorPoints)
}
//SetPoints change the edges of the polygon and recauculate the rest of it's values.
func (polygon Polygon) SetPoints(points []Vector) Polygon {
polygon.CalcPoints = make([]Vector, len(points))
polygon.Edges = make([]Vector, len(points))
polygon.Normals = make([]Vector, len(points))
polygon.Points = make([]Vector, len(points))
for i := 0; i < len(points); i++ {
polygon.Points[i] = polygon.Points[i].Copy(points[i])
}
polygon.recalc()
return polygon
}
//SetAngle changes the angle of the polygon
func (polygon Polygon) SetAngle(angle float64) Polygon {
polygon.Angle = angle
polygon.recalc()
return polygon
}
//SetOffset changes the offset of the polygon
func (polygon Polygon) SetOffset(offset Vector) Polygon {
polygon.Offset = offset
polygon.recalc()
return polygon
}
//Rotate rotates the polygon by angle in radian.
func (polygon Polygon) Rotate(angle float64) Polygon {
points := polygon.Points
for i := 0; i < len(points); i++ {
points[i] = points[i].Rotate(angle)
}
polygon.recalc()
return polygon
}
//Translate the polygon by x and y.
func (polygon Polygon) Translate(x, y float64) Polygon {
points := polygon.Points
for i := 0; i < len(points); i++ {
points[i].X += x
points[i].Y += y
}
polygon.recalc()
return polygon
}
//GetAABB returns the axis-aligned bounding box of the polygon.
func (polygon Polygon) GetAABB() Polygon {
calcPoints := polygon.CalcPoints
xMin := calcPoints[0].X
yMin := calcPoints[0].Y
xMax := calcPoints[0].X
yMax := calcPoints[0].Y
for i := 1; i < len(calcPoints); i++ {
point := calcPoints[i]
if point.X < xMin {
xMin = point.X
} else if point.X > xMax {
xMax = point.X
}
if point.Y < yMin {
yMin = point.Y
} else if point.Y > yMax {
yMax = point.Y
}
}
box := NewBox(polygon.Pos.Clone().Add(NewVector(xMin, yMin)), xMax-xMin, yMax-yMin)
return box.ToPolygon()
}
func (polygon *Polygon) recalc() {
for i := 0; i < len(polygon.Points); i++ {
polygon.CalcPoints[i] = polygon.CalcPoints[i].Copy(polygon.Points[i])
polygon.CalcPoints[i].X += polygon.Offset.X
polygon.CalcPoints[i].Y += polygon.Offset.Y
if polygon.Angle != 0 {
polygon.CalcPoints[i] = polygon.CalcPoints[i].Rotate(polygon.Angle)
}
}
for i := 0; i < len(polygon.Points); i++ {
var p2 Vector
if i < len(polygon.Points)-1 {
p2 = polygon.CalcPoints[i+1]
} else {
p2 = polygon.CalcPoints[0]
}
polygon.Edges[i] = polygon.Edges[i].Copy(p2).Sub(polygon.CalcPoints[i])
polygon.Normals[i] = polygon.Normals[i].Copy(polygon.Edges[i]).Perp().Normalize()
}
}