-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec2.cs
1813 lines (1659 loc) · 54.3 KB
/
Vec2.cs
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// A readonly struct influenced by GLSL and OSL. This is intended
/// for storing points and directions in two-dimensional graphics
/// programs.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Explicit, Pack = 8)]
public readonly struct Vec2 : IComparable<Vec2>, IEquatable<Vec2>, IEnumerable
{
/// <summary>
/// Component on the x axis in the Cartesian coordinate system.
/// </summary>
[FieldOffset(0)] private readonly float x;
/// <summary>
/// Component on the y axis in the Cartesian coordinate system.
/// </summary>
[FieldOffset(4)] private readonly float y;
/// <summary>
/// The number of values (dimensions) in this vector.
/// </summary>
/// <value>length</value>
public int Length { get { return 2; } }
/// <summary>
/// Component on the x axis in the Cartesian coordinate system.
/// </summary>
/// <value>x</value>
public float X { get { return this.x; } }
/// <summary>
/// Component on the y axis in the Cartesian coordinate system.
/// </summary>
/// <value>y</value>
public float Y { get { return this.y; } }
/// <summary>
/// Retrieves a component by index. When the provided index is 1 or -1,
/// returns y; 0 or -2, x.
/// </summary>
/// <value>the component</value>
public float this[int i]
{
get
{
return i switch
{
0 or -2 => this.x,
1 or -1 => this.y,
_ => 0.0f,
};
}
}
/// <summary>
/// Constructs a vector from single precision real numbers.
/// </summary>
/// <param name="x">x component</param>
/// <param name="y">y component</param>
public Vec2(in float x, in float y)
{
this.x = x;
this.y = y;
}
/// <summary>
/// Constructs a vector from boolean values, where true is 1.0 and false is
/// 0.0 .
/// </summary>
/// <param name="x">x component</param>
/// <param name="y">y component</param>
public Vec2(in bool x, in bool y)
{
this.x = x ? 1.0f : 0.0f;
this.y = y ? 1.0f : 0.0f;
}
/// <summary>
/// Tests this vector for equivalence with an object. For approximate
/// equality with another vector, use the static approx function instead.
/// </summary>
/// <param name="value">the object</param>
/// <returns>equivalence</returns>
public override bool Equals(object value)
{
if (Object.ReferenceEquals(this, value)) { return true; }
if (value is null) { return false; }
if (value is Vec2 vec) { return this.Equals(vec); }
return false;
}
/// <summary>
/// Returns a hash code representing this vector.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
unchecked
{
return (Utils.MulBase ^ this.x.GetHashCode()) *
Utils.HashMul ^ this.y.GetHashCode();
}
}
/// <summary>
/// Returns a string representation of this vector.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Vec2.ToString(this);
}
/// <summary>
/// Compares this vector to another.
/// Returns 1 when a component of this vector is greater than
/// another; -1 when lesser. Returns 0 as a last resort.
/// Prioritizes the highest dimension first: y, x.
/// </summary>
/// <param name="v">the comparisand</param>
/// <returns>evaluation</returns>
public int CompareTo(Vec2 v)
{
return (this.y < v.y) ? -1 :
(this.y > v.y) ? 1 :
(this.x < v.x) ? -1 :
(this.x > v.x) ? 1 :
0;
}
/// <summary>
/// Tests this vector for equivalence with another in compliance with the
/// IEquatable interface. For approximate equality with another vector, use
/// the static approx function instead.
/// </summary>
/// <param name="v">vector</param>
/// <returns>equivalence</returns>
public bool Equals(Vec2 v)
{
if (this.y.GetHashCode() != v.y.GetHashCode()) { return false; }
if (this.x.GetHashCode() != v.x.GetHashCode()) { return false; }
return true;
}
/// <summary>
/// Returns an enumerator (or iterator) for this vector, allowing its
/// components to be accessed in a foreach loop.
/// </summary>
/// <returns>enumerator</returns>
public IEnumerator GetEnumerator()
{
yield return this.x;
yield return this.y;
}
/// <summary>
/// Converts a boolean to a vector by supplying the boolean to all the
/// vector's components: 1.0 for true; 0.0 for false.
/// </summary>
/// <param name="b">boolean</param>
public static implicit operator Vec2(in bool b)
{
float eval = b ? 1.0f : 0.0f;
return new(eval, eval);
}
/// <summary>
/// Converts a float to a vector by supplying the scalar to all the vector's
/// components.
/// </summary>
/// <param name="s">scalar</param>
public static implicit operator Vec2(in float s)
{
return new(s, s);
}
/// <summary>
/// Converts a vector to a boolean by finding whether all of its components
/// are non-zero.
/// </summary>
/// <param name="v">vector</param>
public static explicit operator bool(in Vec2 v)
{
return Vec2.All(v);
}
/// <summary>
/// A vector evaluates to true when all of its components are not equal to
/// zero.
/// </summary>
/// <param name="v">vector</param>
/// <returns>evaluation</returns>
public static bool operator true(in Vec2 v)
{
return Vec2.All(v);
}
/// <summary>
/// A vector evaluates to false when all of its components are equal to
/// zero.
/// </summary>
/// <param name="v">vector</param>
/// <returns>evaluation</returns>
public static bool operator false(in Vec2 v)
{
return Vec2.None(v);
}
/// <summary>
/// Evaluates a vector as a boolean. Equivalent to using the ! operator.
/// </summary>
/// <param name="v">vector</param>
/// <returns>opposite</returns>
public static Vec2 operator !(in Vec2 v)
{
return Vec2.Not(v);
}
/// <summary>
/// Evaluates a vector as a boolean. Equivalent to using the ~ operator.
/// </summary>
/// <param name="v">vector</param>
/// <returns>complement</returns>
public static Vec2 operator ~(in Vec2 v)
{
return Vec2.Not(v);
}
/// <summary>
/// Evaluates two vectors like booleans, using the inclusive and (AND) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Vec2 operator &(in Vec2 a, in Vec2 b)
{
return new(
Utils.And(a.x, b.x),
Utils.And(a.y, b.y));
}
/// <summary>
/// Evaluates two vectors like booleans, using the inclusive or (OR) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Vec2 operator |(in Vec2 a, in Vec2 b)
{
return new(
Utils.Or(a.x, b.x),
Utils.Or(a.y, b.y));
}
/// <summary>
/// Evaluates two vectors like booleans, using the exclusive or (XOR) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Vec2 operator ^(in Vec2 a, in Vec2 b)
{
return new(
Utils.Xor(a.x, b.x),
Utils.Xor(a.y, b.y));
}
/// <summary>
/// Negates the vector.
/// </summary>
/// <param name="v">vector</param>
/// <returns>negation</returns>
public static Vec2 operator -(in Vec2 v)
{
return new(-v.x, -v.y);
}
/// <summary>
/// Increments all components of a vector by 1.
/// </summary>
/// <param name="v">vector</param>
/// <returns>increment</returns>
public static Vec2 operator ++(in Vec2 v)
{
return new(v.x + 1.0f, v.y + 1.0f);
}
/// <summary>
/// Decrements all components of a vector by 1.
/// </summary>
/// <param name="v">vector</param>
/// <returns>decrement</returns>
public static Vec2 operator --(in Vec2 v)
{
return new(v.x - 1.0f, v.y - 1.0f);
}
/// <summary>
/// Multiplies two vectors, component-wise, i.e.,
/// returns the Hadamard product.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Vec2 operator *(in Vec2 a, in Vec2 b)
{
return new(a.x * b.x, a.y * b.y);
}
/// <summary>
/// Multiplies a vector by a scalar.
/// </summary>
/// <param name="a">left operand, the vector</param>
/// <param name="b">right operand, the scalar</param>
/// <returns>product</returns>
public static Vec2 operator *(in Vec2 a, in float b)
{
return new(a.x * b, a.y * b);
}
/// <summary>
/// Multiplies a vector by a scalar.
/// </summary>
/// <param name="a">left operand, the scalar</param>
/// <param name="b">right operand, the vector</param>
/// <returns>product</returns>
public static Vec2 operator *(in float a, in Vec2 b)
{
return new(a * b.x, a * b.y);
}
/// <summary>
/// Divides the left operand by the right, component-wise.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Vec2 operator /(in Vec2 a, in Vec2 b)
{
return new(
Utils.Div(a.x, b.x),
Utils.Div(a.y, b.y));
}
/// <summary>
/// Divides a vector by a scalar.
/// </summary>
/// <param name="a">vector, numerator</param>
/// <param name="b">scalar, denominator</param>
/// <returns>quotient</returns>
public static Vec2 operator /(in Vec2 a, in float b)
{
if (b != 0.0f)
{
float bInv = 1.0f / b;
return new(
a.x * bInv,
a.y * bInv);
}
return Vec2.Zero;
}
/// <summary>
/// Divides a scalar by a vector.
/// </summary>
/// <param name="a">scalar, numerator</param>
/// <param name="b">vector, denominator</param>
/// <returns>quotient</returns>
public static Vec2 operator /(in float a, in Vec2 b)
{
return new(
Utils.Div(a, b.x),
Utils.Div(a, b.y));
}
/// <summary>
/// Applies truncation-based modulo to the left and right vectors.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Vec2 operator %(in Vec2 a, in Vec2 b)
{
return Vec2.RemTrunc(a, b);
}
/// <summary>
/// Applies truncation-based modulo to the left and right operands.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Vec2 operator %(in Vec2 a, in float b)
{
if (b != 0.0f) { return new(a.x % b, a.y % b); }
return a;
}
/// <summary>
/// Applies truncation-based modulo to the left and right operands.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Vec2 operator %(in float a, in Vec2 b)
{
return new(
Utils.RemTrunc(a, b.x),
Utils.RemTrunc(a, b.y));
}
/// <summary>
/// Adds two vectors together.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Vec2 operator +(in Vec2 a, in Vec2 b)
{
return new(a.x + b.x, a.y + b.y);
}
/// <summary>
/// Subtracts the right vector from the left vector.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Vec2 operator -(in Vec2 a, in Vec2 b)
{
return new(a.x - b.x, a.y - b.y);
}
/// <summary>
/// Evaluates whether the left comparisand is less than the right
/// comparisand.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec2 operator <(in Vec2 a, in Vec2 b)
{
return new(a.x < b.x, a.y < b.y);
}
/// <summary>
/// Evaluates whether the left comparisand is greater than the right
/// comparisand.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec2 operator >(in Vec2 a, in Vec2 b)
{
return new(a.x > b.x, a.y > b.y);
}
/// <summary>
/// Evaluates whether the left comparisand is less than or equal to the
/// right comparisand.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec2 operator <=(in Vec2 a, in Vec2 b)
{
return new(a.x <= b.x, a.y <= b.y);
}
/// <summary>
/// Evaluates whether the left comparisand is greater than or equal to the
/// right comparisand.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec2 operator >=(in Vec2 a, in Vec2 b)
{
return new(a.x >= b.x, a.y >= b.y);
}
/// <summary>
/// Evaluates whether two vectors are not equal to each other.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec2 operator !=(in Vec2 a, in Vec2 b)
{
return new(a.x != b.x, a.y != b.y);
}
/// <summary>
/// Evaluates whether two vectors are equal to each other.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec2 operator ==(in Vec2 a, in Vec2 b)
{
return new(a.x == b.x, a.y == b.y);
}
/// <summary>
/// Finds the absolute value of each vector component.
/// </summary>
/// <param name="v">vector</param>
/// <returns>absolute vector</returns>
public static Vec2 Abs(in Vec2 v)
{
return new(
MathF.Abs(v.x),
MathF.Abs(v.y));
}
/// <summary>
/// Tests to see if all the vector's components are non-zero. Useful when
/// testing valid dimensions (width and depth) stored in vectors.
/// </summary>
/// <param name="v">vector</param>
/// <returns>evaluation</returns>
public static bool All(in Vec2 v)
{
return v.x != 0.0f && v.y != 0.0f;
}
/// <summary>
/// Finds the angle between two vectors.
/// </summary>
/// <param name="a">the first vector</param>
/// <param name="b">the second vector</param>
/// <returns>angle</returns>
public static float AngleBetween(in Vec2 a, in Vec2 b)
{
// Double precision is required for accurate angle distance.
if (Vec2.Any(a) && Vec2.Any(b))
{
double ax = a.x;
double ay = a.y;
double bx = b.x;
double by = b.y;
return (float)Math.Acos(
(ax * bx + ay * by) /
(Math.Sqrt(ax * ax + ay * ay) *
Math.Sqrt(bx * bx + by * by)));
}
return 0.0f;
}
/// <summary>
/// Tests to see if any of the vector's components are non-zero.
/// </summary>
/// <param name="v">vector</param>
/// <returns>evaluation</returns>
public static bool Any(in Vec2 v)
{
return v.x != 0.0f || v.y != 0.0f;
}
/// <summary>
/// Appends a vector to a one-dimensional vector array. Returns a new array.
/// </summary>
/// <param name="a">array</param>
/// <param name="b">vector</param>
/// <returns>array</returns>
public static Vec2[] Append(in Vec2[] a, in Vec2 b)
{
bool aNull = a == null;
if (aNull) { return new Vec2[] { b }; }
int aLen = a.Length;
Vec2[] result = new Vec2[aLen + 1];
System.Array.Copy(a, 0, result, 0, aLen);
result[aLen] = b;
return result;
}
/// <summary>
/// Tests to see if two vectors approximate each other.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <param name="tol">tolerance</param>
/// <returns>evaluation</returns>
public static bool Approx(
in Vec2 a, in Vec2 b,
in float tol = Utils.Epsilon)
{
return Utils.Approx(a.x, b.x, tol) &&
Utils.Approx(a.y, b.y, tol);
}
/// <summary>
/// Tests to see if two vectors are parallel.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <param name="tol">tolerance</param>
/// <returns>evaluation</returns>
public static bool AreParallel(
in Vec2 a, in Vec2 b,
in float tol = Utils.Epsilon)
{
return Utils.Approx(Vec2.Cross(a, b), 0.0f, tol);
}
/// <summary>
/// Returns a point on a Bezier curve described by two anchor points and two
/// control points according to a step in [0.0, 1.0] .
///
/// When the step is less than zero, returns the first anchor point. When
/// the step is greater than one, returns the second anchor point.
/// </summary>
/// <param name="ap0">first anchor point</param>
/// <param name="cp0">first control point</param>
/// <param name="cp1">second control point</param>
/// <param name="ap1">second anchor point</param>
/// <param name="step">step</param>
/// <returns>point along the curve</returns>
public static Vec2 BezierPoint(
in Vec2 ap0,
in Vec2 cp0,
in Vec2 cp1,
in Vec2 ap1,
in float step)
{
if (step <= 0.0f) { return ap0; }
else if (step >= 1.0f) { return ap1; }
float u = 1.0f - step;
float tcb = step * step;
float ucb = u * u;
float usq3t = ucb * (step + step + step);
float tsq3u = tcb * (u + u + u);
ucb *= u;
tcb *= step;
return new(
ap0.x * ucb +
cp0.x * usq3t +
cp1.x * tsq3u +
ap1.x * tcb,
ap0.y * ucb +
cp0.y * usq3t +
cp1.y * tsq3u +
ap1.y * tcb);
}
/// <summary>
/// Returns a tangent on a Bezier curve described by two anchor points and
/// two control points according to a step in [0.0, 1.0] .
///
/// When the step is less than zero, returns the first anchor point
/// subtracted from the first control point. When the step is greater than
/// one, returns the second anchor point subtracted from the second control
/// point.
/// </summary>
/// <param name="ap0">first anchor point</param>
/// <param name="cp0">first control point</param>
/// <param name="cp1">second control point</param>
/// <param name="ap1">second anchor point</param>
/// <param name="step">step</param>
/// <returns>tangent along the curve</returns>
public static Vec2 BezierTangent(
in Vec2 ap0,
in Vec2 cp0,
in Vec2 cp1,
in Vec2 ap1,
in float step)
{
if (step <= 0.0f) { return cp0 - ap0; }
else if (step >= 1.0f) { return ap1 - cp1; }
float u = 1.0f - step;
float t3 = step + step + step;
float usq3 = u * (u + u + u);
float tsq3 = step * t3;
float ut6 = u * (t3 + t3);
return new(
(cp0.x - ap0.x) * usq3 +
(cp1.x - cp0.x) * ut6 +
(ap1.x - cp1.x) * tsq3,
(cp0.y - ap0.y) * usq3 +
(cp1.y - cp0.y) * ut6 +
(ap1.y - cp1.y) * tsq3);
}
/// <summary>
/// Returns a normalized tangent on a Bezier curve.
/// </summary>
/// <param name="ap0">the first anchor point</param>
/// <param name="cp0">the first control point</param>
/// <param name="cp1">the second control point</param>
/// <param name="ap1">the second anchor point</param>
/// <param name="step">the step</param>
/// <returns>tangent along the curve</returns>
public static Vec2 BezierTanUnit(
in Vec2 ap0,
in Vec2 cp0,
in Vec2 cp1,
in Vec2 ap1,
in float step)
{
return Vec2.Normalize(Vec2.BezierTangent(
ap0, cp0, cp1, ap1, step));
}
/// <summary>
/// Raises each component of the vector to the nearest greater integer.
/// </summary>
/// <param name="v">vector</param>
/// <returns>result</returns>
public static Vec2 Ceil(in Vec2 v)
{
return new(
MathF.Ceiling(v.x),
MathF.Ceiling(v.y));
}
/// <summary>
/// Clamps a vector to a range within the lower and upper bound.
/// </summary>
/// <param name="v">vector</param>
/// <param name="lb">range lower bound</param>
/// <param name="ub">range upper bound</param>
/// <returns>clamped vector</returns>
public static Vec2 Clamp(in Vec2 v, in float lb = 0.0f, in float ub = 1.0f)
{
return new(
Utils.Clamp(v.x, lb, ub),
Utils.Clamp(v.y, lb, ub));
}
/// <summary>
/// Concatenates two one-dimensional Vec2 arrays.
/// </summary>
/// <param name="a">left array</param>
/// <param name="b">right array</param>
/// <returns>concatenation</returns>
public static Vec2[] Concat(in Vec2[] a, in Vec2[] b)
{
bool aNull = a == null;
bool bNull = b == null;
if (aNull && bNull) { return new Vec2[] { }; }
if (aNull)
{
Vec2[] result0 = new Vec2[b.Length];
System.Array.Copy(b, 0, result0, 0, b.Length);
return result0;
}
if (bNull)
{
Vec2[] result1 = new Vec2[a.Length];
System.Array.Copy(a, 0, result1, 0, a.Length);
return result1;
}
int aLen = a.Length;
int bLen = b.Length;
Vec2[] result2 = new Vec2[aLen + bLen];
System.Array.Copy(a, 0, result2, 0, aLen);
System.Array.Copy(b, 0, result2, aLen, bLen);
return result2;
}
/// <summary>
/// Tests to see if the vector contains a value
/// </summary>
/// <param name="a">vector</param>
/// <param name="b">value</param>
/// <returns>evaluation</returns>
public static bool Contains(in Vec2 a, in float b)
{
return Utils.Approx(a.x, b) ||
Utils.Approx(a.y, b);
}
/// <summary>
/// Returns the first vector with the sign of the second.
/// Returns zero where the sign is zero.
/// </summary>
/// <param name="a">magnitude</param>
/// <param name="b">sign</param>
/// <returns>signed vector</returns>
public static Vec2 CopySign(in Vec2 a, in Vec2 b)
{
return new(
Utils.CopySign(a.x, b.x),
Utils.CopySign(a.y, b.y));
}
/// <summary>
/// Returns the z component of the cross product between two vectors. The x
/// and y components of the cross between 2D vectors are always zero. For
/// that reason, the normalized cross product is equal to the sign of the
/// cross product.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>cross product z</returns>
public static float Cross(in Vec2 a, in Vec2 b)
{
return a.x * b.y - a.y * b.x;
}
/// <summary>
/// Finds the absolute value of the difference between two vectors.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>absolute difference</returns>
public static Vec2 Diff(in Vec2 a, in Vec2 b)
{
return new(
Utils.Diff(b.x, a.x),
Utils.Diff(b.y, a.y));
}
/// <summary>
/// Finds the Chebyshev distance between two vectors. Forms a square pattern
/// when plotted.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>Chebyshev distance</returns>
public static float DistChebyshev(in Vec2 a, in Vec2 b)
{
return MathF.Max(Utils.Diff(b.x, a.x),
Utils.Diff(b.y, a.y));
}
/// <summary>
/// Finds the Euclidean distance between two vectors. Where possible, use
/// distance squared to avoid the computational cost of the square-root.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>Euclidean distance</returns>
public static float DistEuclidean(in Vec2 a, in Vec2 b)
{
return MathF.Sqrt(Vec2.DistSq(a, b));
}
/// <summary>
/// Finds the Manhattan distance between two vectors. Forms a diamond
/// pattern when plotted.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>Manhattan distance</returns>
public static float DistManhattan(in Vec2 a, in Vec2 b)
{
return Utils.Diff(b.x, a.x) + Utils.Diff(b.y, a.y);
}
/// <summary>
/// Finds the Minkowski distance between two vectors. This is a
/// generalization of other distance formulae. When the exponent value, c,
/// is 1.0, the Minkowski distance equals the Manhattan distance; when it is
/// 2.0, Minkowski equals the Euclidean distance.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <param name="c">exponent</param>
/// <returns>Minkowski distance</returns>
public static float DistMinkowski(in Vec2 a, in Vec2 b, in float c = 2.0f)
{
if (c != 0.0f)
{
double cd = c;
return (float)Math.Pow(
Math.Pow(Math.Abs(b.x - a.x), cd) +
Math.Pow(Math.Abs(b.y - a.y), cd),
1.0d / cd);
}
return 0.0f;
}
/// <summary>
/// Finds the Euclidean distance squared between two vectors. Equivalent to
/// subtracting one vector from the other, then finding the dot product of
/// the difference with itself.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>distance squared</returns>
public static float DistSq(in Vec2 a, in Vec2 b)
{
float dx = b.x - a.x;
float dy = b.y - a.y;
return dx * dx + dy * dy;
}
/// <summary>
/// Finds the dot product of two vectors by summing the products of their
/// corresponding components.
///
/// dot ( a, b ) := a.x b.x + a.y b.y
///
/// The dot product of a vector with itself is equal to its magnitude
/// squared.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>dot product</returns>
public static float Dot(in Vec2 a, in Vec2 b)
{
return a.x * b.x + a.y * b.y;
}
/// <summary>
/// Negates a vector's x component.
/// </summary>
/// <param name="v">vector</param>
/// <returns>flipped</returns>
public static Vec2 FlipX(in Vec2 v)
{
return new(-v.x, v.y);
}
/// <summary>
/// Negates a vector's y component.
/// </summary>
/// <param name="v">vector</param>
/// <returns>flipped</returns>
public static Vec2 FlipY(in Vec2 v)
{
return new(v.x, -v.y);
}
/// <summary>
/// Floors each component of the vector.
/// </summary>
/// <param name="v">vector</param>
/// <returns>result</returns>
public static Vec2 Floor(in Vec2 v)
{
return new(
MathF.Floor(v.x),
MathF.Floor(v.y));
}
/// <summary>
/// Returns the fractional portion of the vector's components.
/// </summary>
/// <param name="v">vector</param>
/// <returns>fraction</returns>
public static Vec2 Fract(in Vec2 v)
{
return new(
Utils.Fract(v.x),
Utils.Fract(v.y));
}
/// <summary>
/// Creates a vector from polar coordinates: (1) theta, an angle in radians,
/// the vector's heading; (2) rho, a radius, the vector's magnitude. Uses
/// the formula
///
/// ( rho cos ( theta ), rho sin ( theta ) )
/// </summary>
/// <param name="heading">angle in radians</param>
/// <param name="radius">radius</param>
/// <returns>vector</returns>
public static Vec2 FromPolar(
in float heading = 0.0f,