-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector2i.go
274 lines (232 loc) · 6.81 KB
/
vector2i.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2019 The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Initially copied from G3N: github.com/g3n/engine/math32
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// with modifications needed to suit GoGi functionality.
package mat32
// Vec2i is a 2D vector/point with X and Y integer32 components.
type Vec2i struct {
X int32
Y int32
}
// V2i returns a new [Vec2i] with the given x and y components.
func V2i(x, y int32) Vec2i {
return Vec2i{X: x, Y: y}
}
// V2iScalar returns a new [Vec2i] with all components set to the given scalar value.
func V2iScalar(s int32) Vec2i {
return Vec2i{X: s, Y: s}
}
// Set sets this vector X and Y components.
func (v *Vec2i) Set(x, y int32) {
v.X = x
v.Y = y
}
// SetScalar sets all vector components to same scalar value.
func (v *Vec2i) SetScalar(s int32) {
v.X = s
v.Y = s
}
// SetFromVec2 sets from a Vec2 (float32) vector.
func (v *Vec2i) SetFromVec2(vf Vec2) {
v.X = int32(vf.X)
v.Y = int32(vf.Y)
}
// SetDim sets this vector component value by its dimension index
func (v *Vec2i) SetDim(dim Dims, value int32) {
switch dim {
case X:
v.X = value
case Y:
v.Y = value
default:
panic("dim is out of range")
}
}
// Dim returns this vector component
func (v Vec2i) Dim(dim Dims) int32 {
switch dim {
case X:
return v.X
case Y:
return v.Y
default:
panic("dim is out of range")
}
}
// SetByName sets this vector component value by its case insensitive name: "x" or "y".
func (v *Vec2i) SetByName(name string, value int32) {
switch name {
case "x", "X":
v.X = value
case "y", "Y":
v.Y = value
default:
panic("Invalid Vec2i component name: " + name)
}
}
// SetZero sets this vector X and Y components to be zero.
func (v *Vec2i) SetZero() {
v.SetScalar(0)
}
// FromArray sets this vector's components from the specified array and offset.
func (v *Vec2i) FromArray(array []int32, offset int) {
v.X = array[offset]
v.Y = array[offset+1]
}
// ToArray copies this vector's components to array starting at offset.
func (v Vec2i) ToArray(array []int32, offset int) {
array[offset] = v.X
array[offset+1] = v.Y
}
///////////////////////////////////////////////////////////////////////
// Basic math operations
// Add adds other vector to this one and returns result in a new vector.
func (v Vec2i) Add(other Vec2i) Vec2i {
return Vec2i{v.X + other.X, v.Y + other.Y}
}
// AddScalar adds scalar s to each component of this vector and returns new vector.
func (v Vec2i) AddScalar(s int32) Vec2i {
return Vec2i{v.X + s, v.Y + s}
}
// SetAdd sets this to addition with other vector (i.e., += or plus-equals).
func (v *Vec2i) SetAdd(other Vec2i) {
v.X += other.X
v.Y += other.Y
}
// SetAddScalar sets this to addition with scalar.
func (v *Vec2i) SetAddScalar(s int32) {
v.X += s
v.Y += s
}
// Sub subtracts other vector from this one and returns result in new vector.
func (v Vec2i) Sub(other Vec2i) Vec2i {
return Vec2i{v.X - other.X, v.Y - other.Y}
}
// SubScalar subtracts scalar s from each component of this vector and returns new vector.
func (v Vec2i) SubScalar(s int32) Vec2i {
return Vec2i{v.X - s, v.Y - s}
}
// SetSub sets this to subtraction with other vector (i.e., -= or minus-equals).
func (v *Vec2i) SetSub(other Vec2i) {
v.X -= other.X
v.Y -= other.Y
}
// SetSubScalar sets this to subtraction of scalar.
func (v *Vec2i) SetSubScalar(s int32) {
v.X -= s
v.Y -= s
}
// Mul multiplies each component of this vector by the corresponding one from other
// and returns resulting vector.
func (v Vec2i) Mul(other Vec2i) Vec2i {
return Vec2i{v.X * other.X, v.Y * other.Y}
}
// MulScalar multiplies each component of this vector by the scalar s and returns resulting vector.
func (v Vec2i) MulScalar(s int32) Vec2i {
return Vec2i{v.X * s, v.Y * s}
}
// SetMul sets this to multiplication with other vector (i.e., *= or times-equals).
func (v *Vec2i) SetMul(other Vec2i) {
v.X *= other.X
v.Y *= other.Y
}
// SetMulScalar sets this to multiplication by scalar.
func (v *Vec2i) SetMulScalar(s int32) {
v.X *= s
v.Y *= s
}
// Div divides each component of this vector by the corresponding one from other vector
// and returns resulting vector.
func (v Vec2i) Div(other Vec2i) Vec2i {
return Vec2i{v.X / other.X, v.Y / other.Y}
}
// DivScalar divides each component of this vector by the scalar s and returns resulting vector.
// If scalar is zero, returns zero.
func (v Vec2i) DivScalar(scalar int32) Vec2i {
if scalar != 0 {
return v.MulScalar(1 / scalar)
} else {
return Vec2i{}
}
}
// SetDiv sets this to division by other vector (i.e., /= or divide-equals).
func (v *Vec2i) SetDiv(other Vec2i) {
v.X /= other.X
v.Y /= other.Y
}
// SetDivScalar sets this to division by scalar.
func (v *Vec2i) SetDivScalar(s int32) {
if s != 0 {
v.SetMulScalar(1 / s)
} else {
v.SetZero()
}
}
// Min32i returns the min of the two int32 numbers.
func Min32i(a, b int32) int32 {
if a < b {
return a
}
return b
}
// Max32i returns the max of the two int32 numbers.
func Max32i(a, b int32) int32 {
if a > b {
return a
}
return b
}
// Min returns min of this vector components vs. other vector.
func (v Vec2i) Min(other Vec2i) Vec2i {
return Vec2i{Min32i(v.X, other.X), Min32i(v.Y, other.Y)}
}
// SetMin sets this vector components to the minimum values of itself and other vector.
func (v *Vec2i) SetMin(other Vec2i) {
v.X = Min32i(v.X, other.X)
v.Y = Min32i(v.Y, other.Y)
}
// Max returns max of this vector components vs. other vector.
func (v Vec2i) Max(other Vec2i) Vec2i {
return Vec2i{Max32i(v.X, other.X), Max32i(v.Y, other.Y)}
}
// SetMax sets this vector components to the maximum value of itself and other vector.
func (v *Vec2i) SetMax(other Vec2i) {
v.X = Max32i(v.X, other.X)
v.Y = Max32i(v.Y, other.Y)
}
// Clamp sets this vector components to be no less than the corresponding components of min
// and not greater than the corresponding component of max.
// Assumes min < max, if this assumption isn't true it will not operate correctly.
func (v *Vec2i) Clamp(min, max Vec2i) {
if v.X < min.X {
v.X = min.X
} else if v.X > max.X {
v.X = max.X
}
if v.Y < min.Y {
v.Y = min.Y
} else if v.Y > max.Y {
v.Y = max.Y
}
}
// ClampScalar sets this vector components to be no less than minVal and not greater than maxVal.
func (v *Vec2i) ClampScalar(minVal, maxVal int32) {
v.Clamp(V2iScalar(minVal), V2iScalar(maxVal))
}
// Negate returns vector with each component negated.
func (v Vec2i) Negate() Vec2i {
return Vec2i{-v.X, -v.Y}
}
// SetNegate negates each of this vector's components.
func (v *Vec2i) SetNegate() {
v.X = -v.X
v.Y = -v.Y
}
// IsEqual returns if this vector is equal to other.
func (v Vec2i) IsEqual(other Vec2i) bool {
return (other.X == v.X) && (other.Y == v.Y)
}