-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector2.go
615 lines (517 loc) · 14.8 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// 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
import (
"fmt"
"image"
"golang.org/x/image/math/fixed"
)
// Vec2 is a 2D vector/point with X and Y components.
type Vec2 struct {
X float32
Y float32
}
// V2 returns a new [Vec2] with the given x and y components.
func V2(x, y float32) Vec2 {
return Vec2{x, y}
}
// V2Scalar returns a new [Vec2] with all components set to the given scalar value.
func V2Scalar(s float32) Vec2 {
return Vec2{s, s}
}
// V2FromPoint returns a new [Vec2] from the given [image.Point].
func V2FromPoint(pt image.Point) Vec2 {
v := Vec2{}
v.SetPoint(pt)
return v
}
// V2FromFixed returns a new [Vec2] from the given [fixed.Point26_6].
func V2FromFixed(pt fixed.Point26_6) Vec2 {
v := Vec2{}
v.SetFixed(pt)
return v
}
// Set sets this vector's X and Y components.
func (v *Vec2) Set(x, y float32) {
v.X = x
v.Y = y
}
// SetScalar sets all vector components to same scalar value.
func (v *Vec2) SetScalar(s float32) {
v.X = s
v.Y = s
}
// SetFromVec2i sets from a Vec2i (int32) vector.
func (v *Vec2) SetFromVec2i(vi Vec2i) {
v.X = float32(vi.X)
v.Y = float32(vi.Y)
}
// SetDim sets this vector component value by its dimension index.
func (v *Vec2) SetDim(dim Dims, value float32) {
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 Vec2) Dim(dim Dims) float32 {
switch dim {
case X:
return v.X
case Y:
return v.Y
default:
panic("dim is out of range")
}
}
// SetPointDim is a helper function for image.Point for setting given dimension
func SetPointDim(v *image.Point, dim Dims, value int) {
switch dim {
case X:
v.X = value
case Y:
v.Y = value
default:
panic("dim is out of range")
}
}
// Dim returns this vector component from given image.Point
func PointDim(v image.Point, dim Dims) int {
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 *Vec2) SetByName(name string, value float32) {
switch name {
case "x", "X":
v.X = value
case "y", "Y":
v.Y = value
default:
panic("Invalid Vec2 component name: " + name)
}
}
func (a Vec2) String() string {
return fmt.Sprintf("(%v, %v)", a.X, a.Y)
}
func (a Vec2) Fixed() fixed.Point26_6 {
return ToFixedPoint(a.X, a.Y)
}
func (a *Vec2) SetAddDim(d Dims, val float32) {
switch d {
case X:
a.X += val
case Y:
a.Y += val
}
}
func (a *Vec2) SetSubDim(d Dims, val float32) {
switch d {
case X:
a.X -= val
case Y:
a.Y -= val
}
}
func (a *Vec2) SetMulDim(d Dims, val float32) {
switch d {
case X:
a.X *= val
case Y:
a.Y *= val
}
}
func (a *Vec2) SetDivDim(d Dims, val float32) {
switch d {
case X:
a.X /= val
case Y:
a.Y /= val
}
}
// set the value along a given dimension to max of current val and new val
func (a *Vec2) SetMaxDim(d Dims, val float32) {
switch d {
case X:
a.X = Max(a.X, val)
case Y:
a.Y = Max(a.Y, val)
}
}
// set the value along a given dimension to min of current val and new val
func (a *Vec2) SetMinDim(d Dims, val float32) {
switch d {
case X:
a.X = Min(a.X, val)
case Y:
a.Y = Min(a.Y, val)
}
}
// set the value along a given dimension to min of current val and new val
func (a *Vec2) SetMinPosDim(d Dims, val float32) {
switch d {
case X:
a.X = MinPos(val, a.X)
case Y:
a.Y = MinPos(val, a.Y)
}
}
func (a *Vec2) SetPoint(pt image.Point) {
a.X = float32(pt.X)
a.Y = float32(pt.Y)
}
func (a *Vec2) SetFixed(pt fixed.Point26_6) {
a.X = FromFixed(pt.X)
a.Y = FromFixed(pt.Y)
}
func (a Vec2) ToCeil() Vec2 {
return V2(Ceil(a.X), Ceil(a.Y))
}
func (a Vec2) ToFloor() Vec2 {
return V2(Floor(a.X), Floor(a.Y))
}
func (a Vec2) ToRound() Vec2 {
return V2(Round(a.X), Round(a.Y))
}
func (a Vec2) ToPoint() image.Point {
return image.Point{int(a.X), int(a.Y)}
}
func (a Vec2) ToPointCeil() image.Point {
return image.Point{int(Ceil(a.X)), int(Ceil(a.Y))}
}
func (a Vec2) ToPointFloor() image.Point {
return image.Point{int(Floor(a.X)), int(Floor(a.Y))}
}
func (a Vec2) ToPointRound() image.Point {
return image.Point{int(Round(a.X)), int(Round(a.Y))}
}
// RectFromPosSizeMax returns an image.Rectangle from max dims of pos, size
// (floor on pos, ceil on size)
func RectFromPosSizeMax(pos, sz Vec2) image.Rectangle {
tp := pos.ToPointFloor()
ts := sz.ToPointCeil()
return image.Rect(tp.X, tp.Y, tp.X+ts.X, tp.Y+ts.Y)
}
// RectFromPosSizeMin returns an image.Rectangle from min dims of pos, size
// (ceil on pos, floor on size)
func RectFromPosSizeMin(pos, sz Vec2) image.Rectangle {
tp := pos.ToPointCeil()
ts := sz.ToPointFloor()
return image.Rect(tp.X, tp.Y, tp.X+ts.X, tp.Y+ts.Y)
}
// SetZero sets this vector X and Y components to be zero.
func (v *Vec2) SetZero() {
v.SetScalar(0)
}
// FromArray sets this vector's components from the specified array and offset.
func (v *Vec2) FromArray(array []float32, offset int) {
v.X = array[offset]
v.Y = array[offset+1]
}
// ToArray copies this vector's components to array starting at offset.
func (v Vec2) ToArray(array []float32, 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 Vec2) Add(other Vec2) Vec2 {
return V2(v.X+other.X, v.Y+other.Y)
}
// AddScalar adds scalar s to each component of this vector and returns new vector.
func (v Vec2) AddScalar(s float32) Vec2 {
return V2(v.X+s, v.Y+s)
}
// SetAdd sets this to addition with other vector (i.e., += or plus-equals).
func (v *Vec2) SetAdd(other Vec2) {
v.X += other.X
v.Y += other.Y
}
// SetAddScalar sets this to addition with scalar.
func (v *Vec2) SetAddScalar(s float32) {
v.X += s
v.Y += s
}
// Sub subtracts other vector from this one and returns result in new vector.
func (v Vec2) Sub(other Vec2) Vec2 {
return V2(v.X-other.X, v.Y-other.Y)
}
// SubScalar subtracts scalar s from each component of this vector and returns new vector.
func (v Vec2) SubScalar(s float32) Vec2 {
return V2(v.X-s, v.Y-s)
}
// SetSub sets this to subtraction with other vector (i.e., -= or minus-equals).
func (v *Vec2) SetSub(other Vec2) {
v.X -= other.X
v.Y -= other.Y
}
// SetSubScalar sets this to subtraction of scalar.
func (v *Vec2) SetSubScalar(s float32) {
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 Vec2) Mul(other Vec2) Vec2 {
return V2(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 Vec2) MulScalar(s float32) Vec2 {
return V2(v.X*s, v.Y*s)
}
// SetMul sets this to multiplication with other vector (i.e., *= or times-equals).
func (v *Vec2) SetMul(other Vec2) {
v.X *= other.X
v.Y *= other.Y
}
// SetMulScalar sets this to multiplication by scalar.
func (v *Vec2) SetMulScalar(s float32) {
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 Vec2) Div(other Vec2) Vec2 {
return V2(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 Vec2) DivScalar(scalar float32) Vec2 {
if scalar != 0 {
return v.MulScalar(1 / scalar)
} else {
return Vec2{}
}
}
// SetDiv sets this to division by other vector (i.e., /= or divide-equals).
func (v *Vec2) SetDiv(other Vec2) {
v.X /= other.X
v.Y /= other.Y
}
// SetDivScalar sets this to division by scalar.
func (v *Vec2) SetDivScalar(s float32) {
if s != 0 {
v.SetMulScalar(1 / s)
} else {
v.SetZero()
}
}
// Abs returns the absolute value for each dimension
func (v Vec2) Abs() Vec2 {
return V2(Abs(v.X), Abs(v.Y))
}
// Min returns min of this vector components vs. other vector.
func (v Vec2) Min(other Vec2) Vec2 {
return V2(Min(v.X, other.X), Min(v.Y, other.Y))
}
// SetMin sets this vector components to the minimum values of itself and other vector.
func (v *Vec2) SetMin(other Vec2) {
v.X = Min(v.X, other.X)
v.Y = Min(v.Y, other.Y)
}
// Max returns max of this vector components vs. other vector.
func (v Vec2) Max(other Vec2) Vec2 {
return V2(Max(v.X, other.X), Max(v.Y, other.Y))
}
// SetMax sets this vector components to the maximum value of itself and other vector.
func (v *Vec2) SetMax(other Vec2) {
v.X = Max(v.X, other.X)
v.Y = Max(v.Y, other.Y)
}
// MinPos returns minimum of all positive (> 0) numbers
func (a Vec2) MinPos(b Vec2) Vec2 {
return V2(MinPos(a.X, b.X), MinPos(a.Y, b.Y))
}
// SetMinPos set to minpos of current vs. other
func (v *Vec2) SetMinPos(b Vec2) {
v.X = MinPos(v.X, b.X)
v.Y = MinPos(v.Y, b.Y)
}
// set the value along a given dimension to min of current val and new val
func (a *Vec2) SetMaxPos(o Vec2) {
a.X = MaxPos(o.X, a.X)
a.Y = MaxPos(o.Y, a.Y)
}
// SetMaxScalar sets to max of current value and scalar val
func (v *Vec2) SetMaxScalar(val float32) {
v.X = Max(v.X, val)
v.Y = Max(v.Y, val)
}
// SetMinScalar sets to min of current value and scalar val
func (v *Vec2) SetMinScalar(val float32) {
v.X = Min(v.X, val)
v.Y = Min(v.Y, val)
}
// SetMinPosScalar sets to minpos of current value and scalar val
func (v *Vec2) SetMinPosScalar(val float32) {
v.X = MinPos(v.X, val)
v.Y = MinPos(v.Y, val)
}
// 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 *Vec2) Clamp(min, max Vec2) {
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 *Vec2) ClampScalar(minVal, maxVal float32) {
v.Clamp(V2Scalar(minVal), V2Scalar(maxVal))
}
// Floor returns vector with mat32.Floor() applied to each of this vector's components.
func (v Vec2) Floor() Vec2 {
return V2(Floor(v.X), Floor(v.Y))
}
// SetFloor applies mat32.Floor() to each of this vector's components.
func (v *Vec2) SetFloor() {
v.X = Floor(v.X)
v.Y = Floor(v.Y)
}
// Ceil returns vector with mat32.Ceil() applied to each of this vector's components.
func (v Vec2) Ceil() Vec2 {
return V2(Ceil(v.X), Ceil(v.Y))
}
// SetCeil applies mat32.Ceil() to each of this vector's components.
func (v *Vec2) SetCeil() {
v.X = Ceil(v.X)
v.Y = Ceil(v.Y)
}
// Round returns vector with mat32.Round() applied to each of this vector's components.
func (v Vec2) Round() Vec2 {
return V2(Round(v.X), Round(v.Y))
}
// SetRound rounds each of this vector's components.
func (v *Vec2) SetRound() {
v.X = Round(v.X)
v.Y = Round(v.Y)
}
// Negate returns vector with each component negated.
func (v Vec2) Negate() Vec2 {
return V2(-v.X, -v.Y)
}
// SetNegate negates each of this vector's components.
func (v *Vec2) SetNegate() {
v.X = -v.X
v.Y = -v.Y
}
//////////////////////////////////////////////////////////////////////////////////
// Distance, Norm
// IsEqual returns if this vector is equal to other.
func (v Vec2) IsEqual(other Vec2) bool {
return (other.X == v.X) && (other.Y == v.Y)
}
// AlmostEqual returns whether the vector is almost equal to another vector within the specified tolerance.
func (v Vec2) AlmostEqual(other Vec2, tol float32) bool {
if (Abs(v.X-other.X) < tol) && (Abs(v.Y-other.Y) < tol) {
return true
}
return false
}
// Dot returns the dot product of this vector with other.
func (v Vec2) Dot(other Vec2) float32 {
return v.X*other.X + v.Y*other.Y
}
// LengthSq returns the length squared of this vector.
// LengthSq can be used to compare vectors' lengths without the need to perform a square root.
func (v Vec2) LengthSq() float32 {
return v.X*v.X + v.Y*v.Y
}
// Length returns the length of this vector.
func (v Vec2) Length() float32 {
return Sqrt(v.X*v.X + v.Y*v.Y)
}
// Normal returns this vector divided by its length
func (v Vec2) Normal() Vec2 {
return v.DivScalar(v.Length())
}
// SetNormal normalizes this vector so its length will be 1.
func (v *Vec2) SetNormal() {
v.SetDivScalar(v.Length())
}
// Normalize normalizes this vector so its length will be 1.
func (v *Vec2) Normalize() {
v.SetDivScalar(v.Length())
}
// DistTo returns the distance of this point to other.
func (v Vec2) DistTo(other Vec2) float32 {
return Sqrt(v.DistToSquared(other))
}
// DistToSquared returns the distance squared of this point to other.
func (v Vec2) DistToSquared(other Vec2) float32 {
dx := v.X - other.X
dy := v.Y - other.Y
return dx*dx + dy*dy
}
// SetLength sets this vector to have the specified length.
func (v *Vec2) SetLength(l float32) {
oldLength := v.Length()
if oldLength != 0 && l != oldLength {
v.SetMulScalar(l / oldLength)
}
}
// Cross returns the cross product of this vector with other
// which is a scalar, equivalent to the Z coord in 3D: X1 * Y2 - X2 Y1
func (v Vec2) Cross(other Vec2) float32 {
return v.X*other.Y - v.Y*other.X
}
// CosTo returns the cosine (normalized dot product) between this vector and other.
func (v Vec2) CosTo(other Vec2) float32 {
return v.Dot(other) / (v.Length() * other.Length())
}
// AngleTo returns the angle between this vector and other.
// Returns angles in range of -PI to PI (not 0 to 2 PI).
func (v Vec2) AngleTo(other Vec2) float32 {
ang := Acos(Clamp(v.CosTo(other), -1, 1))
cross := v.Cross(other)
if cross > 0 {
ang = -ang
}
return ang
}
// Lerp returns vector with each components as the linear interpolated value of
// alpha between itself and the corresponding other component.
func (v Vec2) Lerp(other Vec2, alpha float32) Vec2 {
return V2(v.X+(other.X-v.X)*alpha, v.Y+(other.Y-v.Y)*alpha)
}
// Lerp sets each of this vector's components to the linear interpolated value of
// alpha between ifself and the corresponding other component.
func (v *Vec2) SetLerp(other Vec2, alpha float32) {
v.X += (other.X - v.X) * alpha
v.Y += (other.Y - v.Y) * alpha
}
// InTriangle returns whether the vector is inside the specified triangle.
func (v Vec2) InTriangle(p0, p1, p2 Vec2) bool {
A := 0.5 * (-p1.Y*p2.X + p0.Y*(-p1.X+p2.X) + p0.X*(p1.Y-p2.Y) + p1.X*p2.Y)
sign := float32(1)
if A < 0 {
sign = float32(-1)
}
s := (p0.Y*p2.X - p0.X*p2.Y + (p2.Y-p0.Y)*v.X + (p0.X-p2.X)*v.Y) * sign
t := (p0.X*p1.Y - p0.Y*p1.X + (p0.Y-p1.Y)*v.X + (p1.X-p0.X)*v.Y) * sign
return s >= 0 && t >= 0 && (s+t) < 2*A*sign
}