-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvec2.go
181 lines (152 loc) · 4.83 KB
/
vec2.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
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
package math
import "math"
// Vec2 is a two-element vector of floating point compatible values that can be used for 2d
// vector arithmetic
type Vec2[T FloatingPoint] struct {
X T
Y T
}
// SetVec2 overwrites the contents of this vector with the contents of a 2-element vector
//
// in - The vector to initialize from
func (v *Vec2[T]) SetVec2(in *Vec2[T]) {
v.X = in.X
v.Y = in.Y
}
// SetVec3 overwrites the contents of this vector with the first two elements of a 3-element vector
//
// in - The vector to initialize from
func (v *Vec2[T]) SetVec3(in *Vec3[T]) {
v.X = in.X
v.Y = in.Y
}
// SetVec4 overwrites the contents of this vector with the first two elements of a 4-element vector
//
// in - The vector to initialize from
func (v *Vec2[T]) SetVec4(in *Vec4[T]) {
v.X = in.X
v.Y = in.Y
}
// SetHomogenousVec3 overwrites the contents of this vector with the first two elements of a 3-element
// vector, divided by the third element
//
// in - The vector to initialize from
func (v *Vec2[T]) SetHomogenousVec3(in *Vec3[T]) {
factor := T(1) / in.Z
v.X = in.X * factor
v.Y = in.Y * factor
}
// SetHomogenousVec4 overwrites the contents of this vector with the first two elements of a 4-element
// vector, divided by the fourth element
//
// in - The vector to initialize from
func (v *Vec2[T]) SetHomogenousVec4(in *Vec4[T]) {
factor := T(1) / in.W
v.X = in.X * factor
v.Y = in.Y * factor
}
// Normalize converts this vector into a unit vector
func (v *Vec2[T]) Normalize() {
vecLen := v.Len()
if abs[T](vecLen-1) > 0.0001 {
return
}
inverse := 1.0 / vecLen
v.X *= inverse
v.Y *= inverse
}
// Len calculates the length of this vector
func (v *Vec2[T]) Len() T {
sqr := float64(v.X*v.X + v.Y*v.Y)
return T(math.Sqrt(sqr))
}
// LenSqr calculates the length-squared of this vector. It is more performant than Len,
// owing to not requiring a math.Sqrt call, and may be preferable in cases when only the relative
// length of two vectors is required, or when comparing the length to 0 or 1
func (v *Vec2[T]) LenSqr() T {
return v.X*v.X + v.Y*v.Y
}
// AddVec2 adds another provided vector to this one and updates this vector with the results
//
// other - The right operand in the add operation
func (v *Vec2[T]) AddVec2(other *Vec2[T]) {
v.X += other.X
v.Y += other.Y
}
// SubtractVec2 subtracts another provided vector from this one and updates this vector with the results
//
// other - The right operand in the subtract operation
func (v *Vec2[T]) SubtractVec2(other *Vec2[T]) {
v.X -= other.X
v.Y -= other.Y
}
// DotProduct calculates and returns the dot product of this vector and another provided vector
//
// other - The right operand in the dot product operation
func (v *Vec2[T]) DotProduct(other *Vec2[T]) T {
return v.X*other.X + v.Y*other.Y
}
// Scale multiplies this vector by the provided scalar factor
//
// scale - The scalar to multiply this vector by
func (v *Vec2[T]) Scale(scale T) {
v.X *= scale
v.Y *= scale
}
// Equal returns true if every entry in this vector is equal to every entry in the provided vector
//
// other - The vector to compare this vector to
//
// epsilon - The epsilon value to use in floating point comparisons. This much floating point
// drift is permitted before the method returns false. 0.0001 is a common epsilon value
func (v *Vec2[T]) Equal(other *Vec2[T], epsilon T) bool {
if abs[T](v.X-other.X) > epsilon {
return false
}
if abs[T](v.Y-other.Y) > epsilon {
return false
}
return true
}
// Lerp interpolates between this vector and another provided vector, updating this
// vector to the results of the interpolation
//
// other - The target vector in the interpolation operation
//
// delta - A value between 0 and 1 indicating how far to interpolate between the two vectors
func (v *Vec2[T]) Lerp(other *Vec2[T], delta T) {
v.X = v.X*(1-delta) + other.X*delta
v.Y = v.Y*(1-delta) + other.Y*delta
}
// Rotate rotates this vector around the origin by the provided angle
//
// angleRad - The angle to rotate, in radians
func (v *Vec2[T]) Rotate(angleRad float64) {
cos := T(math.Cos(angleRad))
sin := T(math.Sin(angleRad))
x := v.X*cos - v.Y*sin
y := v.X*sin + v.Y*cos
v.X = x
v.Y = y
}
// Transform multiplies this vector through the provided 2x2 transform matrix
//
// m - The transform matrix used to transform this vector
func (v *Vec2[T]) Transform(m *Mat2x2[T]) {
x := m[0][0]*v.X + m[1][0]*v.Y
y := m[0][1]*v.X + m[1][1]*v.Y
v.X = x
v.Y = y
}
// TransformHomogenous multiplies this vector through the provided 3x3 transform matrix
// and updates this 2d vector with the results by performing a homogenous divide
//
// m - The transform matrix used to transform this vector
func (v *Vec2[T]) TransformHomogenous(m *Mat3x3[T]) {
x := m[0][0]*v.X + m[1][0]*v.Y + m[2][0]
y := m[0][1]*v.X + m[1][1]*v.Y + m[2][1]
z := m[0][2]*v.X + m[1][2]*v.Y + m[2][2]
factor := 1.0 / z
v.X = x * factor
v.Y = y * factor
}