-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorExtensions.cs
1202 lines (1052 loc) · 49.8 KB
/
VectorExtensions.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 UnityEngine;
namespace Baz_geluk9.Extensions
{
/// <summary>
/// Provides versatile extensions for Vector2 and Vector3, enhancing functionality for various operations.
/// Ideal for integration into any Unity project.
/// Extensions:
/// - Set Axis
/// - Divide Vector & Axis
/// - Add Vector & Axis
/// - Subtract Vector & Axis
/// - Multiply Vector & Axis
/// - Compare Vector & Axis (with optional margin)
/// - Randomize Vector & Axis
/// - Invert Vector & Axis
/// - Calculate Midpoint between 2 vectors
/// - Calculate Weighted Average Vector
/// - IsWithinRange Vector & Axis
/// </summary>
public static class VectorExtensions
{
#region Vector 3
#region Set Axis
/// <summary>
/// Change the X vector of this Vector3
/// </summary>
/// <param name="v">This vector3.</param>
/// <param name="x">Target value for the X vector.</param>
public static ref Vector3 SetX(ref this Vector3 v, float x)
{
v.x = x;
return ref v;
}
/// <summary>
/// Change the Y vector of this Vector3.
/// </summary>
/// <param name="v">This vector3.</param>
/// <param name="y">Target value for the Y vector.</param>
public static ref Vector3 SetY(ref this Vector3 v, float y)
{
v.y = y;
return ref v;
}
/// <summary>
/// Change the Z vector of this Vector3.
/// </summary>
/// <param name="v">This vector3.</param>
/// <param name="z">Target value for the Z vector.</param>
public static ref Vector3 SetZ(ref this Vector3 v, float z)
{
v.z = z;
return ref v;
}
#endregion
#region Add
/// <summary>
/// Add the Axis of two Vector3 into one.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to add.</param>
/// <returns>A new Vector3 with combined Axis.</returns>
public static ref Vector3 Add(ref this Vector3 v, Vector3 f)
{
v += f;
return ref v;
}
/// <summary>
/// Add the specified values to the Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to add to the X Axis.</param>
/// <param name="y">The value to add to the Y Axis.</param>
/// <param name="z">The value to add to the Z Axis.</param>
/// <returns>A new Vector3 with updated Axis.</returns>
public static ref Vector3 Add(ref this Vector3 v, float x, float y, float z) => ref v.AddX(x).AddY(y).AddZ(z);
/// <summary>
/// Add the X Axis of two Vector3 into one.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to add.</param>
/// <returns>A new Vector3 with combined X Axis.</returns>
public static ref Vector3 AddX(ref this Vector3 v, Vector3 f) => ref v.AddX(f.x);
/// <summary>
/// Add the specified value to the X Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to add to the X Axis.</param>
/// <returns>A new Vector3 with updated X Axis.</returns>
public static ref Vector3 AddX(ref this Vector3 v, float x)
{
v.x += x;
return ref v;
}
/// <summary>
/// Add the Y Axis of two Vector3 into one.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to add.</param>
/// <returns>A new Vector3 with combined Y Axis.</returns>
public static ref Vector3 AddY(ref this Vector3 v, Vector3 f) => ref v.AddY(f.y);
/// <summary>
/// Add the specified value to the Y Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="y">The value to add to the Y Axis.</param>
/// <returns>A new Vector3 with updated Y Axis.</returns>
public static ref Vector3 AddY(ref this Vector3 v, float y)
{
v.y += y;
return ref v;
}
/// <summary>
/// Add the Z Axis of two Vector3 into one.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to add.</param>
/// <returns>A new Vector3 with combined Z Axis.</returns>
public static ref Vector3 AddZ(ref this Vector3 v, Vector3 f) => ref v.AddZ(f.z);
/// <summary>
/// Add the specified value to the Z Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="z">The value to add to the Z Axis.</param>
/// <returns>A new Vector3 with updated Z Axis.</returns>
public static ref Vector3 AddZ(ref this Vector3 v, float z)
{
v.z += z;
return ref v;
}
#endregion
#region Subtract
/// <summary>
/// Subtract the Axis of the second Vector3 from the first Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to subtract.</param>
/// <returns>A new Vector3 with subtracted Axis.</returns>
public static ref Vector3 Subtract(ref this Vector3 v, Vector3 f)
{
v -= f;
return ref v;
}
/// <summary>
/// Subtract the specified values from the Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to subtract from the X Axis.</param>
/// <param name="y">The value to subtract from the Y Axis.</param>
/// <param name="z">The value to subtract from the Z Axis.</param>
/// <returns>A new Vector3 with updated Axis.</returns>
public static ref Vector3 Subtract(ref this Vector3 v, float x, float y, float z) =>
ref v.SubtractX(x).SubtractY(y).SubtractZ(z);
/// <summary>
/// Subtract the X Axis of the second Vector3 from the first Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to subtract.</param>
/// <returns>A new Vector3 with subtracted X Axis.</returns>
public static ref Vector3 SubtractX(ref this Vector3 v, Vector3 f) => ref v.SubtractX(f.x);
/// <summary>
/// Subtract the specified value from the X Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to subtract from the X Axis.</param>
/// <returns>A new Vector3 with updated X Axis.</returns>
public static ref Vector3 SubtractX(ref this Vector3 v, float x)
{
v.x -= x;
return ref v;
}
/// <summary>
/// Subtract the Y Axis of the second Vector3 from the first Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to subtract.</param>
/// <returns>A new Vector3 with subtracted Y Axis.</returns>
public static ref Vector3 SubtractY(ref this Vector3 v, Vector3 f) => ref v.SubtractY(f.y);
/// <summary>
/// Subtract the specified value from the Y Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="y">The value to subtract from the Y Axis.</param>
/// <returns>A new Vector3 with updated Y Axis.</returns>
public static ref Vector3 SubtractY(ref this Vector3 v, float y)
{
v.y -= y;
return ref v;
}
/// <summary>
/// Subtract the Z Axis of the second Vector3 from the first Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to subtract.</param>
/// <returns>A new Vector3 with subtracted Z Axis.</returns>
public static ref Vector3 SubtractZ(ref this Vector3 v, Vector3 f) => ref v.SubtractZ(f.z);
/// <summary>
/// Subtract the specified value from the Z Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="z">The value to subtract from the Z Axis.</param>
/// <returns>A new Vector3 with updated Z Axis.</returns>
public static ref Vector3 SubtractZ(ref this Vector3 v, float z)
{
v.z -= z;
return ref v;
}
#endregion
#region Multiply
/// <summary>
/// Multiply the Axis of this Vector3 with the corresponding Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to multiply.</param>
/// <returns>A new Vector3 with multiplied Axis.</returns>
public static ref Vector3 Multiply(ref this Vector3 v, Vector3 f) =>
ref v.MultiplyX(f.x).MultiplyY(f.y).MultiplyZ(f.z);
/// <summary>
/// Multiply the specified values with the Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to multiply with the X Axis.</param>
/// <param name="y">The value to multiply with the Y Axis.</param>
/// <param name="z">The value to multiply with the Z Axis.</param>
/// <returns>A new Vector3 with updated Axis.</returns>
public static ref Vector3 Multiply(ref this Vector3 v, float x, float y, float z) =>
ref v.MultiplyX(x).MultiplyY(y).MultiplyZ(z);
/// <summary>
/// Multiply the X Axis of this Vector3 with the X Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to multiply.</param>
/// <returns>A new Vector3 with multiplied X Axis.</returns>
public static ref Vector3 MultiplyX(ref this Vector3 v, Vector3 f) => ref v.MultiplyX(f.x);
/// <summary>
/// Multiply the specified value with the X Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to multiply with the X Axis.</param>
/// <returns>A new Vector3 with updated X Axis.</returns>
public static ref Vector3 MultiplyX(ref this Vector3 v, float x)
{
v.x *= x;
return ref v;
}
/// <summary>
/// Multiply the Y Axis of this Vector3 with the Y Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to multiply.</param>
/// <returns>A new Vector3 with multiplied Y Axis.</returns>
public static ref Vector3 MultiplyY(ref this Vector3 v, Vector3 f) => ref v.MultiplyY(f.y);
/// <summary>
/// Multiply the specified value with the Y Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="y">The value to multiply with the Y Axis.</param>
/// <returns>A new Vector3 with updated Y Axis.</returns>
public static ref Vector3 MultiplyY(ref this Vector3 v, float y)
{
v.y *= y;
return ref v;
}
/// <summary>
/// Multiply the Z Axis of this Vector3 with the Z Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to multiply.</param>
/// <returns>A new Vector3 with multiplied Z Axis.</returns>
public static ref Vector3 MultiplyZ(ref this Vector3 v, Vector3 f) => ref v.MultiplyZ(f.z);
/// <summary>
/// Multiply the specified value with the Z Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="z">The value to multiply with the Z Axis.</param>
/// <returns>A new Vector3 with updated Z Axis.</returns>
public static ref Vector3 MultiplyZ(ref this Vector3 v, float z)
{
v.z *= z;
return ref v;
}
#endregion
#region Divide
/// <summary>
/// Divide the Axis of this Vector3 by the corresponding Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to divide by.</param>
/// <returns>A new Vector3 with divided Axis.</returns>
public static ref Vector3 Divide(ref this Vector3 v, Vector3 f) => ref v.DivideX(f.x).DivideY(f.y).DivideZ(f.z);
/// <summary>
/// Divide the specified values by the Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to divide by the X Axis.</param>
/// <param name="y">The value to divide by the Y Axis.</param>
/// <param name="z">The value to divide by the Z Axis.</param>
/// <returns>A new Vector3 with updated Axis.</returns>
public static ref Vector3 Divide(ref this Vector3 v, float x, float y, float z) =>
ref v.DivideX(x).DivideY(y).DivideZ(z);
/// <summary>
/// Divide the X Axis of this Vector3 by the X Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to divide by.</param>
/// <returns>A new Vector3 with divided X Axis.</returns>
public static ref Vector3 DivideX(ref this Vector3 v, Vector3 f) => ref v.DivideX(f.x);
/// <summary>
/// Divide the X Axis of this Vector3 by the specified value and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to divide by.</param>
/// <returns>A new Vector3 with updated X Axis.</returns>
public static ref Vector3 DivideX(ref this Vector3 v, float x)
{
v.x /= x;
return ref v;
}
/// <summary>
/// Divide the Y Axis of this Vector3 by the Y Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to divide by.</param>
/// <returns>A new Vector3 with divided Y Axis.</returns>
public static ref Vector3 DivideY(ref this Vector3 v, Vector3 f) => ref v.DivideY(f.y);
/// <summary>
/// Divide the Y Axis of this Vector3 by the specified value and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="y">The value to divide by.</param>
/// <returns>A new Vector3 with updated Y Axis.</returns>
public static ref Vector3 DivideY(ref this Vector3 v, float y)
{
v.y /= y;
return ref v;
}
/// <summary>
/// Divide the Z Axis of this Vector3 by the Z Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The Vector3 to divide by.</param>
/// <returns>A new Vector3 with divided Z Axis.</returns>
public static ref Vector3 DivideZ(ref this Vector3 v, Vector3 f) => ref v.DivideZ(f.z);
/// <summary>
/// Divide the Z Axis of this Vector3 by the specified value and return the result.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="z">The value to divide by.</param>
/// <returns>A new Vector3 with updated Z Axis.</returns>
public static ref Vector3 DivideZ(ref this Vector3 v, float z)
{
v.z /= z;
return ref v;
}
#endregion
#region Compare
/// <summary>
/// Compare two Vector3 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The f Vector3 to compare with.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the vectors are approximately equal within the specified margin.</returns>
public static bool Compare(this Vector3 v, Vector3 f, float margin = 0.0001f)
{
return v.CompareX(f.x, margin)
&& v.CompareY(f.y, margin)
&& v.CompareZ(f.x, margin);
}
/// <summary>
/// Compare the X axis of two Vector3 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The f Vector3 to compare with.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the X axes are approximately equal within the specified margin.</returns>
public static bool CompareX(this Vector3 v, Vector3 f, float margin = 0.0001f) => v.CompareX(f.x, margin);
/// <summary>
/// Compare a float value with the X Axis of this Vector3 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The float value to compare with the X Axis.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the X Axis is approximately equal to the specified float value within the margin.</returns>
public static bool CompareX(this Vector3 v, float x, float margin = 0.0001f) => Mathf.Abs(v.x - x) < margin;
/// <summary>
/// Compare the Y axis of two Vector3 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The f Vector3 to compare with.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the Y axes are approximately equal within the specified margin.</returns>
public static bool CompareY(this Vector3 v, Vector3 f, float margin = 0.0001f) => v.CompareY(f.y, margin);
/// <summary>
/// Compare a float value with the Y Axis of this Vector3 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="y">The float value to compare with the Y Axis.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the Y Axis is approximately equal to the specified float value within the margin.</returns>
public static bool CompareY(this Vector3 v, float y, float margin = 0.0001f) => Mathf.Abs(v.y - y) < margin;
/// <summary>
/// Compare the Z axis of two Vector3 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The f Vector3 to compare with.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the Z axes are approximately equal within the specified margin.</returns>
public static bool CompareZ(this Vector3 v, Vector3 f, float margin = 0.0001f) => v.CompareZ(f.z, margin);
/// <summary>
/// Compare a float value with the Z Axis of this Vector3 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="z">The float value to compare with the Z Axis.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the Z Axis is approximately equal to the specified float value within the margin.</returns>
public static bool CompareZ(this Vector3 v, float z, float margin = 0.0001f) => Mathf.Abs(v.z - z) < margin;
#endregion
#region Randomize
/// <summary>
/// Randomize the X, Y, and Z axes of this Vector3 within specified ranges.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="xRange">Range for the X axis.</param>
/// <param name="yRange">Range for the Y axis.</param>
/// <param name="zRange">Range for the Z axis.</param>
/// <returns>A new Vector3 with randomized axes.</returns>
public static ref Vector3 Randomize(ref this Vector3 v, Vector2 xRange, Vector2 yRange, Vector2 zRange)
{
v.RandomizeX(xRange);
v.RandomizeY(yRange);
v.RandomizeZ(zRange);
return ref v;
}
/// <summary>
/// Randomize the X, Y, and Z axes of this Vector3 within the specified range.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="range">Range for all axes.</param>
/// <returns>A new Vector3 with randomized axes.</returns>
public static ref Vector3 Randomize(ref this Vector3 v, Vector2 range)
{
v.RandomizeX(range);
v.RandomizeY(range);
v.RandomizeZ(range);
return ref v;
}
/// <summary>
/// Randomize all axes of this Vector3 with the same random number within the specified range.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="range">Range for all axes.</param>
/// <returns>A new Vector3 with the same randomized value for all axes.</returns>
public static ref Vector3 RandomizeUniform(ref this Vector3 v, Vector2 range)
{
float randomValue = Random.Range(range.x, range.y);
v.x = randomValue;
v.y = randomValue;
v.z = randomValue;
return ref v;
}
/// <summary>
/// Randomize the X axis of this Vector3 within a specified range.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="xRange">Range for the X axis.</param>
/// <returns>A new Vector3 with a randomized X axis.</returns>
public static ref Vector3 RandomizeX(ref this Vector3 v, Vector2 xRange)
{
v.x = Random.Range(xRange.x, xRange.y);
return ref v;
}
/// <summary>
/// Randomize the Y axis of this Vector3 within a specified range.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="yRange">Range for the Y axis.</param>
/// <returns>A new Vector3 with a randomized Y axis.</returns>
public static ref Vector3 RandomizeY(ref this Vector3 v, Vector2 yRange)
{
v.y = Random.Range(yRange.x, yRange.y);
return ref v;
}
/// <summary>
/// Randomize the Z axis of this Vector3 within a specified range.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="zRange">Range for the Z axis.</param>
/// <returns>A new Vector3 with a randomized Z axis.</returns>
public static ref Vector3 RandomizeZ(ref this Vector3 v, Vector2 zRange)
{
v.z = Random.Range(zRange.x, zRange.y);
return ref v;
}
#endregion
#region Invert
/// <summary>
/// Invert the X, Y, and Z axes of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <returns>The Vector3 with inverted axes.</returns>
public static ref Vector3 Invert(ref this Vector3 v) => ref v.InvertX().InvertY().InvertZ();
/// <summary>
/// Invert the X axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <returns>The Vector3 with the inverted X axis.</returns>
public static ref Vector3 InvertX(ref this Vector3 v)
{
v.x = -v.x;
return ref v;
}
/// <summary>
/// Invert the Y axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <returns>The Vector3 with the inverted Y axis.</returns>
public static ref Vector3 InvertY(ref this Vector3 v)
{
v.y = -v.y;
return ref v;
}
/// <summary>
/// Invert the Z axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <returns>The Vector3 with the inverted Z axis.</returns>
public static ref Vector3 InvertZ(ref this Vector3 v)
{
v.z = -v.z;
return ref v;
}
#endregion
#region Midpoint
/// <summary>
/// Calculate the midpoint between two Vector3.
/// </summary>
/// <param name="v">The first Vector3.</param>
/// <param name="f">The second Vector3.</param>
/// <returns>The midpoint Vector3.</returns>
public static Vector3 Midpoint(this Vector3 v, Vector3 f)
{
return v.Add(f) / 2;
}
/// <summary>
/// Calculate the weighted average of two Vector3 based on specified weight.
/// </summary>
/// <param name="v">The first Vector3.</param>
/// <param name="f">The second Vector3.</param>
/// <param name="weight">Weight for the first Vector3. Should be in the range [0, 100], it's a percentage.</param>
/// <returns>The weighted average Vector3.</returns>
public static Vector3 WeightedAverage(this Vector3 v, Vector3 f, float weight)
{
const float totalWeight = 100f;
float weightF = totalWeight - weight;
Vector3 weightedV = v * weight;
Vector3 weightedF = f * weightF;
return weightedV.Add(weightedF) / totalWeight;
}
#endregion
#region IsWithinRange
/// <summary>
/// Checks if this Vector3 is within the specified range of another Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The target Vector3.</param>
/// <param name="range">The range within which the f Vector3 is considered "within."</param>
/// <returns>True if the f Vector3 is within the specified range; otherwise, false.</returns>
public static bool IsWithinRange(this Vector3 v, Vector3 f, float range)
{
float distance = Vector3.Distance(v, f);
return distance <= range;
}
/// <summary>
/// Checks if the X Axis of this Vector3 is within the specified range of the X Axis of another Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The f Vector3 to compare with.</param>
/// <param name="range">The range within which the X Axis are considered "within."</param>
/// <returns>True if the X Axis are within the specified range; otherwise, false.</returns>
public static bool IsXWithinRange(this Vector3 v, Vector3 f, float range)
{
return Mathf.Abs(v.x - f.x) <= range;
}
/// <summary>
/// Checks if the Y Axis of this Vector3 is within the specified range of the Y Axis of another Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The f Vector3 to compare with.</param>
/// <param name="range">The range within which the Y Axis are considered "within."</param>
/// <returns>True if the Y Axis are within the specified range; otherwise, false.</returns>
public static bool IsYWithinRange(this Vector3 v, Vector3 f, float range)
{
return Mathf.Abs(v.y - f.y) <= range;
}
/// <summary>
/// Checks if the Z Axis of this Vector3 is within the specified range of the Z Axis of another Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="f">The f Vector3 to compare with.</param>
/// <param name="range">The range within which the Z Axis are considered "within."</param>
/// <returns>True if the Z Axis are within the specified range; otherwise, false.</returns>
public static bool IsZWithinRange(this Vector3 v, Vector3 f, float range)
{
return Mathf.Abs(v.z - f.z) <= range;
}
#endregion
#endregion
#region Vector 2
#region Change Axis
/// <summary>
/// Change the X vector of this Vector2 in place.
/// </summary>
/// <param name="v">This vector2.</param>
/// <param name="x">Target value for the X vector.</param>
public static ref Vector2 SetX(ref this Vector2 v, float x)
{
v.x = x;
return ref v;
}
/// <summary>
/// Change the Y vector of this Vector2.
/// </summary>
/// <param name="v">This vector2.</param>
/// <param name="y">Target value for the Y vector.</param>
public static ref Vector2 SetY(ref this Vector2 v, float y)
{
v.y = y;
return ref v;
}
#endregion
#region Add
/// <summary>
/// Add the Axis of two Vector2 into one.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to add.</param>
/// <returns>A new Vector2 with combined Axis.</returns>
public static ref Vector2 Add(ref this Vector2 v, Vector2 f)
{
v += f;
return ref v;
}
/// <summary>
/// Add the specified values to the Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to add to the X Axis.</param>
/// <param name="y">The value to add to the Y Axis.</param>
/// <returns>A new Vector2 with updated Axis.</returns>
public static ref Vector2 Add(ref this Vector2 v, float x, float y) => ref v.AddX(x).AddY(y);
/// <summary>
/// Add the X Axis of two Vector2 into one.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to add.</param>
/// <returns>A new Vector2 with combined X Axis.</returns>
public static ref Vector2 AddX(ref this Vector2 v, Vector2 f) => ref v.AddX(f.x);
/// <summary>
/// Add the specified value to the X Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="x">The value to add to the X Axis.</param>
/// <returns>A new Vector2 with updated X Axis.</returns>
public static ref Vector2 AddX(ref this Vector2 v, float x)
{
v.x += x;
return ref v;
}
/// <summary>
/// Add the Y Axis of two Vector2 into one.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to add.</param>
/// <returns>A new Vector2 with combined Y Axis.</returns>
public static ref Vector2 AddY(ref this Vector2 v, Vector2 f) => ref v.AddY(f.y);
/// <summary>
/// Add the specified value to the Y Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="y">The value to add to the Y Axis.</param>
/// <returns>A new Vector2 with updated Y Axis.</returns>
public static ref Vector2 AddY(ref this Vector2 v, float y)
{
v.y += y;
return ref v;
}
#endregion
#region Subtract
/// <summary>
/// Subtract the Axis of the second Vector2 from the first Vector2 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to subtract.</param>
/// <returns>A new Vector2 with subtracted Axis.</returns>
public static ref Vector2 Subtract(ref this Vector2 v, Vector2 f)
{
v -= f;
return ref v;
}
/// <summary>
/// Subtract the specified values from the Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="x">The value to subtract from the X Axis.</param>
/// <param name="y">The value to subtract from the Y Axis.</param>
/// <returns>A new Vector2 with updated Axis.</returns>
public static ref Vector2 Subtract(ref this Vector2 v, float x, float y) => ref v.SubtractX(x).SubtractY(y);
/// <summary>
/// Subtract the X Axis of the second Vector2 from the first Vector2 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to subtract.</param>
/// <returns>A new Vector2 with subtracted X Axis.</returns>
public static ref Vector2 SubtractX(ref this Vector2 v, Vector2 f) => ref v.SubtractX(f.x);
/// <summary>
/// Subtract the specified value from the X Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="x">The value to subtract from the X Axis.</param>
/// <returns>A new Vector2 with updated X Axis.</returns>
public static ref Vector2 SubtractX(ref this Vector2 v, float x)
{
v.x -= x;
return ref v;
}
/// <summary>
/// Subtract the Y Axis of the second Vector2 from the first Vector2 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to subtract.</param>
/// <returns>A new Vector2 with subtracted Y Axis.</returns>
public static ref Vector2 SubtractY(ref this Vector2 v, Vector2 f) => ref v.SubtractY(f.y);
/// <summary>
/// Subtract the specified value from the Y Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="y">The value to subtract from the Y Axis.</param>
/// <returns>A new Vector2 with updated Y Axis.</returns>
public static ref Vector2 SubtractY(ref this Vector2 v, float y)
{
v.y -= y;
return ref v;
}
#endregion
#region Multiply
/// <summary>
/// Multiply the Axis of this Vector2 with the corresponding Axis of another Vector2 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to multiply.</param>
/// <returns>A new Vector2 with multiplied Axis.</returns>
public static ref Vector2 Multiply(ref this Vector2 v, Vector2 f) => ref v.MultiplyX(f.x).MultiplyY(f.y);
/// <summary>
/// Multiply the specified values with the Axis of this Vector3.
/// </summary>
/// <param name="v">This Vector3.</param>
/// <param name="x">The value to multiply with the X Axis.</param>
/// <param name="y">The value to multiply with the Y Axis.</param>
/// <returns>A new Vector2 with updated Axis.</returns>
public static ref Vector2 Multiply(ref this Vector2 v, float x, float y) => ref v.MultiplyX(x).MultiplyY(y);
/// <summary>
/// Multiply the X Axis of this Vector2 with the X Axis of another Vector2 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to multiply.</param>
/// <returns>A new Vector2 with multiplied X Axis.</returns>
public static ref Vector2 MultiplyX(ref this Vector2 v, Vector2 f) => ref v.MultiplyX(f.x);
/// <summary>
/// Multiply the specified value with the X Axis of this Vector2.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="x">The value to multiply with the X Axis.</param>
/// <returns>A new Vector2 with updated X Axis.</returns>
public static ref Vector2 MultiplyX(ref this Vector2 v, float x)
{
v.x *= x;
return ref v;
}
/// <summary>
/// Multiply the Y Axis of this Vector2 with the Y Axis of another Vector2 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to multiply.</param>
/// <returns>A new Vector2 with multiplied Y Axis.</returns>
public static ref Vector2 MultiplyY(ref this Vector2 v, Vector2 f) => ref v.MultiplyY(f.y);
/// <summary>
/// Multiply the specified value with the Y Axis of this Vector2.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="y">The value to multiply with the Y Axis.</param>
/// <returns>A new Vector2 with updated Y Axis.</returns>
public static ref Vector2 MultiplyY(ref this Vector2 v, float y)
{
v.y *= y;
return ref v;
}
#endregion
#region Divide
/// <summary>
/// Divide the Axis of this Vector2 by the corresponding Axis of another Vector2 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to divide by.</param>
/// <returns>A new Vector2 with divided Axis.</returns>
public static ref Vector2 Divide(ref this Vector2 v, Vector2 f) => ref v.DivideX(f.x).DivideY(f.y);
/// <summary>
/// Divide the specified values by the Axis of this Vector2.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="x">The value to divide by the X Axis.</param>
/// <param name="y">The value to divide by the Y Axis.</param>
/// <returns>A new Vector2 with updated Axis.</returns>
public static ref Vector2 Divide(ref this Vector2 v, float x, float y) => ref v.DivideX(x).DivideY(y);
/// <summary>
/// Divide the X Axis of this Vector2 by the X Axis of another Vector3 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to divide by.</param>
/// <returns>A new Vector2 with divided X Axis.</returns>
public static ref Vector2 DivideX(ref this Vector2 v, Vector2 f) => ref v.DivideX(f.x);
/// <summary>
/// Divide the X Axis of this Vector2 by the specified value and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="x">The value to divide by.</param>
/// <returns>A new Vector2 with updated X Axis.</returns>
public static ref Vector2 DivideX(ref this Vector2 v, float x)
{
v.x /= x;
return ref v;
}
/// <summary>
/// Divide the Y Axis of this Vector2 by the Y Axis of another Vector2 and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The Vector2 to divide by.</param>
/// <returns>A new Vector2 with divided Y Axis.</returns>
public static ref Vector2 DivideY(ref this Vector2 v, Vector2 f) => ref v.DivideY(f.y);
/// <summary>
/// Divide the Y Axis of this Vector2 by the specified value and return the result.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="y">The value to divide by.</param>
/// <returns>A new Vector2 with updated Y Axis.</returns>
public static ref Vector2 DivideY(ref this Vector2 v, float y)
{
v.y /= y;
return ref v;
}
#endregion
#region Compare
/// <summary>
/// Compare two Vector2 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The f Vector2 to compare with.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the vectors are approximately equal within the specified margin.</returns>
public static bool Compare(this Vector2 v, Vector2 f, float margin = 0.0001f)
{
return v.CompareX(f.x, margin)
&& v.CompareY(f.y, margin);
}
/// <summary>
/// Compare the X axis of two Vector2 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="f">The f Vector2 to compare with.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>
/// <returns>True if the X axes are approximately equal within the specified margin.</returns>
public static bool CompareX(this Vector2 v, Vector2 f, float margin = 0.0001f) => v.CompareX(f.x, margin);
/// <summary>
/// Compare a float value with the X Axis of this Vector2 with an optional margin for floating-point precision.
/// </summary>
/// <param name="v">This Vector2.</param>
/// <param name="x">The float value to compare with the X Axis.</param>
/// <param name="margin">Optional margin for floating-point precision. Default is 0.</param>