-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mutable_vector.go
128 lines (103 loc) · 3.31 KB
/
mutable_vector.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
package vector
// MutableVector is a vector where all arithmetic operations will be done in
// place on the calling vector. This will increase performance and minimize the
// memory consumption.
type MutableVector []float64
// In takes a vector and turns it into a mutable vector.
func In(a Vector) MutableVector {
return MutableVector(a)
}
// Clone a mutable vector.
func (a MutableVector) Clone() MutableVector {
return clone(a)
}
// Add a vector in place in the mutable vector
func (a MutableVector) Add(b Vector) MutableVector {
return add(a, b)
}
// Sum a vector with a vector or a set of vectors
func (a MutableVector) Sum(vectors ...Vector) MutableVector {
return sum(a, vectors)
}
// Sub subtracts a vector with another vector or a set of vectors
func (a MutableVector) Sub(b Vector) MutableVector {
return sub(a, b)
}
// Invert inverts the vector, and then returns it
func (a MutableVector) Invert() MutableVector {
return invert(a)
}
// Scale vector with a given size
func (a MutableVector) Scale(size float64) MutableVector {
return scale(a, size)
}
// Equal compares that two vectors are equal to each other
func (a MutableVector) Equal(b Vector) bool {
return equal(a, b)
}
// Magnitude of a vector
func (a MutableVector) Magnitude() float64 {
return magnitude(a)
}
// Unit returns a direction vector with the length of one.
func (a MutableVector) Unit() MutableVector {
return unit(a)
}
// Dot product of two vectors
func (a MutableVector) Dot(b Vector) float64 {
return dot(a, b)
}
// Cross product of two vectors
func (a MutableVector) Cross(b Vector) (Vector, error) {
return cross(a, b)
}
// Rotate is rotating a vector around an abitrary vector axis
// If no axis are specified it will default to rotate around the Z axis
//
// If a vector with more than 3-dimensions is rotated, it will cut the extra
// dimensions and return a 3-dimensional vector.
//
// NOTE: the ...MutableVector is just syntactic sugar that allows the vector axis to not be
// specified and default to the Z axis, if multiple axis is passed the first will be
// set as the rotational axis
func (a MutableVector) Rotate(angle float64, axis ...Vector) MutableVector {
as := Z
if len(axis) > 0 {
as = axis[0]
}
return rotate(a, angle, clone(as))
}
// Angle returns the angle in radians from the first MutableVector to the second, and an error if the two MutableVectors
// aren't of equal dimensions (length). For 0-dimension MutableVectors, the returned angle is 0. For 1-dimension MutableVectors,
// the angle is Pi if the second MutableVector's coordinate is less than the first MutableVector's coordinate, and 0 otherwise.
func (a MutableVector) Angle(axis ...Vector) float64 {
as := X
if len(axis) > 0 {
as = axis[0]
}
return angle(a, as)
}
// X is corresponding to doing a MutableVector[0] lookup, if index 0 does not exist yet, a
// 0 will be returned instead
func (a MutableVector) X() float64 {
if len(a) < 1 {
return 0
}
return a[x]
}
// Y is corresponding to doing a MutableVector[1] lookup, if index 1 does not exist yet, a
// 0 will be returned instead
func (a MutableVector) Y() float64 {
if len(a) < 2 {
return 0
}
return a[y]
}
// Z is corresponding to doing a MutableVector[2] lookup, if index 2 does not exist yet, a
// 0 will be returned instead
func (a MutableVector) Z() float64 {
if len(a) < 3 {
return 0
}
return a[z]
}