forked from LeagueSharp/LeagueSharp.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometry.cs
More file actions
1023 lines (894 loc) · 35.4 KB
/
Geometry.cs
File metadata and controls
1023 lines (894 loc) · 35.4 KB
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
#region LICENSE
/*
Copyright 2014 - 2015 LeagueSharp
Geometry.cs is part of LeagueSharp.Common.
LeagueSharp.Common is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LeagueSharp.Common is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LeagueSharp.Common. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
#region
using System;
using System.Collections.Generic;
using System.Linq;
using ClipperLib;
using SharpDX;
using Color = System.Drawing.Color;
#endregion
namespace LeagueSharp.Common
{
public static class Geometry
{
//Obj_AI_Base class extended methods:
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>
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>
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>
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>
public static float Distance(this Obj_AI_Base unit, Vector2 point, bool squared = false)
{
return unit.ServerPosition.To2D().Distance(point, squared);
}
/// <summary>
/// Calculates the 3D distance to the unit.
/// </summary>
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);
}
//Vector3 class extended methods:
/// <summary>
/// Converts a Vector3 to Vector2
/// </summary>
public static Vector2 To2D(this Vector3 v)
{
return new Vector2(v.X, v.Y);
}
/// <summary>
/// Returns the 2D distance (XY plane) between two vector.
/// </summary>
public static float Distance(this Vector3 v, Vector3 other, bool squared = false)
{
return v.To2D().Distance(other, squared);
}
//Vector2 class extended methods:
/// <summary>
/// Returns true if the vector is valid.
/// </summary>
public static bool IsValid(this Vector2 v)
{
return v != Vector2.Zero;
}
public static bool IsValid(this Vector3 v)
{
return v != Vector3.Zero;
}
/// <summary>
/// Converts the Vector2 to Vector3. (Z = Player.ServerPosition.Z)
/// </summary>
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>
public static Vector3 To3D2(this Vector2 v)
{
return new Vector3(v.X, v.Y, NavMesh.GetHeightForPosition(v.X, v.Y));
}
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>
/// Calculates the distance to the Vector2.
/// </summary>
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>
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>
public static float Distance(this Vector2 v, Obj_AI_Base to, bool squared = false)
{
return v.Distance(to.ServerPosition.To2D(), squared);
}
/// <summary>
/// Retursn the distance to the line segment.
/// </summary>
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>
/// Returns the vector normalized.
/// </summary>
public static Vector2 Normalized(this Vector2 v)
{
v.Normalize();
return v;
}
public static Vector3 Normalized(this Vector3 v)
{
v.Normalize();
return v;
}
public static Vector2 Extend(this Vector2 v, Vector2 to, float distance)
{
return v + distance * (to - v).Normalized();
}
public static Vector3 Extend(this Vector3 v, Vector3 to, float distance)
{
return v + distance * (to - v).Normalized();
}
public static Vector2 Shorten(this Vector2 v, Vector2 to, float distance)
{
return v - distance * (to - v).Normalized();
}
public static Vector3 Shorten(this Vector3 v, Vector3 to, float distance)
{
return v - distance * (to - v).Normalized();
}
public static Vector3 SwitchYZ(this Vector3 v)
{
return new Vector3(v.X, v.Z, v.Y);
}
/// <summary>
/// Returns the perpendicular vector.
/// </summary>
public static Vector2 Perpendicular(this Vector2 v)
{
return new Vector2(-v.Y, v.X);
}
/// <summary>
/// Returns the second perpendicular vector.
/// </summary>
public static Vector2 Perpendicular2(this Vector2 v)
{
return new Vector2(v.Y, -v.X);
}
/// <summary>
/// Rotates the vector a set angle (angle in radians).
/// </summary>
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>
/// Returns the cross product Z value.
/// </summary>
public static float CrossProduct(this Vector2 self, Vector2 other)
{
return other.Y * self.X - other.X * self.Y;
}
public static float RadianToDegree(double angle)
{
return (float) (angle * (180.0 / Math.PI));
}
public static float DegreeToRadian(double angle)
{
return (float) (Math.PI * angle / 180.0);
}
/// <summary>
/// Returns the polar for vector angle (in Degrees).
/// </summary>
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 angle with the vector p2 in degrees;
/// </summary>
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 the closest vector from a list.
/// </summary>
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 projection of the Vector2 on the segment.
/// </summary>
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);
}
//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>
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)));
}
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() };
}
/// <summary>
/// Returns the total distance of a path.
/// </summary>
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>
/// Converts a 3D path to 2D
/// </summary>
public static List<Vector2> To2D(this List<Vector3> path)
{
return path.Select(point => point.To2D()).ToList();
}
/// <summary>
/// Returns the two intersection points between two circles.
/// </summary>
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 };
}
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>
/// Rotates the vector around the set position.
/// Angle is in radians.
/// </summary>
public static Vector2 RotateAroundPoint(this Vector2 rotated, Vector2 around, float angle)
{
var sin = Math.Sin(angle);
var cos = Math.Cos(angle);
var x = ((rotated.X - around.X) * cos) - ((around.Y - rotated.Y) * sin) + around.X;
var y = ((around.Y - rotated.Y) * cos) + ((rotated.X - around.X) * sin) + around.Y;
return new Vector2((float) x, (float) y);
}
/// <summary>
/// Rotates the polygon around the set position.
/// Angle is in radians.
/// </summary>
public static Polygon RotatePolygon(this Polygon polygon, Vector2 around, float angle)
{
var p = new Polygon();
foreach (var polygonePoint in polygon.Points.Select(poinit => poinit.RotateAroundPoint(around, angle)))
{
p.Add(polygonePoint);
}
return p;
}
/// <summary>
/// Rotates the polygon around to the set direction.
/// </summary>
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>
/// Moves the polygone to the set position. It dosent rotate the polygone.
/// </summary>
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 a Vector2 at center of the polygone.
/// </summary>
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>
/// Joins all the polygones in the list in one polygone if they interect.
/// </summary>
public static List<Polygon> JoinPolygons(this List<Polygon> sList)
{
var p = ClipPolygons(sList);
List<List<IntPoint>> tList = new List<List<IntPoint>>();
Clipper 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>
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);
List<List<IntPoint>> tList = new List<List<IntPoint>>();
Clipper c = new Clipper();
c.AddPaths(p, pType, true);
c.Execute(cType, tList, pFType1, pFType2);
return ToPolygons(tList);
}
public static List<Polygon> ToPolygons(this List<List<IntPoint>> v)
{
return v.Select(path => path.ToPolygon()).ToList();
}
/// <summary>
/// Returns the position where the vector will be after t(time) with s(speed) and delay.
/// </summary>
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];
}
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;
}
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;
}
public struct IntersectionResult
{
public bool Intersects;
public Vector2 Point;
public IntersectionResult(bool Intersects = false, Vector2 Point = new Vector2())
{
this.Intersects = Intersects;
this.Point = Point;
}
}
public struct ProjectionInfo
{
public bool IsOnSegment;
public Vector2 LinePoint;
public Vector2 SegmentPoint;
public ProjectionInfo(bool isOnSegment, Vector2 segmentPoint, Vector2 linePoint)
{
IsOnSegment = isOnSegment;
SegmentPoint = segmentPoint;
LinePoint = linePoint;
}
}
public class Polygon
{
public List<Vector2> Points = new List<Vector2>();
public void Add(Vector2 point)
{
Points.Add(point);
}
public void Add(Vector3 point)
{
Points.Add(point.To2D());
}
public void Add(Polygon polygon)
{
foreach (var point in polygon.Points)
{
Points.Add(point);
}
}
public List<IntPoint> ToClipperPath()
{
var result = new List<IntPoint>(Points.Count);
result.AddRange(Points.Select(point => new IntPoint(point.X, point.Y)));
return result;
}
public virtual void Draw(Color color, int width = 1)
{
for (var i = 0; i <= Points.Count - 1; i++)
{
var nextIndex = (Points.Count - 1 == i) ? 0 : (i + 1);
var from = Drawing.WorldToScreen(Points[i].To3D());
var to = Drawing.WorldToScreen(Points[nextIndex].To3D());
Drawing.DrawLine(from[0], from[1], to[0], to[1], width, color);
}
}
public bool IsInside(Vector2 point)
{
return !IsOutside(point);
}
public bool IsInside(Vector3 point)
{
return !IsOutside(point.To2D());
}
public bool IsInside(GameObject point)
{
return !IsOutside(point.Position.To2D());
}
public bool IsOutside(Vector2 point)
{
var p = new IntPoint(point.X, point.Y);
return Clipper.PointInPolygon(p, ToClipperPath()) != 1;
}
public class Arc : Polygon
{
public float Angle;
public Vector2 EndPos;
public float Radius;
public Vector2 StartPos;
private readonly int _quality;
public Arc(Vector3 start, Vector3 direction, float angle, float radius, int quality = 20)
: this(start.To2D(), direction.To2D(), angle, radius, quality) {}
public Arc(Vector2 start, Vector2 direction, float angle, float radius, int quality = 20)
{
StartPos = start;
EndPos = (direction - start).Normalized();
Angle = angle;
Radius = radius;
_quality = quality;
UpdatePolygon();
}
public void UpdatePolygon(int offset = 0)
{
Points.Clear();
var outRadius = (Radius + offset) / (float)Math.Cos(2 * Math.PI / _quality);
var side1 = EndPos.Rotated(-Angle * 0.5f);
for (var i = 0; i <= _quality; i++)
{
var cDirection = side1.Rotated(i * Angle / _quality).Normalized();
Points.Add(
new Vector2(StartPos.X + outRadius * cDirection.X, StartPos.Y + outRadius * cDirection.Y));
}
}
}
public class Line : Polygon
{
public Vector2 LineStart;
public Vector2 LineEnd;
public float Length
{
get { return LineStart.Distance(LineEnd); }
set { LineEnd = (LineEnd - LineStart).Normalized() * value + LineStart; }
}
public Line(Vector3 start, Vector3 end, float length = -1) : this(start.To2D(), end.To2D(), length) {}
public Line(Vector2 start, Vector2 end, float length = -1)
{
LineStart = start;
LineEnd = end;
if (length > 0)
{
Length = length;
}
UpdatePolygon();
}
public void UpdatePolygon()
{
Points.Clear();
Points.Add(LineStart);
Points.Add(LineEnd);
}
}
public class Circle : Polygon
{
public Vector2 Center;
public float Radius;
private readonly int _quality;
public Circle(Vector3 center, float radius, int quality = 20) : this(center.To2D(), radius, quality) {}
public Circle(Vector2 center, float radius, int quality = 20)
{
Center = center;
Radius = radius;
_quality = quality;
UpdatePolygon();
}
public void UpdatePolygon(int offset = 0, float overrideWidth = -1)
{
Points.Clear();
var outRadius = (overrideWidth > 0
? overrideWidth
: (offset + Radius) / (float)Math.Cos(2 * Math.PI / _quality));
for (var i = 1; i <= _quality; i++)
{
var angle = i * 2 * Math.PI / _quality;
var point = new Vector2(
Center.X + outRadius * (float)Math.Cos(angle), Center.Y + outRadius * (float)Math.Sin(angle));
Points.Add(point);
}
}
}
public class Rectangle : Polygon
{
public Vector2 Direction { get { return (End - Start).Normalized(); } }
public Vector2 Perpendicular { get { return Direction.Perpendicular(); } }
public Vector2 End;
public Vector2 Start;
public float Width;
public Rectangle(Vector3 start, Vector3 end, float width) : this(start.To2D(), end.To2D(), width) {}
public Rectangle(Vector2 start, Vector2 end, float width)
{
Start = start;
End = end;
Width = width;
UpdatePolygon();
}
public void UpdatePolygon(int offset = 0, float overrideWidth = -1)
{
Points.Clear();
Points.Add(
Start + (overrideWidth > 0 ? overrideWidth : Width + offset) * Perpendicular - offset * Direction);
Points.Add(
Start - (overrideWidth > 0 ? overrideWidth : Width + offset) * Perpendicular - offset * Direction);
Points.Add(
End - (overrideWidth > 0 ? overrideWidth : Width + offset) * Perpendicular + offset * Direction);
Points.Add(
End + (overrideWidth > 0 ? overrideWidth : Width + offset) * Perpendicular + offset * Direction);
}
}
public class Ring : Polygon
{
public Vector2 Center;
public float InnerRadius;
public float OuterRadius;
private readonly int _quality;
public Ring(Vector3 center, float innerRadius, float outerRadius, int quality = 20)
: this(center.To2D(), innerRadius, outerRadius, quality) {}
public Ring(Vector2 center, float innerRadius, float outerRadius, int quality = 20)
{
Center = center;
InnerRadius = innerRadius;
OuterRadius = outerRadius;
_quality = quality;
UpdatePolygon();
}
public void UpdatePolygon(int offset = 0)
{
Points.Clear();
var outRadius = (offset + InnerRadius + OuterRadius) / (float)Math.Cos(2 * Math.PI / _quality);
var innerRadius = InnerRadius - OuterRadius - offset;
for (var i = 0; i <= _quality; i++)
{
var angle = i * 2 * Math.PI / _quality;
var point = new Vector2(
Center.X - outRadius * (float)Math.Cos(angle), Center.Y - outRadius * (float)Math.Sin(angle));
Points.Add(point);
}
for (var i = 0; i <= _quality; i++)
{
var angle = i * 2 * Math.PI / _quality;
var point = new Vector2(
Center.X + innerRadius * (float)Math.Cos(angle),
Center.Y - innerRadius * (float)Math.Sin(angle));
Points.Add(point);
}
}
}
public class Sector : Polygon
{
public float Angle;
public Vector2 Center;
public Vector2 Direction;
public float Radius;
private readonly int _quality;
public Sector(Vector3 center, Vector3 direction, float angle, float radius, int quality = 20)
: this(center.To2D(), direction.To2D(), angle, radius, quality) {}
public Sector(Vector2 center, Vector2 direction, float angle, float radius, int quality = 20)
{