-
Notifications
You must be signed in to change notification settings - Fork 1
/
point.go
130 lines (102 loc) · 2.09 KB
/
point.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
package poly2tri
import (
"math"
)
type Point struct {
X float64
Y float64
Edges []*Edge
}
func NewPoint(x, y float64) *Point {
return &Point{
X: x,
Y: y,
Edges: make([]*Edge, 0),
}
}
func (p *Point) GetX() float64 {
return p.X
}
func (p *Point) GetY() float64 {
return p.Y
}
func (p *Point) String() string {
return XYString(p)
}
func (p *Point) ToJSON() string {
panic("poly2tri:Point.ToJSON not implemented")
}
func (p *Point) Clone() *Point {
return NewPoint(p.X, p.Y)
}
func (p *Point) SetZero() *Point {
return p.Set(0, 0)
}
func (p *Point) Set(x, y float64) *Point {
p.X = x
p.Y = y
return p
}
func (p *Point) Negate() *Point {
p.X *= -1
p.Y *= -1
return p
}
func (p *Point) Add(n XYInterface) *Point {
p.X += n.GetX()
p.Y += n.GetY()
return p
}
func (p *Point) Sub(n XYInterface) *Point {
p.X -= n.GetX()
p.Y -= n.GetY()
return p
}
func (p *Point) Mul(n XYInterface) *Point {
p.X *= n.GetX()
p.Y *= n.GetY()
return p
}
func (p *Point) Length() float64 {
return math.Sqrt(p.X*p.X + p.Y*p.Y)
}
func (p *Point) Normalize() float64 {
len := p.Length()
p.X /= len
p.Y /= len
return len
}
func (p *Point) Equals(p2 XYInterface) bool {
return XYEquals(p, p2)
}
func PointNegate(p *Point) *Point {
return p.Clone().Negate()
}
func PointAdd(a, b *Point) *Point {
return a.Clone().Add(b)
}
func PointSub(a, b *Point) *Point {
return a.Clone().Sub(b)
}
func PointMul(a, b *Point) *Point {
return a.Clone().Mul(b)
}
func PointCross(a, b *Point) {
panic("poly2tri:Point.PointCross not implemented")
}
func PointString(a *Point) string {
return a.String()
}
func PointCompare(a, b *Point) float64 {
return XYCompare(a, b)
}
func PointEquals(a, b *Point) bool {
return XYEquals(a, b)
}
func PointDot(a, b XYInterface) float64 {
return a.GetX()*b.GetX() + a.GetY()*b.GetY()
}
type SortablePointsCollection []*Point
func (c SortablePointsCollection) Len() int { return len(c) }
func (c SortablePointsCollection) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c SortablePointsCollection) Less(i, j int) bool { return XYCompare(c[i], c[j]) < 0 }