-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertex.go
76 lines (60 loc) · 940 Bytes
/
vertex.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
package shape
import "math"
var zero = Vert(0, 0)
type Vert2d struct {
X float64
Y float64
}
func Zero() *Vert2d {
c := *zero
return &c
}
func Vert(x float64, y float64) *Vert2d {
return &Vert2d{
X: x,
Y: y,
}
}
func VertAngle(r float64, angle float64) *Vert2d {
x := r * math.Cos(angle)
y := r * math.Sin(angle)
return Vert(x, y)
}
func (v *Vert2d) Invert() {
v.X = -v.X
v.Y = -v.Y
}
func (v *Vert2d) Rotate() {
x := v.X
y := v.Y
v.X = -y
v.Y = x
}
func (v *Vert2d) Multiply(other *Vert2d) {
v.X *= other.X
v.Y *= other.Y
}
func (v *Vert2d) Plus(other *Vert2d) {
v.X += other.X
v.Y += other.Y
}
func MinVert(verts ...*Vert2d) *Vert2d {
if len(verts) == 0 {
return nil
}
if len(verts) == 1 {
return verts[0]
}
x := float64(0)
y := float64(0)
for i, v := range verts {
if i == 0 {
x = v.X
y = v.Y
continue
}
x = math.Min(x, v.X)
y = math.Min(y, v.Y)
}
return Vert(x, y)
}