-
Notifications
You must be signed in to change notification settings - Fork 71
/
vector2.go
471 lines (398 loc) · 12.3 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
// Copyright 2019 Cogent Core. 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 Cogent Core functionality.
package math32
import (
"fmt"
"image"
"golang.org/x/image/math/fixed"
)
// Vector2 is a 2D vector/point with X and Y components.
type Vector2 struct {
X float32
Y float32
}
// Vec2 returns a new [Vector2] with the given x and y components.
func Vec2(x, y float32) Vector2 {
return Vector2{x, y}
}
// Vector2Scalar returns a new [Vector2] with all components set to the given scalar value.
func Vector2Scalar(scalar float32) Vector2 {
return Vector2{scalar, scalar}
}
// Vector2FromPoint returns a new [Vector2] from the given [image.Point].
func Vector2FromPoint(pt image.Point) Vector2 {
v := Vector2{}
v.SetPoint(pt)
return v
}
// Vector2FromFixed returns a new [Vector2] from the given [fixed.Point26_6].
func Vector2FromFixed(pt fixed.Point26_6) Vector2 {
v := Vector2{}
v.SetFixed(pt)
return v
}
// Set sets this vector's X and Y components.
func (v *Vector2) Set(x, y float32) {
v.X = x
v.Y = y
}
// SetScalar sets all vector components to the same scalar value.
func (v *Vector2) SetScalar(scalar float32) {
v.X = scalar
v.Y = scalar
}
// SetFromVector2i sets from a [Vector2i] (int32) vector.
func (v *Vector2) SetFromVector2i(vi Vector2i) {
v.X = float32(vi.X)
v.Y = float32(vi.Y)
}
// SetDim sets the given vector component value by its dimension index.
func (v *Vector2) 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 the given vector component.
func (v Vector2) Dim(dim Dims) float32 {
switch dim {
case X:
return v.X
case Y:
return v.Y
default:
panic("dim is out of range")
}
}
// SetPointDim sets the given dimension of the given [image.Point] to the given value.
func SetPointDim(pt *image.Point, dim Dims, value int) {
switch dim {
case X:
pt.X = value
case Y:
pt.Y = value
default:
panic("dim is out of range")
}
}
// PointDim returns the given dimension of the given [image.Point].
func PointDim(pt image.Point, dim Dims) int {
switch dim {
case X:
return pt.X
case Y:
return pt.Y
default:
panic("dim is out of range")
}
}
func (a Vector2) String() string {
return fmt.Sprintf("(%v, %v)", a.X, a.Y)
}
// SetPoint sets the vector from the given [image.Point].
func (a *Vector2) SetPoint(pt image.Point) {
a.X = float32(pt.X)
a.Y = float32(pt.Y)
}
// SetFixed sets the vector from the given [fixed.Point26_6].
func (a *Vector2) SetFixed(pt fixed.Point26_6) {
a.X = FromFixed(pt.X)
a.Y = FromFixed(pt.Y)
}
// ToPoint returns the vector as an [image.Point].
func (a Vector2) ToPoint() image.Point {
return image.Point{int(a.X), int(a.Y)}
}
// ToPointFloor returns the vector as an [image.Point] with all values [Floor]ed.
func (a Vector2) ToPointFloor() image.Point {
return image.Point{int(Floor(a.X)), int(Floor(a.Y))}
}
// ToPointCeil returns the vector as an [image.Point] with all values [Ceil]ed.
func (a Vector2) ToPointCeil() image.Point {
return image.Point{int(Ceil(a.X)), int(Ceil(a.Y))}
}
// ToPointRound returns the vector as an [image.Point] with all values [Round]ed.
func (a Vector2) ToPointRound() image.Point {
return image.Point{int(Round(a.X)), int(Round(a.Y))}
}
// ToFixed returns the vector as a [fixed.Point26_6].
func (a Vector2) ToFixed() fixed.Point26_6 {
return ToFixedPoint(a.X, a.Y)
}
// RectFromPosSizeMax returns an [image.Rectangle] from the floor of pos
// and ceil of size.
func RectFromPosSizeMax(pos, size Vector2) image.Rectangle {
tp := pos.ToPointFloor()
ts := size.ToPointCeil()
return image.Rect(tp.X, tp.Y, tp.X+ts.X, tp.Y+ts.Y)
}
// RectFromPosSizeMax returns an [image.Rectangle] from the ceil of pos
// and floor of size.
func RectFromPosSizeMin(pos, size Vector2) image.Rectangle {
tp := pos.ToPointCeil()
ts := size.ToPointFloor()
return image.Rect(tp.X, tp.Y, tp.X+ts.X, tp.Y+ts.Y)
}
// SetZero sets all of the vector's components to zero.
func (v *Vector2) SetZero() {
v.SetScalar(0)
}
// FromSlice sets this vector's components from the given slice, starting at offset.
func (v *Vector2) FromSlice(slice []float32, offset int) {
v.X = slice[offset]
v.Y = slice[offset+1]
}
// ToSlice copies this vector's components to the given slice, starting at offset.
func (v Vector2) ToSlice(slice []float32, offset int) {
slice[offset] = v.X
slice[offset+1] = v.Y
}
// Basic math operations:
// Add adds the other given vector to this one and returns the result as a new vector.
func (v Vector2) Add(other Vector2) Vector2 {
return Vec2(v.X+other.X, v.Y+other.Y)
}
// AddScalar adds scalar s to each component of this vector and returns new vector.
func (v Vector2) AddScalar(s float32) Vector2 {
return Vec2(v.X+s, v.Y+s)
}
// SetAdd sets this to addition with other vector (i.e., += or plus-equals).
func (v *Vector2) SetAdd(other Vector2) {
v.X += other.X
v.Y += other.Y
}
// SetAddScalar sets this to addition with scalar.
func (v *Vector2) SetAddScalar(s float32) {
v.X += s
v.Y += s
}
// Sub subtracts other vector from this one and returns result in new vector.
func (v Vector2) Sub(other Vector2) Vector2 {
return Vec2(v.X-other.X, v.Y-other.Y)
}
// SubScalar subtracts scalar s from each component of this vector and returns new vector.
func (v Vector2) SubScalar(s float32) Vector2 {
return Vec2(v.X-s, v.Y-s)
}
// SetSub sets this to subtraction with other vector (i.e., -= or minus-equals).
func (v *Vector2) SetSub(other Vector2) {
v.X -= other.X
v.Y -= other.Y
}
// SetSubScalar sets this to subtraction of scalar.
func (v *Vector2) 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 Vector2) Mul(other Vector2) Vector2 {
return Vec2(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 Vector2) MulScalar(s float32) Vector2 {
return Vec2(v.X*s, v.Y*s)
}
// SetMul sets this to multiplication with other vector (i.e., *= or times-equals).
func (v *Vector2) SetMul(other Vector2) {
v.X *= other.X
v.Y *= other.Y
}
// SetMulScalar sets this to multiplication by scalar.
func (v *Vector2) 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 Vector2) Div(other Vector2) Vector2 {
return Vec2(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 Vector2) DivScalar(scalar float32) Vector2 {
if scalar != 0 {
return v.MulScalar(1 / scalar)
} else {
return Vector2{}
}
}
// SetDiv sets this to division by other vector (i.e., /= or divide-equals).
func (v *Vector2) SetDiv(other Vector2) {
v.X /= other.X
v.Y /= other.Y
}
// SetDivScalar sets this to division by scalar.
func (v *Vector2) SetDivScalar(scalar float32) {
if scalar != 0 {
v.SetMulScalar(1 / scalar)
} else {
v.SetZero()
}
}
// Abs returns the vector with [Abs] applied to each component.
func (v Vector2) Abs() Vector2 {
return Vec2(Abs(v.X), Abs(v.Y))
}
// Min returns min of this vector components vs. other vector.
func (v Vector2) Min(other Vector2) Vector2 {
return Vec2(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 *Vector2) SetMin(other Vector2) {
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 Vector2) Max(other Vector2) Vector2 {
return Vec2(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 *Vector2) SetMax(other Vector2) {
v.X = Max(v.X, other.X)
v.Y = Max(v.Y, other.Y)
}
// Clamp sets this vector's 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 *Vector2) Clamp(min, max Vector2) {
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
}
}
// Floor returns this vector with [Floor] applied to each of its components.
func (v Vector2) Floor() Vector2 {
return Vec2(Floor(v.X), Floor(v.Y))
}
// Ceil returns this vector with [Ceil] applied to each of its components.
func (v Vector2) Ceil() Vector2 {
return Vec2(Ceil(v.X), Ceil(v.Y))
}
// Round returns this vector with [Round] applied to each of its components.
func (v Vector2) Round() Vector2 {
return Vec2(Round(v.X), Round(v.Y))
}
// Negate returns the vector with each component negated.
func (v Vector2) Negate() Vector2 {
return Vec2(-v.X, -v.Y)
}
// AddDim returns the vector with the given value added on the given dimension.
func (a Vector2) AddDim(d Dims, value float32) Vector2 {
switch d {
case X:
a.X += value
case Y:
a.Y += value
}
return a
}
// SubDim returns the vector with the given value subtracted on the given dimension.
func (a Vector2) SubDim(d Dims, value float32) Vector2 {
switch d {
case X:
a.X -= value
case Y:
a.Y -= value
}
return a
}
// MulDim returns the vector with the given value multiplied by on the given dimension.
func (a Vector2) MulDim(d Dims, value float32) Vector2 {
switch d {
case X:
a.X *= value
case Y:
a.Y *= value
}
return a
}
// DivDim returns the vector with the given value divided by on the given dimension.
func (a Vector2) DivDim(d Dims, value float32) Vector2 {
switch d {
case X:
a.X /= value
case Y:
a.Y /= value
}
return a
}
// Distance, Normal:
// Dot returns the dot product of this vector with the given other vector.
func (v Vector2) Dot(other Vector2) float32 {
return v.X*other.X + v.Y*other.Y
}
// Length returns the length (magnitude) of this vector.
func (v Vector2) Length() float32 {
return Sqrt(v.LengthSquared())
}
// LengthSquared returns the length squared of this vector.
// LengthSquared can be used to compare the lengths of vectors
// without the need to perform a square root.
func (v Vector2) LengthSquared() float32 {
return v.X*v.X + v.Y*v.Y
}
// Normal returns this vector divided by its length (its unit vector).
func (v Vector2) Normal() Vector2 {
return v.DivScalar(v.Length())
}
// DistanceTo returns the distance between these two vectors as points.
func (v Vector2) DistanceTo(other Vector2) float32 {
return Sqrt(v.DistanceToSquared(other))
}
// DistanceToSquared returns the squared distance between these two vectors as points.
func (v Vector2) DistanceToSquared(other Vector2) float32 {
dx := v.X - other.X
dy := v.Y - other.Y
return dx*dx + dy*dy
}
// Cross returns the cross product of this vector with other.
func (v Vector2) Cross(other Vector2) float32 {
return v.X*other.Y - v.Y*other.X
}
// CosTo returns the cosine (normalized dot product) between this vector and other.
func (v Vector2) CosTo(other Vector2) 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 Vector2) AngleTo(other Vector2) 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 Vector2) Lerp(other Vector2, alpha float32) Vector2 {
return Vec2(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 Vector2) InTriangle(p0, p1, p2 Vector2) 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
}