-
Notifications
You must be signed in to change notification settings - Fork 1
/
vec3i.go
200 lines (162 loc) · 5.43 KB
/
vec3i.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package vmath
import (
"fmt"
"math"
"github.com/maja42/vmath/mathi"
)
type Vec3i [3]int
func (v Vec3i) String() string {
return fmt.Sprintf("Vec3i[%d x %d x %d]", v[0], v[1], v[2])
}
// Format the vector to a string.
func (v Vec3i) Format(format string) string {
return fmt.Sprintf(format, v[0], v[1], v[2])
}
// Abs returns a vector with the components turned into absolute values.
func (v Vec3i) Abs() Vec3i {
return Vec3i{mathi.Abs(v[0]), mathi.Abs(v[1]), mathi.Abs(v[2])}
}
// Vec2f returns a float representation of the vector.
func (v Vec3i) Vec3f() Vec3f {
return Vec3f{float32(v[0]), float32(v[1]), float32(v[2])}
}
// Vec4i creates a 4D vector.
func (v Vec3i) Vec4i(w int) Vec4i {
return Vec4i{v[0], v[1], v[2], w}
}
// Split returns the vector's components.
func (v Vec3i) Split() (x, y, z int) {
return v[0], v[1], v[2]
}
// X returns the vector's first component.
// Performance is equivalent to using v[0].
func (v Vec3i) X() int {
return v[0]
}
// Y returns the vector's second component.
// Performance is equivalent to using v[1].
func (v Vec3i) Y() int {
return v[1]
}
// Z returns the vector's third component.
// Performance is equivalent to using v[2].
func (v Vec3i) Z() int {
return v[2]
}
// XY returns a 2D vector with the X and Y components.
func (v Vec3i) XY() Vec2i {
return Vec2i{v[0], v[1]}
}
// IsOrthogonal returns true if the vector is parallel to the X, Y or Z axis (one of its components is zero).
func (v Vec3i) IsOrthogonal() bool {
return v[0] == 0 || v[1] == 0 || v[2] == 0
}
// Add performs component-wise addition between two vectors.
func (v Vec3i) Add(other Vec3i) Vec3i {
return Vec3i{v[0] + other[0], v[1] + other[1], v[2] + other[2]}
}
// AddScalar performs a component-wise scalar addition.
func (v Vec3i) AddScalar(s int) Vec3i {
return Vec3i{v[0] + s, v[1] + s, v[2] + s}
}
// AddScalarf performs a scalar addition.
func (v Vec3i) AddScalarf(s float32) Vec3f {
return Vec3f{float32(v[0]) + s, float32(v[1]) + s, float32(v[2]) + s}
}
// Sub performs component-wise subtraction between two vectors.
func (v Vec3i) Sub(other Vec3i) Vec3i {
return Vec3i{v[0] - other[0], v[1] - other[1], v[2] - other[2]}
}
// SubScalar performs a component-wise scalar subtraction.
func (v Vec3i) SubScalar(s int) Vec3i {
return Vec3i{v[0] - s, v[1] - s, v[2] - s}
}
// SubScalarf performs a scalar subtraction.
func (v Vec3i) SubScalarf(s float32) Vec3f {
return Vec3f{float32(v[0]) - s, float32(v[1]) - s, float32(v[2]) - s}
}
// Mul performs a component-wise multiplication.
func (v Vec3i) Mul(other Vec3i) Vec3i {
return Vec3i{v[0] * other[0], v[1] * other[1], v[2] * other[2]}
}
// MulScalar performs a scalar multiplication.
func (v Vec3i) MulScalar(s int) Vec3i {
return Vec3i{v[0] * s, v[1] * s, v[2] * s}
}
// MulScalar performs a scalar multiplication.
func (v Vec3i) MulScalarf(s float32) Vec3f {
return Vec3f{float32(v[0]) * s, float32(v[1]) * s, float32(v[2]) * s}
}
// Div performs a component-wise division.
func (v Vec3i) Div(other Vec3i) Vec3i {
return Vec3i{v[0] / other[0], v[1] / other[1], v[2] / other[2]}
}
// DivScalar performs a scalar division.
func (v Vec3i) DivScalar(s int) Vec3i {
return Vec3i{v[0] / s, v[1] / s, v[2] / s}
}
// DivScalarf performs a scalar division.
func (v Vec3i) DivScalarf(s float32) Vec3f {
return Vec3f{float32(v[0]) / s, float32(v[1]) / s, float32(v[2]) / s}
}
// Length returns the vector's length.
func (v Vec3i) Length() float32 {
return float32(math.Sqrt(float64(v[0]*v[0] + v[1]*v[1] + v[2]*v[2])))
}
// SquareLength returns the vector's squared length.
func (v Vec3i) SquareLength() int {
return v[0]*v[0] + v[1]*v[1] + v[2]*v[2]
}
// IsZero returns true if all components are zero.
func (v Vec3i) IsZero() bool {
return v[0] == 0 && v[1] == 0 && v[2] == 0
}
// Equal compares two vectors component-wise.
func (v Vec3i) Equal(other Vec3i) bool {
return v[0] == other[0] && v[1] == other[1] && v[2] == other[2]
}
// Clamp clamps each component to the range of [min, max].
func (v Vec3i) Clamp(min, max int) Vec3i {
return Vec3i{
Clampi(v[0], min, max),
Clampi(v[1], min, max),
Clampi(v[2], min, max),
}
}
// Negate inverts all components.
func (v Vec3i) Negate() Vec3i {
return Vec3i{-v[0], -v[1], -v[2]}
}
// Dot performs a dot product with another vector.
func (v Vec3i) Dot(other Vec3i) int {
return v[0]*other[0] + v[1]*other[1] + v[2]*other[2]
}
// Cross performs a cross product with another vector.
func (v Vec3i) Cross(other Vec3i) Vec3i {
return Vec3i{
v[1]*other[2] - v[2]*other[1],
v[2]*other[0] - v[0]*other[2],
v[0]*other[1] - v[1]*other[0],
}
}
// IsParallel returns true if the given vector is parallel.
// Vectors that point in opposite directions are also parallel (but not collinear).
func (v Vec3i) IsParallel(other Vec3i) bool {
return v.Cross(other).SquareLength() == 0
}
// IsCollinear returns true if the given vector is collinear (pointing in the same direction).
// Uses the given Epsilon as relative tolerance.
func (v Vec3i) IsCollinear(other Vec3i) bool {
return v.IsParallel(other) &&
(v[0] >= 0) == (other[0] >= 0) && // same x direction
(v[1] >= 0) == (other[1] >= 0) && // same y direction
(v[2] >= 0) == (other[2] >= 0) // same z direction
}
// Distance returns the euclidean distance to another position.
func (v Vec3i) Distance(other Vec3i) float32 {
return other.Sub(v).Length()
}
// SquareDistance returns the squared euclidean distance to another position.
func (v Vec3i) SquareDistance(other Vec3i) int {
return other.Sub(v).SquareLength()
}