forked from LeagueSharp/LeagueSharp.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geometry.cs
1713 lines (1510 loc) · 60.8 KB
/
Geometry.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
namespace LeagueSharp.Common
{
using System;
using System.Collections.Generic;
using System.Linq;
using ClipperLib;
using SharpDX;
using Color = System.Drawing.Color;
/// <summary>
/// Provides methods regarding geometry math.
/// </summary>
public static class Geometry
{
#region Public Methods and Operators
/// <summary>
/// Returns the angle with the vector p2 in degrees;
/// </summary>
/// <param name="p1">The first point.</param>
/// <param name="p2">The second point.</param>
/// <returns></returns>
public static float AngleBetween(this Vector2 p1, Vector2 p2)
{
var theta = p1.Polar() - p2.Polar();
if (theta < 0)
{
theta = theta + 360;
}
if (theta > 180)
{
theta = 360 - theta;
}
return theta;
}
/// <summary>
/// Returns a Vector2 at center of the polygone.
/// </summary>
/// <param name="p">The polygon.</param>
/// <returns></returns>
public static Vector2 CenterOfPolygone(this Polygon p)
{
var cX = 0f;
var cY = 0f;
var pc = p.Points.Count;
foreach (var point in p.Points)
{
cX += point.X;
cY += point.Y;
}
return new Vector2(cX / pc, cY / pc);
}
/// <summary>
/// Returns the two intersection points between two circles.
/// </summary>
/// <param name="center1">The center1.</param>
/// <param name="center2">The center2.</param>
/// <param name="radius1">The radius1.</param>
/// <param name="radius2">The radius2.</param>
/// <returns></returns>
public static Vector2[] CircleCircleIntersection(Vector2 center1, Vector2 center2, float radius1, float radius2)
{
var D = center1.Distance(center2);
//The Circles dont intersect:
if (D > radius1 + radius2 || (D <= Math.Abs(radius1 - radius2)))
{
return new Vector2[] { };
}
var A = (radius1 * radius1 - radius2 * radius2 + D * D) / (2 * D);
var H = (float)Math.Sqrt(radius1 * radius1 - A * A);
var Direction = (center2 - center1).Normalized();
var PA = center1 + A * Direction;
var S1 = PA + H * Direction.Perpendicular();
var S2 = PA - H * Direction.Perpendicular();
return new[] { S1, S2 };
}
/// <summary>
/// Clips the polygons.
/// </summary>
/// <param name="polygons">The polygons.</param>
/// <returns></returns>
public static List<List<IntPoint>> ClipPolygons(List<Polygon> polygons)
{
var subj = new List<List<IntPoint>>(polygons.Count);
var clip = new List<List<IntPoint>>(polygons.Count);
foreach (var polygon in polygons)
{
subj.Add(polygon.ToClipperPath());
clip.Add(polygon.ToClipperPath());
}
var solution = new List<List<IntPoint>>();
var c = new Clipper();
c.AddPaths(subj, PolyType.ptSubject, true);
c.AddPaths(clip, PolyType.ptClip, true);
c.Execute(ClipType.ctUnion, solution, PolyFillType.pftPositive, PolyFillType.pftEvenOdd);
return solution;
}
/// <summary>
/// Checks if the two floats are close to each other.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <param name="eps">The epsilon.</param>
/// <returns></returns>
public static bool Close(float a, float b, float eps)
{
if (Math.Abs(eps) < float.Epsilon)
{
eps = (float)1e-9;
}
return Math.Abs(a - b) <= eps;
}
/// <summary>
/// Returns the closest vector from a list.
/// </summary>
/// <param name="v">The v.</param>
/// <param name="vList">The v list.</param>
/// <returns></returns>
public static Vector2 Closest(this Vector2 v, List<Vector2> vList)
{
var result = new Vector2();
var dist = float.MaxValue;
foreach (var vector in vList)
{
var distance = Vector2.DistanceSquared(v, vector);
if (distance < dist)
{
dist = distance;
result = vector;
}
}
return result;
}
/// <summary>
/// Returns the cross product Z value.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="other">The other.</param>
/// <returns></returns>
public static float CrossProduct(this Vector2 self, Vector2 other)
{
return other.Y * self.X - other.X * self.Y;
}
/// <summary>
/// Converts degrees to radians.
/// </summary>
/// <param name="angle">The angle.</param>
/// <returns></returns>
public static float DegreeToRadian(double angle)
{
return (float)(Math.PI * angle / 180.0);
}
//Obj_AI_Base class extended methods:
/// <summary>
/// Calculates the 2D distance to the unit.
/// </summary>
/// <param name="anotherUnit">Another unit.</param>
/// <param name="squared">if set to <c>true</c> [squared].</param>
/// <returns></returns>
public static float Distance(Obj_AI_Base anotherUnit, bool squared = false)
{
return ObjectManager.Player.Distance(anotherUnit, squared);
}
/// <summary>
/// Calculates the 2D distance to the unit.
/// </summary>
/// <param name="unit">The unit.</param>
/// <param name="anotherUnit">Another unit.</param>
/// <param name="squared">if set to <c>true</c> [squared].</param>
/// <returns></returns>
public static float Distance(this Obj_AI_Base unit, Obj_AI_Base anotherUnit, bool squared = false)
{
return unit.ServerPosition.To2D().Distance(anotherUnit.ServerPosition.To2D(), squared);
}
/// <summary>
/// Calculates the 2D distance to the unit.
/// </summary>
/// <param name="unit">The unit.</param>
/// <param name="anotherUnit">Another unit.</param>
/// <param name="squared">if set to <c>true</c> [squared].</param>
/// <returns></returns>
public static float Distance(this Obj_AI_Base unit, AttackableUnit anotherUnit, bool squared = false)
{
return unit.ServerPosition.To2D().Distance(anotherUnit.Position.To2D(), squared);
}
/// <summary>
/// Calculates the 2D distance to the point.
/// </summary>
/// <param name="unit">The unit.</param>
/// <param name="point">The point.</param>
/// <param name="squared">if set to <c>true</c> [squared].</param>
/// <returns></returns>
public static float Distance(this Obj_AI_Base unit, Vector3 point, bool squared = false)
{
return unit.ServerPosition.To2D().Distance(point.To2D(), squared);
}
/// <summary>
/// Calculates the 2D distance to the point.
/// </summary>
/// <param name="unit">The unit.</param>
/// <param name="point">The point.</param>
/// <param name="squared">if set to <c>true</c> [squared].</param>
/// <returns></returns>
public static float Distance(this Obj_AI_Base unit, Vector2 point, bool squared = false)
{
return unit.ServerPosition.To2D().Distance(point, squared);
}
/// <summary>
/// Returns the 2D distance (XY plane) between two vector.
/// </summary>
/// <param name="v">The v.</param>
/// <param name="other">The other.</param>
/// <param name="squared">if set to <c>true</c> [squared].</param>
/// <returns></returns>
public static float Distance(this Vector3 v, Vector3 other, bool squared = false)
{
return v.To2D().Distance(other, squared);
}
/// <summary>
/// Calculates the distance to the Vector2.
/// </summary>
/// <param name="v">The v.</param>
/// <param name="to">To.</param>
/// <param name="squared">if set to <c>true</c> gets the distance squared.</param>
/// <returns></returns>
public static float Distance(this Vector2 v, Vector2 to, bool squared = false)
{
return squared ? Vector2.DistanceSquared(v, to) : Vector2.Distance(v, to);
}
/// <summary>
/// Calculates the distance to the Vector3.
/// </summary>
/// <param name="v">The v.</param>
/// <param name="to">To.</param>
/// <param name="squared">if set to <c>true</c> gets the distance squared.</param>
/// <returns></returns>
public static float Distance(this Vector2 v, Vector3 to, bool squared = false)
{
return v.Distance(to.To2D(), squared);
}
/// <summary>
/// Calculates the distance to the unit.
/// </summary>
/// <param name="v">The v.</param>
/// <param name="to">To.</param>
/// <param name="squared">if set to <c>true</c> gets the distance squared.</param>
/// <returns></returns>
public static float Distance(this Vector2 v, Obj_AI_Base to, bool squared = false)
{
return v.Distance(to.ServerPosition.To2D(), squared);
}
/// <summary>
/// Returns the distance to the line segment.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="segmentStart">The segment start.</param>
/// <param name="segmentEnd">The segment end.</param>
/// <param name="onlyIfOnSegment">if set to <c>true</c> [only if on segment].</param>
/// <param name="squared">if set to <c>true</c> [squared].</param>
/// <returns></returns>
public static float Distance(
this Vector2 point,
Vector2 segmentStart,
Vector2 segmentEnd,
bool onlyIfOnSegment = false,
bool squared = false)
{
var objects = point.ProjectOn(segmentStart, segmentEnd);
if (objects.IsOnSegment || onlyIfOnSegment == false)
{
return squared
? Vector2.DistanceSquared(objects.SegmentPoint, point)
: Vector2.Distance(objects.SegmentPoint, point);
}
return float.MaxValue;
}
/// <summary>
/// Calculates the 3D distance to the unit.
/// </summary>
/// <param name="unit">The unit.</param>
/// <param name="anotherUnit">Another unit.</param>
/// <param name="squared">if set to <c>true</c> [squared].</param>
/// <returns></returns>
public static float Distance3D(this Obj_AI_Base unit, Obj_AI_Base anotherUnit, bool squared = false)
{
return squared
? Vector3.DistanceSquared(unit.Position, anotherUnit.Position)
: Vector3.Distance(unit.Position, anotherUnit.Position);
}
/// <summary>
/// Extends the vector.
/// </summary>
/// <param name="v">The vector.</param>
/// <param name="to">The vector to extend to</param>
/// <param name="distance">The distance to extend.</param>
/// <returns></returns>
public static Vector2 Extend(this Vector2 v, Vector2 to, float distance)
{
return v + distance * (to - v).Normalized();
}
/// <summary>
/// Extends the specified vector.
/// </summary>
/// <param name="v">The vector.</param>
/// <param name="to">The vector to extend to.</param>
/// <param name="distance">The distance.</param>
/// <returns></returns>
public static Vector3 Extend(this Vector3 v, Vector3 to, float distance)
{
return v + distance * (to - v).Normalized();
}
//From: http://social.msdn.microsoft.com/Forums/vstudio/en-US/e5993847-c7a9-46ec-8edc-bfb86bd689e3/help-on-line-segment-intersection-algorithm
/// <summary>
/// Intersects two line segments.
/// </summary>
/// <param name="lineSegment1Start">The line segment1 start.</param>
/// <param name="lineSegment1End">The line segment1 end.</param>
/// <param name="lineSegment2Start">The line segment2 start.</param>
/// <param name="lineSegment2End">The line segment2 end.</param>
/// <returns></returns>
public static IntersectionResult Intersection(
this Vector2 lineSegment1Start,
Vector2 lineSegment1End,
Vector2 lineSegment2Start,
Vector2 lineSegment2End)
{
double deltaACy = lineSegment1Start.Y - lineSegment2Start.Y;
double deltaDCx = lineSegment2End.X - lineSegment2Start.X;
double deltaACx = lineSegment1Start.X - lineSegment2Start.X;
double deltaDCy = lineSegment2End.Y - lineSegment2Start.Y;
double deltaBAx = lineSegment1End.X - lineSegment1Start.X;
double deltaBAy = lineSegment1End.Y - lineSegment1Start.Y;
var denominator = deltaBAx * deltaDCy - deltaBAy * deltaDCx;
var numerator = deltaACy * deltaDCx - deltaACx * deltaDCy;
if (Math.Abs(denominator) < float.Epsilon)
{
if (Math.Abs(numerator) < float.Epsilon)
{
// collinear. Potentially infinite intersection points.
// Check and return one of them.
if (lineSegment1Start.X >= lineSegment2Start.X && lineSegment1Start.X <= lineSegment2End.X)
{
return new IntersectionResult(true, lineSegment1Start);
}
if (lineSegment2Start.X >= lineSegment1Start.X && lineSegment2Start.X <= lineSegment1End.X)
{
return new IntersectionResult(true, lineSegment2Start);
}
return new IntersectionResult();
}
// parallel
return new IntersectionResult();
}
var r = numerator / denominator;
if (r < 0 || r > 1)
{
return new IntersectionResult();
}
var s = (deltaACy * deltaBAx - deltaACx * deltaBAy) / denominator;
if (s < 0 || s > 1)
{
return new IntersectionResult();
}
return new IntersectionResult(
true,
new Vector2((float)(lineSegment1Start.X + r * deltaBAx), (float)(lineSegment1Start.Y + r * deltaBAy)));
}
//Vector2 class extended methods:
/// <summary>
/// Returns true if the vector is valid.
/// </summary>
/// <param name="v">The vector.</param>
/// <returns></returns>
public static bool IsValid(this Vector2 v)
{
return v != Vector2.Zero;
}
/// <summary>
/// Determines whether this instance is valid.
/// </summary>
/// <param name="v">The vector.</param>
/// <returns></returns>
public static bool IsValid(this Vector3 v)
{
return v != Vector3.Zero;
}
/// <summary>
/// Joins all the polygones in the list in one polygone if they interect.
/// </summary>
/// <param name="sList">The polygon list.</param>
/// <returns></returns>
public static List<Polygon> JoinPolygons(this List<Polygon> sList)
{
var p = ClipPolygons(sList);
var tList = new List<List<IntPoint>>();
var c = new Clipper();
c.AddPaths(p, PolyType.ptClip, true);
c.Execute(ClipType.ctUnion, tList, PolyFillType.pftNonZero, PolyFillType.pftNonZero);
return ToPolygons(tList);
}
/// <summary>
/// Joins all the polygones.
/// ClipType: http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/ClipType.htm
/// PolyFillType: http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/PolyFillType.htm
/// </summary>
/// <param name="sList">The s list.</param>
/// <param name="cType">Type of the c.</param>
/// <param name="pType">Type of the p.</param>
/// <param name="pFType1">The p f type1.</param>
/// <param name="pFType2">The p f type2.</param>
/// <returns></returns>
public static List<Polygon> JoinPolygons(
this List<Polygon> sList,
ClipType cType,
PolyType pType = PolyType.ptClip,
PolyFillType pFType1 = PolyFillType.pftNonZero,
PolyFillType pFType2 = PolyFillType.pftNonZero)
{
var p = ClipPolygons(sList);
var tList = new List<List<IntPoint>>();
var c = new Clipper();
c.AddPaths(p, pType, true);
c.Execute(cType, tList, pFType1, pFType2);
return ToPolygons(tList);
}
/// <summary>
/// Moves the polygone to the set position. It dosent rotate the polygone.
/// </summary>
/// <param name="polygon">The polygon.</param>
/// <param name="moveTo">The move to.</param>
/// <returns></returns>
public static Polygon MovePolygone(this Polygon polygon, Vector2 moveTo)
{
var p = new Polygon();
p.Add(moveTo);
var count = polygon.Points.Count;
var startPoint = polygon.Points[0];
for (var i = 1; i < count; i++)
{
var polygonePoint = polygon.Points[i];
p.Add(
new Vector2(
moveTo.X + (polygonePoint.X - startPoint.X),
moveTo.Y + (polygonePoint.Y - startPoint.Y)));
}
return p;
}
/// <summary>
/// Returns the vector normalized.
/// </summary>
/// <param name="v">The vector.</param>
/// <returns></returns>
public static Vector2 Normalized(this Vector2 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Normalizes the specified vector.
/// </summary>
/// <param name="v">The vector.</param>
/// <returns></returns>
public static Vector3 Normalized(this Vector3 v)
{
v.Normalize();
return v;
}
/// <summary>
/// Returns the total distance of a path.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
public static float PathLength(this List<Vector2> path)
{
var distance = 0f;
for (var i = 0; i < path.Count - 1; i++)
{
distance += path[i].Distance(path[i + 1]);
}
return distance;
}
/// <summary>
/// Returns the perpendicular vector.
/// </summary>
/// <param name="v">The v.</param>
/// <returns></returns>
public static Vector2 Perpendicular(this Vector2 v)
{
return new Vector2(-v.Y, v.X);
}
/// <summary>
/// Returns the second perpendicular vector.
/// </summary>
/// <param name="v">The vector.</param>
/// <returns></returns>
public static Vector2 Perpendicular2(this Vector2 v)
{
return new Vector2(v.Y, -v.X);
}
/// <summary>
/// Returns the polar for vector angle (in Degrees).
/// </summary>
/// <param name="v1">The vector.</param>
/// <returns></returns>
public static float Polar(this Vector2 v1)
{
if (Close(v1.X, 0, 0))
{
if (v1.Y > 0)
{
return 90;
}
return v1.Y < 0 ? 270 : 0;
}
var theta = RadianToDegree(Math.Atan((v1.Y) / v1.X));
if (v1.X < 0)
{
theta = theta + 180;
}
if (theta < 0)
{
theta = theta + 360;
}
return theta;
}
/// <summary>
/// Returns the position where the vector will be after t(time) with s(speed) and delay.
/// </summary>
/// <param name="self">The self.</param>
/// <param name="t">The time.</param>
/// <param name="s">The speed.</param>
/// <param name="delay">The delay.</param>
/// <returns></returns>
public static Vector2 PositionAfter(this List<Vector2> self, int t, int s, int delay = 0)
{
var distance = Math.Max(0, t - delay) * s / 1000;
for (var i = 0; i <= self.Count - 2; i++)
{
var from = self[i];
var to = self[i + 1];
var d = (int)to.Distance(from);
if (d > distance)
{
return from + distance * (to - from).Normalized();
}
distance -= d;
}
return self[self.Count - 1];
}
/// <summary>
/// Returns the projection of the Vector2 on the segment.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="segmentStart">The segment start.</param>
/// <param name="segmentEnd">The segment end.</param>
/// <returns></returns>
public static ProjectionInfo ProjectOn(this Vector2 point, Vector2 segmentStart, Vector2 segmentEnd)
{
var cx = point.X;
var cy = point.Y;
var ax = segmentStart.X;
var ay = segmentStart.Y;
var bx = segmentEnd.X;
var by = segmentEnd.Y;
var rL = ((cx - ax) * (bx - ax) + (cy - ay) * (by - ay))
/ ((float)Math.Pow(bx - ax, 2) + (float)Math.Pow(by - ay, 2));
var pointLine = new Vector2(ax + rL * (bx - ax), ay + rL * (by - ay));
float rS;
if (rL < 0)
{
rS = 0;
}
else if (rL > 1)
{
rS = 1;
}
else
{
rS = rL;
}
var isOnSegment = rS.CompareTo(rL) == 0;
var pointSegment = isOnSegment ? pointLine : new Vector2(ax + rS * (bx - ax), ay + rS * (@by - ay));
return new ProjectionInfo(isOnSegment, pointSegment, pointLine);
}
/// <summary>
/// Converts radians to degrees.
/// </summary>
/// <param name="angle">The angle.</param>
/// <returns></returns>
public static float RadianToDegree(double angle)
{
return (float)(angle * (180.0 / Math.PI));
}
/// <summary>
/// Rotates the vector around the set position.
/// Angle is in radians.
/// </summary>
/// <param name="rotated">The rotated.</param>
/// <param name="around">The around.</param>
/// <param name="angle">The angle.</param>
/// <returns></returns>
public static Vector2 RotateAroundPoint(this Vector2 rotated, Vector2 around, float angle)
{
var sin = Math.Sin(angle);
var cos = Math.Cos(angle);
var x = cos * (rotated.X - around.X) - sin * (rotated.Y - around.Y) + around.X;
var y = sin * (rotated.X - around.X) + cos * (rotated.Y - around.Y) + around.Y;
return new Vector2((float)x, (float)y);
}
/// <summary>
/// Rotates the vector a set angle (angle in radians).
/// </summary>
/// <param name="v">The vector.</param>
/// <param name="angle">The angle.</param>
/// <returns></returns>
public static Vector2 Rotated(this Vector2 v, float angle)
{
var c = Math.Cos(angle);
var s = Math.Sin(angle);
return new Vector2((float)(v.X * c - v.Y * s), (float)(v.Y * c + v.X * s));
}
/// <summary>
/// Rotates the polygon around the set position.
/// Angle is in radians.
/// </summary>
/// <param name="polygon">The polygon.</param>
/// <param name="around">The around.</param>
/// <param name="angle">The angle.</param>
/// <returns></returns>
public static Polygon RotatePolygon(this Polygon polygon, Vector2 around, float angle)
{
var p = new Polygon();
foreach (var polygonePoint in polygon.Points.Select(poinit => RotateAroundPoint(poinit, around, angle)))
{
p.Add(polygonePoint);
}
return p;
}
/// <summary>
/// Rotates the polygon around to the set direction.
/// </summary>
/// <param name="polygon">The polygon.</param>
/// <param name="around">The around.</param>
/// <param name="direction">The direction.</param>
/// <returns></returns>
public static Polygon RotatePolygon(this Polygon polygon, Vector2 around, Vector2 direction)
{
var deltaX = around.X - direction.X;
var deltaY = around.Y - direction.Y;
var angle = (float)Math.Atan2(deltaY, deltaX);
return RotatePolygon(polygon, around, angle - DegreeToRadian(90));
}
/// <summary>
/// Sets the z.
/// </summary>
/// <param name="v">The v.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static Vector3 SetZ(this Vector3 v, float? value = null)
{
if (value == null)
{
v.Z = Game.CursorPos.Z;
}
else
{
v.Z = (float)value;
}
return v;
}
/// <summary>
/// Shortens the specified vector.
/// </summary>
/// <param name="v">The vector.</param>
/// <param name="to">The vector to shorten from.</param>
/// <param name="distance">The distance.</param>
/// <returns></returns>
public static Vector2 Shorten(this Vector2 v, Vector2 to, float distance)
{
return v - distance * (to - v).Normalized();
}
/// <summary>
/// Shortens the specified vector.
/// </summary>
/// <param name="v">The vector.</param>
/// <param name="to">The vector to shorten from.</param>
/// <param name="distance">The distance.</param>
/// <returns></returns>
public static Vector3 Shorten(this Vector3 v, Vector3 to, float distance)
{
return v - distance * (to - v).Normalized();
}
/// <summary>
/// Switches the Y and Z.
/// </summary>
/// <param name="v">The vector.</param>
/// <returns></returns>
public static Vector3 SwitchYZ(this Vector3 v)
{
return new Vector3(v.X, v.Z, v.Y);
}
//Vector3 class extended methods:
/// <summary>
/// Converts a Vector3 to Vector2
/// </summary>
/// <param name="v">The v.</param>
/// <returns></returns>
public static Vector2 To2D(this Vector3 v)
{
return new Vector2(v.X, v.Y);
}
/// <summary>
/// Converts a 3D path to 2D
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
public static List<Vector2> To2D(this List<Vector3> path)
{
return path.Select(point => point.To2D()).ToList();
}
/// <summary>
/// Converts the Vector2 to Vector3. (Z = Player.ServerPosition.Z)
/// </summary>
/// <param name="v">The vector.</param>
/// <returns></returns>
public static Vector3 To3D(this Vector2 v)
{
return new Vector3(v.X, v.Y, ObjectManager.Player.ServerPosition.Z);
}
/// <summary>
/// Converts the Vector2 to Vector3. (Z = NavMesh.GetHeightForPosition)
/// </summary>
/// <param name="v">The vector.</param>
/// <returns></returns>
public static Vector3 To3D2(this Vector2 v)
{
return new Vector3(v.X, v.Y, NavMesh.GetHeightForPosition(v.X, v.Y));
}
/// <summary>
/// Converts a list of <see cref="IntPoint" /> to a polygon.
/// </summary>
/// <param name="v">The int points.</param>
/// <returns></returns>
public static Polygon ToPolygon(this List<IntPoint> v)
{
var polygon = new Polygon();
foreach (var point in v)
{
polygon.Add(new Vector2(point.X, point.Y));
}
return polygon;
}
/// <summary>
/// Converts a list of list points to a polygon.
/// </summary>
/// <param name="v">The v.</param>
/// <returns></returns>
public static List<Polygon> ToPolygons(this List<List<IntPoint>> v)
{
return v.Select(path => path.ToPolygon()).ToList();
}
/// <summary>
/// Gets the vectors movement collision.
/// </summary>
/// <param name="startPoint1">The start point1.</param>
/// <param name="endPoint1">The end point1.</param>
/// <param name="v1">The v1.</param>
/// <param name="startPoint2">The start point2.</param>
/// <param name="v2">The v2.</param>
/// <param name="delay">The delay.</param>
/// <returns></returns>
public static Object[] VectorMovementCollision(
Vector2 startPoint1,
Vector2 endPoint1,
float v1,
Vector2 startPoint2,
float v2,
float delay = 0f)
{
float sP1x = startPoint1.X,
sP1y = startPoint1.Y,
eP1x = endPoint1.X,
eP1y = endPoint1.Y,
sP2x = startPoint2.X,
sP2y = startPoint2.Y;
float d = eP1x - sP1x, e = eP1y - sP1y;
float dist = (float)Math.Sqrt(d * d + e * e), t1 = float.NaN;
float S = Math.Abs(dist) > float.Epsilon ? v1 * d / dist : 0,
K = (Math.Abs(dist) > float.Epsilon) ? v1 * e / dist : 0f;
float r = sP2x - sP1x, j = sP2y - sP1y;
var c = r * r + j * j;
if (dist > 0f)
{
if (Math.Abs(v1 - float.MaxValue) < float.Epsilon)
{
var t = dist / v1;
t1 = v2 * t >= 0f ? t : float.NaN;
}
else if (Math.Abs(v2 - float.MaxValue) < float.Epsilon)
{
t1 = 0f;
}
else
{
float a = S * S + K * K - v2 * v2, b = -r * S - j * K;
if (Math.Abs(a) < float.Epsilon)
{
if (Math.Abs(b) < float.Epsilon)
{
t1 = (Math.Abs(c) < float.Epsilon) ? 0f : float.NaN;
}
else
{
var t = -c / (2 * b);
t1 = (v2 * t >= 0f) ? t : float.NaN;
}
}
else
{
var sqr = b * b - a * c;
if (sqr >= 0)
{
var nom = (float)Math.Sqrt(sqr);
var t = (-nom - b) / a;
t1 = v2 * t >= 0f ? t : float.NaN;
t = (nom - b) / a;
var t2 = (v2 * t >= 0f) ? t : float.NaN;
if (!float.IsNaN(t2) && !float.IsNaN(t1))
{
if (t1 >= delay && t2 >= delay)
{
t1 = Math.Min(t1, t2);
}
else if (t2 >= delay)
{
t1 = t2;
}
}
}
}
}
}
else if (Math.Abs(dist) < float.Epsilon)
{
t1 = 0f;
}
return new Object[] { t1, (!float.IsNaN(t1)) ? new Vector2(sP1x + S * t1, sP1y + K * t1) : new Vector2() };
}
#endregion
/// <summary>
/// Represents an intersection result.
/// </summary>
public struct IntersectionResult
{
#region Fields
/// <summary>
/// If they intersect.
/// </summary>
public bool Intersects;
/// <summary>
/// The point
/// </summary>
public Vector2 Point;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="IntersectionResult" /> struct.
/// </summary>
/// <param name="Intersects">if set to <c>true</c>, they insersect.</param>
/// <param name="Point">The point.</param>
public IntersectionResult(bool Intersects = false, Vector2 Point = new Vector2())
{
this.Intersects = Intersects;
this.Point = Point;
}
#endregion
}
/// <summary>
/// Represents the projection information.
/// </summary>
public struct ProjectionInfo
{
#region Fields
/// <summary>
/// The is on segment
/// </summary>
public bool IsOnSegment;
/// <summary>
/// The line point
/// </summary>
public Vector2 LinePoint;
/// <summary>
/// The segment point
/// </summary>
public Vector2 SegmentPoint;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="ProjectionInfo" /> struct.
/// </summary>