/
vector2.go
496 lines (421 loc) · 9.78 KB
/
vector2.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package vector2
import (
"encoding/json"
"fmt"
"math"
"math/rand"
"github.com/EliCDavis/vector"
)
type Vector[T vector.Number] struct {
x T
y T
}
type (
Float64 = Vector[float64]
Float32 = Vector[float32]
Int = Vector[int]
Int64 = Vector[int64]
Int32 = Vector[int32]
Int16 = Vector[int16]
Int8 = Vector[int8]
)
func New[T vector.Number](x T, y T) Vector[T] {
return Vector[T]{
x: x,
y: y,
}
}
// Fill creates a vector where each component is equal to v
func Fill[T vector.Number](v T) Vector[T] {
return Vector[T]{
x: v,
y: v,
}
}
func Zero[T vector.Number]() Vector[T] {
return Vector[T]{
x: 0,
y: 0,
}
}
func Up[T vector.Number]() Vector[T] {
return Vector[T]{
x: 0,
y: 1,
}
}
func Down[T vector.Number]() Vector[T] {
return Vector[T]{
x: 0,
y: -1,
}
}
func Left[T vector.Number]() Vector[T] {
return Vector[T]{
x: -1,
y: 0,
}
}
func Right[T vector.Number]() Vector[T] {
return Vector[T]{
x: 1,
y: 0,
}
}
func One[T vector.Number]() Vector[T] {
return Vector[T]{
x: 1,
y: 1,
}
}
// Lerp linearly interpolates between a and b by t
func Lerp[T vector.Number](a, b Vector[T], t float64) Vector[T] {
return Vector[T]{
x: T((float64(b.x-a.x) * t) + float64(a.x)),
y: T((float64(b.y-a.y) * t) + float64(a.y)),
}
}
func Min[T vector.Number](a, b Vector[T]) Vector[T] {
return New(
T(math.Min(float64(a.x), float64(b.x))),
T(math.Min(float64(a.y), float64(b.y))),
)
}
func Max[T vector.Number](a, b Vector[T]) Vector[T] {
return New(
T(math.Max(float64(a.x), float64(b.x))),
T(math.Max(float64(a.y), float64(b.y))),
)
}
func MaxX[T vector.Number](a, b Vector[T]) T {
return T(math.Max(float64(a.x), float64(b.x)))
}
func MaxY[T vector.Number](a, b Vector[T]) T {
return T(math.Max(float64(a.y), float64(b.y)))
}
func MinX[T vector.Number](a, b Vector[T]) T {
return T(math.Min(float64(a.x), float64(b.x)))
}
func MinY[T vector.Number](a, b Vector[T]) T {
return T(math.Min(float64(a.y), float64(b.y)))
}
func Midpoint[T vector.Number](a, b Vector[T]) Vector[T] {
// center = (b - a)0.5 + a
// center = b0.5 - a0.5 + a
// center = b0.5 + a0.5
// center = 0.5(b + a)
return Vector[T]{
x: T(float64(a.x+b.x) * 0.5),
y: T(float64(a.y+b.y) * 0.5),
}
}
// Builds a vector from the data found from the passed in array to the best of
// it's ability. If the length of the array is smaller than the vector itself,
// only those values will be used to build the vector, and the remaining vector
// components will remain the default value of the vector's data type (some
// version of 0).
func FromArray[T vector.Number](data []T) Vector[T] {
v := Vector[T]{}
if len(data) > 0 {
v.x = data[0]
}
if len(data) > 1 {
v.y = data[1]
}
return v
}
func Rand(r *rand.Rand) Vector[float64] {
return Vector[float64]{
x: r.Float64(),
y: r.Float64(),
}
}
func (v Vector[T]) MinComponent() T {
return T(math.Min(float64(v.x), float64(v.y)))
}
func (v Vector[T]) MaxComponent() T {
return T(math.Max(float64(v.x), float64(v.y)))
}
func (v Vector[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
X float64 `json:"x"`
Y float64 `json:"y"`
}{
X: float64(v.x),
Y: float64(v.y),
})
}
func (v *Vector[T]) UnmarshalJSON(data []byte) error {
aux := &struct {
X float64 `json:"x"`
Y float64 `json:"y"`
}{
X: 0,
Y: 0,
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
v.x = T(aux.X)
v.y = T(aux.Y)
return nil
}
func (v Vector[T]) Format(format string) string {
return fmt.Sprintf(format, v.x, v.y)
}
// Sqrt applies the math.Sqrt to each component of the vector
func (v Vector[T]) Sqrt() Vector[T] {
return New(
T(math.Sqrt(float64(v.x))),
T(math.Sqrt(float64(v.y))),
)
}
func (v Vector[T]) ToInt() Vector[int] {
return Vector[int]{
x: int(v.x),
y: int(v.y),
}
}
func (v Vector[T]) ToFloat64() Vector[float64] {
return Vector[float64]{
x: float64(v.x),
y: float64(v.y),
}
}
func (v Vector[T]) Clamp(min, max T) Vector[T] {
return Vector[T]{
x: T(math.Max(math.Min(float64(v.x), float64(max)), float64(min))),
y: T(math.Max(math.Min(float64(v.y), float64(max)), float64(min))),
}
}
func (v Vector[T]) ToFloat32() Vector[float32] {
return Vector[float32]{
x: float32(v.x),
y: float32(v.y),
}
}
func (v Vector[T]) ToInt64() Vector[int64] {
return Vector[int64]{
x: int64(v.x),
y: int64(v.y),
}
}
func (v Vector[T]) X() T {
return v.x
}
// SetX changes the x component of the vector
func (v Vector[T]) SetX(newX T) Vector[T] {
return Vector[T]{
x: newX,
y: v.y,
}
}
func (v Vector[T]) Y() T {
return v.y
}
// SetY changes the y component of the vector
func (v Vector[T]) SetY(newY T) Vector[T] {
return Vector[T]{
x: v.x,
y: newY,
}
}
func (v Vector[T]) YX() Vector[T] {
return Vector[T]{
x: v.y,
y: v.x,
}
}
func (v Vector[T]) Angle(other Vector[T]) float64 {
denominator := math.Sqrt(v.LengthSquared() * other.LengthSquared())
if denominator < 1e-15 {
return 0.
}
return math.Acos(vector.Clamp(v.Dot(other)/denominator, -1., 1.))
}
// Midpoint returns the midpoint between this vector and the vector passed in.
func (v Vector[T]) Midpoint(o Vector[T]) Vector[T] {
return o.Add(v).Scale(0.5)
}
func (v Vector[T]) Dot(other Vector[T]) float64 {
return float64(v.x*other.x) + float64(v.y*other.y)
}
// Perpendicular creates a vector perpendicular to the one passed in with the
// same magnitude
func (v Vector[T]) Perpendicular() Vector[T] {
return Vector[T]{
x: v.y,
y: -v.x,
}
}
// Add returns a vector that is the result of two vectors added together
func (v Vector[T]) Add(other Vector[T]) Vector[T] {
return Vector[T]{
x: v.x + other.x,
y: v.y + other.y,
}
}
func (v Vector[T]) Sub(other Vector[T]) Vector[T] {
return Vector[T]{
x: v.x - other.x,
y: v.y - other.y,
}
}
func (v Vector[T]) Length() float64 {
return math.Sqrt(float64(v.x*v.x) + float64(v.y*v.y))
}
func (v Vector[T]) LengthSquared() float64 {
return float64(v.x*v.x) + float64(v.y*v.y)
}
func (v Vector[T]) Normalized() Vector[T] {
return v.DivByConstant(v.Length())
}
func (v Vector[T]) Scale(t float64) Vector[T] {
return Vector[T]{
x: T(float64(v.x) * t),
y: T(float64(v.y) * t),
}
}
func (v Vector[T]) MultByVector(o Vector[T]) Vector[T] {
return Vector[T]{
x: v.x * o.x,
y: v.y * o.y,
}
}
func (v Vector[T]) DivByConstant(t float64) Vector[T] {
return v.Scale(1.0 / t)
}
func (v Vector[T]) DistanceSquared(other Vector[T]) float64 {
xDist := other.x - v.x
yDist := other.y - v.y
return float64((xDist * xDist) + (yDist * yDist))
}
// Distance is the euclidean distance between two points
func (v Vector[T]) Distance(other Vector[T]) float64 {
return math.Sqrt(v.DistanceSquared(other))
}
// Round takes each component of the vector and rounds it to the nearest whole
// number
func (v Vector[T]) Round() Vector[T] {
return Vector[T]{
x: T(math.Round(float64(v.x))),
y: T(math.Round(float64(v.y))),
}
}
// RoundToInt takes each component of the vector and rounds it to the nearest
// whole number, and then casts it to a int
func (v Vector[T]) RoundToInt() Vector[int] {
return New(
int(math.Round(float64(v.x))),
int(math.Round(float64(v.y))),
)
}
// Ceil applies the ceil math operation to each component of the vector
func (v Vector[T]) Ceil() Vector[T] {
return Vector[T]{
x: T(math.Ceil(float64(v.x))),
y: T(math.Ceil(float64(v.y))),
}
}
// CeilToInt applies the ceil math operation to each component of the vector,
// and then casts it to a int
func (v Vector[T]) CeilToInt() Vector[int] {
return New(
int(math.Ceil(float64(v.x))),
int(math.Ceil(float64(v.y))),
)
}
func (v Vector[T]) Floor() Vector[T] {
return Vector[T]{
x: T(math.Floor(float64(v.x))),
y: T(math.Floor(float64(v.y))),
}
}
// FloorToInt applies the floor math operation to each component of the vector,
// and then casts it to a int
func (v Vector[T]) FloorToInt() Vector[int] {
return New(
int(math.Floor(float64(v.x))),
int(math.Floor(float64(v.y))),
)
}
// Abs applies the Abs math operation to each component of the vector
func (v Vector[T]) Abs() Vector[T] {
return Vector[T]{
x: T(math.Abs(float64(v.x))),
y: T(math.Abs(float64(v.y))),
}
}
func (v Vector[T]) NearZero() bool {
const s = 1e-8
return (math.Abs(float64(v.x)) < s) && (math.Abs(float64(v.y)) < s)
}
func (v Vector[T]) ContainsNaN() bool {
if math.IsNaN(float64(v.x)) {
return true
}
if math.IsNaN(float64(v.y)) {
return true
}
return false
}
func (v Vector[T]) Flip() Vector[T] {
return Vector[T]{
x: v.x * -1,
y: v.y * -1,
}
}
func (v Vector[T]) FlipX() Vector[T] {
return Vector[T]{
x: v.x * -1,
y: v.y,
}
}
func (v Vector[T]) FlipY() Vector[T] {
return Vector[T]{
x: v.x,
y: v.y * -1,
}
}
// Log returns the natural logarithm for each component
func (v Vector[T]) Log() Vector[T] {
return Vector[T]{
x: T(math.Log(float64(v.x))),
y: T(math.Log(float64(v.y))),
}
}
// Log10 returns the decimal logarithm for each component.
func (v Vector[T]) Log10() Vector[T] {
return Vector[T]{
x: T(math.Log10(float64(v.x))),
y: T(math.Log10(float64(v.y))),
}
}
// Log2 returns the binary logarithm for each component
func (v Vector[T]) Log2() Vector[T] {
return Vector[T]{
x: T(math.Log2(float64(v.x))),
y: T(math.Log2(float64(v.y))),
}
}
// Exp2 returns 2**x, the base-2 exponential for each component
func (v Vector[T]) Exp2() Vector[T] {
return Vector[T]{
x: T(math.Exp2(float64(v.x))),
y: T(math.Exp2(float64(v.y))),
}
}
// Exp returns e**x, the base-e exponential for each component
func (v Vector[T]) Exp() Vector[T] {
return Vector[T]{
x: T(math.Exp(float64(v.x))),
y: T(math.Exp(float64(v.y))),
}
}
// Expm1 returns e**x - 1, the base-e exponential for each component minus 1. It is more accurate than Exp(x) - 1 when the component is near zero
func (v Vector[T]) Expm1() Vector[T] {
return Vector[T]{
x: T(math.Expm1(float64(v.x))),
y: T(math.Expm1(float64(v.y))),
}
}