4 using System.Collections.Generic;
14 private const float largeValue = 10000;
15 private const float smallValue = -largeValue;
18 public static readonly Vector2 InvalidPoint =
new Vector2(
float.NegativeInfinity,
float.NegativeInfinity);
25 var edges =
new Edge[points.Count];
27 for (
int pointIndex = 0; pointIndex < points.Count; pointIndex++)
29 var pointA = points[pointIndex];
30 var pointB = points[(pointIndex + 1) % points.Count];
31 edges[pointIndex] =
new Edge(pointA.x, pointA.z, pointB.x, pointB.z);
42 return !
float.IsNegativeInfinity(point.x) && !
float.IsNegativeInfinity(point.y);
51 Vector2 farPoint = point;
52 farPoint.x = largeValue;
53 var rightEdge =
new Edge(point, farPoint);
54 int intersections = 0;
55 foreach (var edge
in edges)
57 if (IsValidPoint(GetIntersection(edge, rightEdge)))
62 return (intersections & 1) == 1;
70 float s1_x = edge1.
Bx - edge1.
Ax;
71 float s1_y = edge1.
By - edge1.
Ay;
72 float s2_x = edge2.
Bx - edge2.
Ax;
73 float s2_y = edge2.
By - edge2.
Ay;
76 s = (-s1_y * (edge1.
Ax - edge2.
Ax) + s1_x * (edge1.
Ay - edge2.
Ay)) / (-s2_x * s1_y + s1_x * s2_y);
77 t = (s2_x * (edge1.
Ay - edge2.
Ay) - s2_y * (edge1.
Ax - edge2.
Ax)) / (-s2_x * s1_y + s1_x * s2_y);
79 if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
82 return new Vector2(edge1.
Ax + (t * s1_x), edge1.
Ay + (t * s1_y));