-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector3i.go
134 lines (120 loc) · 5.39 KB
/
vector3i.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
package xy
import "math"
// Vector3i is A 3-element structure that can be used to represent 3D grid coordinates or any other triplet of integers.
//
// It uses integer coordinates and is therefore preferable to [Vector3] when exact precision is required. Note that the
// values are limited to 32 bits, and unlike [Vector3] this cannot be configured with an engine build option. Use [Int]
// or [PackedInt64Array] if 64-bit values are needed.
//
// Note: In a boolean context, a [Vector3i] will evaluate to false if it's equal to Vector3i{0, 0, 0}. Otherwise, a
// [Vector3i] will always evaluate to true.
type Vector3i [3]int32
func (v Vector3i) X() int64 { return int64(v[X]) }
func (v Vector3i) Y() int64 { return int64(v[Y]) }
func (v Vector3i) Z() int64 { return int64(v[Z]) }
func (v *Vector3i) SetX(x int64) { v[X] = int32(x) }
func (v *Vector3i) SetY(y int64) { v[Y] = int32(y) }
func (v *Vector3i) SetZ(z int64) { v[Z] = int32(z) }
// "Constants"
func (Vector3i) ZERO() Vector3i { return Vector3i{} }
func (Vector3i) ONE() Vector3i { return Vector3i{1, 1, 1} }
func (Vector3i) MIN() Vector3i { return Vector3i{math.MinInt32, math.MinInt32, math.MinInt32} }
func (Vector3i) MAX() Vector3i { return Vector3i{math.MaxInt32, math.MaxInt32, math.MaxInt32} }
func (Vector3i) LEFT() Vector3i { return Vector3i{-1, 0, 0} }
func (Vector3i) RIGHT() Vector3i { return Vector3i{1, 0, 0} }
func (Vector3i) UP() Vector3i { return Vector3i{0, 1, 0} }
func (Vector3i) DOWN() Vector3i { return Vector3i{0, -1, 0} }
func (Vector3i) FORWARD() Vector3i { return Vector3i{0, 0, 1} }
func (Vector3i) BACK() Vector3i { return Vector3i{0, 0, -1} }
func (v Vector3i) Vector3() Vector3 { return Vector3{float(v[X]), float(v[Y]), float(v[Z])} } //Vector3(Vector3i)
// Abs returns a new vector with all components in absolute values (i.e. positive).
func (v Vector3i) Abs() Vector3i { return v.abs() } //Vector3i.abs
// Clamp returns a new vector with all components clamped between the components of min and max,
// by running [Clampi] on each component.
func (v Vector3i) Clamp(min, max Vector3i) Vector3i { //Vector3i.clamp
return Vector3i{
Clampi(v[X], min[X], max[X]),
Clampi(v[Y], min[Y], max[Y]),
Clampi(v[Z], min[Z], max[Z]),
}
}
// Length returns the length (magnitude) of this vector.
func (v Vector3i) Length() float64 { return Sqrt(float64(v.LengthSquared())) } //Vector3i.length
// LengthSquared returns the squared length (squared magnitude) of this vector.
//
// This method runs faster than length, so prefer it if you need to compare vectors
// or need the squared distance for some formula.
func (v Vector3i) LengthSquared() int64 { //Vector3i.length_squared
return int64(v[X])*int64(v[X]) + int64(v[Y])*int64(v[Y]) + int64(v[Z])*int64(v[Z])
}
// MaxAxis returns the axis of the vector's highest value. See [Axis] constants. If all components are
// equal, this method returns [X].
func (v Vector3i) MaxAxis() Axis { //Vector3i.max_axis_index
if v[X] < v[Y] {
if v[Y] < v[Z] {
return Z
}
return Y
}
if v[X] < v[Z] {
return Z
}
return X
}
// MinAxis returns the axis of the vector's lowest value. See [Axis] constants. If all components are
// equal, this method returns [Z].
func (v Vector3i) MinAxis() Axis { //Vector3i.min_axis_index
if v[X] < v[Y] {
if v[Y] < v[Z] {
return X
}
return Z
}
if v[X] < v[Z] {
return Y
}
return Z
}
// Sign returns a new vector with each component set to 1 if it's positive, -1 if it's negative, and 0 if
// it's zero. The result is identical to calling [Signi] on each component.
func (v Vector3i) Sign() Vector3i { return v.sign() } //Vector3i.sign
// Snapped returns a new vector with each component snapped to the closest multiple of the corresponding
// component in step.
func (v Vector3i) Snapped(step Vector3i) Vector3i { //Vector3i.snapped
return Vector3i{
Snappedi(v[X], step[X]),
Snappedi(v[Y], step[Y]),
Snappedi(v[Z], step[Z]),
}
}
func (v Vector3i) Add(other Vector3i) Vector3i { //Vector3i + Vector3i
return Vector3i{v[x] + other[x], v[y] + other[y], v[z] + other[z]}
}
func (v Vector3i) Sub(other Vector3i) Vector3i { //Vector3i - Vector3i
return Vector3i{v[x] - other[x], v[y] - other[y], v[z] - other[z]}
}
func (v Vector3i) Mul(other Vector3i) Vector3i { //Vector3i * Vector3i
return Vector3i{v[x] * other[x], v[y] * other[y], v[z] * other[z]}
}
func (v Vector3i) Div(other Vector3i) Vector3i { //Vector3i / Vector3i
return Vector3i{v[x] / other[x], v[y] / other[y], v[z] / other[z]}
}
func (v Vector3i) Mod(other Vector3i) Vector3i { //Vector3i % Vector3i
return Vector3i{v[x] % other[x], v[y] % other[y], v[z] % other[z]}
}
func (v Vector3i) Addi(other int64) Vector3i { //Vector3i + int64
return Vector3i{v[x] + int32(other), v[y] + int32(other), v[z] + int32(other)}
}
func (v Vector3i) Subi(other int64) Vector3i { //Vector3i - int64
return Vector3i{v[x] - int32(other), v[y] - int32(other), v[z] - int32(other)}
}
func (v Vector3i) Muli(other int64) Vector3i { //Vector3i * int64
return Vector3i{v[x] * int32(other), v[y] * int32(other), v[z] * int32(other)}
}
func (v Vector3i) Divi(other int64) Vector3i { //Vector3i / int64
return Vector3i{v[x] / int32(other), v[y] / int32(other), v[z] / int32(other)}
}
func (v Vector3i) Modi(other int64) Vector3i { //Vector3i % int64
return Vector3i{v[x] % int32(other), v[y] % int32(other), v[z] % int32(other)}
}
func (v Vector3i) Neg() Vector3i { return Vector3i{-v[x], -v[y], -v[z]} } //-Vector3i