-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.go
94 lines (76 loc) · 2.07 KB
/
vec.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
package v2d
import (
"github.com/chewxy/math32"
)
// Add adds the two vectors.
func (v Vec) Add(w Vec) Vec {
return Vec{X: v.X + w.X, Y: v.Y + w.Y}
}
// Sub subtracts w from v.
func (v Vec) Sub(w Vec) Vec {
return Vec{X: v.X - w.X, Y: v.Y - w.Y}
}
// Neg returns -v.
func (v Vec) Neg() Vec {
return Vec{X: -v.X, Y: -v.Y}
}
// Scale scales the vector with s.
func (v Vec) Scale(s float32) Vec {
return Vec{X: v.X * s, Y: v.Y * s}
}
// Dot takes the dot-product of v and w.
func (v Vec) Dot(w Vec) float32 {
return v.X*w.X + v.Y*w.Y
}
// Cross assumes that v, w are (x,y,0) vectors and returns the z component of 3D cross product only.
func (v Vec) Cross(w Vec) float32 {
return v.X*w.Y - v.Y*w.X
}
// CrossZ returns the cross product of (x, y, 0) and (0, 0, z).
func (v Vec) CrossZ(z float32) Vec {
return Vec{X: v.Y * z, Y: -v.X * z}
}
// CrossZ returns the cross product of (0, 0, z) and (x, y, 0).
func CrossZ(z float32, v Vec) Vec {
return Vec{X: -v.Y * z, Y: v.X * z}
}
// LengthSquared returns the length of the vector squared.
func (v Vec) LengthSquared() float32 {
return v.X*v.X + v.Y*v.Y
}
// Length returns the length of the vector.
func (v Vec) Length() float32 {
return math32.Sqrt(v.X*v.X + v.Y*v.Y)
}
// Unit returns a unit vector in the same direction.
func (v Vec) Unit() Vec {
// pre-scaling to not get underflow / overflow in
// the sqrt in .length
v = v.Scale(min(math32.Abs(1/v.X), math32.Abs(1/v.Y)))
return v.Scale(1.0 / v.Length())
}
// Orth returns a vector w of same length as v that is orthogonal to v such that
// (v, w) are "right handed".
func (v Vec) Orth() (w Vec) {
w.X = -v.Y
w.Y = v.X
return
}
// Eq returns true if v and w are equal withing tol.
func (v Vec) Eq(w Vec, tol float32) bool {
return math32.Abs(v.X-w.X) <= tol && math32.Abs(v.Y-w.Y) <= tol
}
// IsZero checks if the vector is (0,0)
func (v Vec) IsZero() bool {
return v.X == 0 && v.Y == 0
}
// Angle returns the angle of the vector (from x axis)
func (v Vec) Angle() float32 {
return math32.Atan2(v.Y, v.X)
}
func min(a, b float32) float32 {
if a < b {
return a
}
return b
}