/
point.go
463 lines (396 loc) · 10.9 KB
/
point.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
package floatgeom
import (
"math"
"github.com/oakmound/oak/v2/alg"
)
// Point2 represents a 2D point on a plane.
type Point2 [2]float64
// Point3 represents a 3D point in space.
type Point3 [3]float64
// Point4 represents a 4D point, in space + some additional dimension.
type Point4 [4]float64
// AnglePoint creates a unit vector from the given angle in degrees as a Point2.
func AnglePoint(angle float64) Point2 {
return RadianPoint(angle * math.Pi / 180)
}
// RadianPoint creates a unit vector from the given angle in radians as a Point2.
func RadianPoint(radians float64) Point2 {
return Point2{math.Cos(radians), math.Sin(radians)}
}
// Dim returns the value of p in the ith dimension.
// Panics if i > 1.
func (p Point2) Dim(i int) float64 {
return p[i]
}
// Dim returns the value of p in the ith dimension.
// Panics if i > 2.
func (p Point3) Dim(i int) float64 {
return p[i]
}
// Dim returns the value of p in the ith dimension.
// Panics if i > 3.
func (p Point4) Dim(i int) float64 {
return p[i]
}
// X returns p's value on the X axis.
func (p Point2) X() float64 {
return p.Dim(0)
}
// Y returns p's value on the Y axis.
func (p Point2) Y() float64 {
return p.Dim(1)
}
// X returns p's value on the X axis.
func (p Point3) X() float64 {
return p.Dim(0)
}
// Y returns p's value on the Y axis.
func (p Point3) Y() float64 {
return p.Dim(1)
}
// Z returns p's value on the Z axis.
func (p Point3) Z() float64 {
return p.Dim(2)
}
// W returns p's value on the W axis.
func (p Point4) W() float64 {
return p.Dim(0)
}
// X returns p's value on the X axis.
func (p Point4) X() float64 {
return p.Dim(1)
}
// Y returns p's value on the Y axis.
func (p Point4) Y() float64 {
return p.Dim(2)
}
// Z returns p's value on the Z axis.
func (p Point4) Z() float64 {
return p.Dim(3)
}
// Distance calculates the distance between this Point2 and another.
func (p Point2) Distance(p2 Point2) float64 {
return Distance2(p.X(), p.Y(), p2.X(), p2.Y())
}
// Distance calculates the distance between this Point3 and another.
func (p Point3) Distance(p2 Point3) float64 {
return Distance3(p.X(), p.Y(), p.Z(), p2.X(), p2.Y(), p2.Z())
}
// Distance2 calculates the euclidean distance between two points, as two (x,y) pairs
func Distance2(x1, y1, x2, y2 float64) float64 {
return math.Sqrt(
math.Pow(x1-x2, 2) +
math.Pow(y1-y2, 2))
}
// Distance3 calculates the euclidean distance between two points, as two (x,y,z) triplets
func Distance3(x1, y1, z1, x2, y2, z2 float64) float64 {
return math.Sqrt(
math.Pow(x1-x2, 2) +
math.Pow(y1-y2, 2) +
math.Pow(z1-z2, 2))
}
// LesserOf returns the lowest values on each axis of the input points as a point.
func (p Point2) LesserOf(ps ...Point2) Point2 {
for _, p2 := range ps {
p[0] = math.Min(p[0], p2[0])
p[1] = math.Min(p[1], p2[1])
}
return p
}
// LesserOf returns the lowest values on each axis of the input points as a point.
func (p Point3) LesserOf(ps ...Point3) Point3 {
for _, p2 := range ps {
p[0] = math.Min(p[0], p2[0])
p[1] = math.Min(p[1], p2[1])
p[2] = math.Min(p[2], p2[2])
}
return p
}
// GreaterOf returns the highest values on each axis of the input points as a point.
func (p Point2) GreaterOf(ps ...Point2) Point2 {
for _, p2 := range ps {
p[0] = math.Max(p[0], p2[0])
p[1] = math.Max(p[1], p2[1])
}
return p
}
// GreaterOf returns the highest values on each axis of the input points as a point.
func (p Point3) GreaterOf(ps ...Point3) Point3 {
for _, p2 := range ps {
p[0] = math.Max(p[0], p2[0])
p[1] = math.Max(p[1], p2[1])
p[2] = math.Max(p[2], p2[2])
}
return p
}
// Add combines the input points via addition.
func (p Point2) Add(ps ...Point2) Point2 {
for _, p2 := range ps {
p[0] += p2[0]
p[1] += p2[1]
}
return p
}
// Sub combines the input points via subtraction.
func (p Point2) Sub(ps ...Point2) Point2 {
for _, p2 := range ps {
p[0] -= p2[0]
p[1] -= p2[1]
}
return p
}
// Mul combines in the input points via multiplication.
func (p Point2) Mul(ps ...Point2) Point2 {
for _, p2 := range ps {
p[0] *= p2[0]
p[1] *= p2[1]
}
return p
}
// MulConst multiplies all elements of a point by the input floats
func (p Point2) MulConst(fs ...float64) Point2 {
for _, f := range fs {
p[0] *= f
p[1] *= f
}
return p
}
// Cross gets the cross product of two Point 3s
func (p Point3) Cross(p2 Point3) Point3 {
return Point3{p.Y()*p2.Z() - p.Z()*p2.Y(), p.Z()*p2.X() - p.X()*p2.Z(), p.X()*p2.Y() - p.Y()*p2.X()}
}
// Div combines the input points via division.
// Div does not check that the inputs are non zero before operating,
// and can panic if that is not true.
func (p Point2) Div(ps ...Point2) Point2 {
for _, p2 := range ps {
p[0] /= p2[0]
p[1] /= p2[1]
}
return p
}
// DivConst divides all elements of a point by the input floats
// DivConst does not check that the inputs are non zero before operating,
// and can panic if that is not true.
func (p Point2) DivConst(fs ...float64) Point2 {
for _, f := range fs {
p[0] /= f
p[1] /= f
}
return p
}
// Add combines the input points via addition.
func (p Point3) Add(ps ...Point3) Point3 {
for _, p2 := range ps {
p[0] += p2[0]
p[1] += p2[1]
p[2] += p2[2]
}
return p
}
// Sub combines the input points via subtraction.
func (p Point3) Sub(ps ...Point3) Point3 {
for _, p2 := range ps {
p[0] -= p2[0]
p[1] -= p2[1]
p[2] -= p2[2]
}
return p
}
// Mul combines in the input points via multiplication.
func (p Point3) Mul(ps ...Point3) Point3 {
for _, p2 := range ps {
p[0] *= p2[0]
p[1] *= p2[1]
p[2] *= p2[2]
}
return p
}
// MulConst multiplies all elements of a point by the input floats
func (p Point3) MulConst(fs ...float64) Point3 {
for _, f := range fs {
p[0] *= f
p[1] *= f
p[2] *= f
}
return p
}
// Div combines the input points via division.
// Div does not check that the inputs are non zero before operating,
// and can panic if that is not true.
func (p Point3) Div(ps ...Point3) Point3 {
for _, p2 := range ps {
p[0] /= p2[0]
p[1] /= p2[1]
p[2] /= p2[2]
}
return p
}
// DivConst divides all elements of a point by the input floats
// DivConst does not check that the inputs are non zero before operating,
// and can panic if that is not true.
func (p Point3) DivConst(fs ...float64) Point3 {
for _, f := range fs {
p[0] /= f
p[1] /= f
p[2] /= f
}
return p
}
// MulConst multiplies all elements of a point by the input floats
func (p Point4) MulConst(fs ...float64) Point4 {
for _, f := range fs {
p[0] *= f
p[1] *= f
p[2] *= f
p[3] *= f
}
return p
}
// DivConst divides all elements of a point by the input floats
// DivConst does not check that the inputs are non zero before operating,
// and can panic if that is not true.
func (p Point4) DivConst(fs ...float64) Point4 {
for _, f := range fs {
p[0] /= f
p[1] /= f
p[2] /= f
p[3] /= f
}
return p
}
// Dot returns the dot product of the input points
func (p Point2) Dot(p2 Point2) float64 {
return p[0]*p2[0] + p[1]*p2[1]
}
// Dot returns the dot product of the input points
func (p Point3) Dot(p2 Point3) float64 {
return p[0]*p2[0] + p[1]*p2[1] + p[2]*p2[2]
}
// Dot returns the dot product of the input points
func (p Point4) Dot(p2 Point4) float64 {
return p[0]*p2[0] + p[1]*p2[1] + p[2]*p2[2] + p[3]*p2[3]
}
// Magnitude returns the magnitude of the combined components of a Point
func (p Point2) Magnitude() float64 {
return math.Sqrt(p.Dot(p))
}
// Magnitude returns the magnitude of the combined components of a Point
func (p Point3) Magnitude() float64 {
return math.Sqrt(p.Dot(p))
}
// Magnitude returns the magnitude of the combined components of a Point
func (p Point4) Magnitude() float64 {
return math.Sqrt(p.Dot(p))
}
// Normalize converts this point into a unit vector.
func (p Point2) Normalize() Point2 {
mgn := p.Magnitude()
if mgn == 0 {
return p
}
return p.DivConst(mgn)
}
// Normalize converts this point into a unit vector.
func (p Point3) Normalize() Point3 {
mgn := p.Magnitude()
if mgn == 0 {
return p
}
return p.DivConst(mgn)
}
// Normalize converts this point into a unit vector.
func (p Point4) Normalize() Point4 {
mgn := p.Magnitude()
if mgn == 0 {
return p
}
return p.DivConst(mgn)
}
// Rotate takes in a set of angles and rotates v by their sum
// the input angles are expected to be in degrees.
func (p Point2) Rotate(fs ...float64) Point2 {
angle := 0.0
for _, f := range fs {
angle += f
}
mgn := p.Magnitude()
angle = p.ToRadians() + (angle * alg.DegToRad)
return Point2{math.Cos(angle) * mgn, math.Sin(angle) * mgn}
}
// RotateRadians takes in a set of angles and rotates v by their sum
// the input angles are expected to be in radians.
func (p Point2) RotateRadians(fs ...float64) Point2 {
angle := p.ToRadians()
for _, f := range fs {
angle += f
}
mgn := p.Magnitude()
return Point2{math.Cos(angle) * mgn, math.Sin(angle) * mgn}
}
// ToRect converts this point into a rectangle spanning span distance
// in each axis.
func (p Point2) ToRect(span float64) Rect2 {
return NewRect2WH(p[0], p[1], span, span)
}
// ToRect converts this point into a rectangle spanning span distance
// in each axis.
func (p Point3) ToRect(span float64) Rect3 {
return NewRect3WH(p[0], p[1], p[2], span, span, span)
}
// ProjectX projects the Point3 onto the x axis, removing it's
// x component and returning a Point2
// todo: I'm not sure about this (these) function name
func (p Point3) ProjectX() Point2 {
return Point2{p[1], p[2]}
}
// ProjectY projects the Point3 onto the y axis, removing it's
// y component and returning a Point2
func (p Point3) ProjectY() Point2 {
return Point2{p[0], p[2]}
}
// ProjectZ projects the Point3 onto the z axis, removing it's
// z component and returning a Point2
func (p Point3) ProjectZ() Point2 {
return Point2{p[0], p[1]}
}
// ToAngle returns this point as an angle in degrees.
func (p Point2) ToAngle() float64 {
return p.ToRadians() * alg.RadToDeg
}
// ToRadians returns this point as an angle in radians.
func (p Point2) ToRadians() float64 {
return math.Atan2(p[1], p[0])
}
// AngleTo returns the angle from p to p2 in degrees.
func (p Point2) AngleTo(p2 Point2) float64 {
return p.Sub(p2).ToAngle()
}
// RadiansTo returns the angle from p to p2 in radians.
func (p Point2) RadiansTo(p2 Point2) float64 {
return p.Sub(p2).ToRadians()
}
// Conjugate returns a value of a Point4 often obtained to calculate the inverse
func (p Point4) Conjugate() Point4 {
return Point4{
p[0],
-1 * p[1],
-1 * p[2],
-1 * p[3],
}
}
// Inverse of a Point4, often used to get the inverse rotation of a quaternion
func (p Point4) Inverse() Point4 {
cng := p.Conjugate()
return cng.Normalize()
}
// MulQuat multiplies two quaternions to generate a combined quarternion that represents both rotations
// ref: https://www.mathworks.com/help/aeroblks/quaternionmultiplication.html
func (p Point4) MulQuat(p2 Point4) Point4 {
return Point4{
p2[0]*p[0] - p2[1]*p[1] - p2[2]*p[2] - p2[3]*p[3],
p2[0]*p[1] + p2[1]*p[0] - p2[2]*p[3] + p2[3]*p[2],
p2[0]*p[2] + p2[1]*p[3] + p2[2]*p[0] - p2[3]*p[1],
p2[0]*p[3] - p2[1]*p[2] + p2[2]*p[1] + p2[3]*p[0],
}
}