public static bool IsInside(List <int[]> polygonEdgePoints, int[] testPoint)
        {
            //a imaginary ray line from point to right infinity
            var rayLine = new Line()
            {
                x1 = testPoint[0],
                y1 = testPoint[1],
                x2 = double.MaxValue,
                y2 = testPoint[1]
            };

            var intersectionCount = 0;

            for (int i = 0; i < polygonEdgePoints.Count - 1; i++)
            {
                var edgeLine = new Line()
                {
                    x1 = polygonEdgePoints[i][0],
                    y1 = polygonEdgePoints[i][1],
                    x2 = polygonEdgePoints[i + 1][0],
                    y2 = polygonEdgePoints[i + 1][1]
                };

                if (!LineIntersection.FindIntersection(rayLine, edgeLine).Equals(default(Point)))
                {
                    intersectionCount++;
                }
            }

            //should have odd intersections if point is inside the polygon
            return(intersectionCount % 2 != 0);
        }
Пример #2
0
        public int CompareTo(object that)
        {
            if (Equals(that))
            {
                return(0);
            }

            var thatEvent = that as Event;

            var line1 = Segment;
            var line2 = thatEvent.Segment;

            Point intersectionA;

            if (Type == EventType.Intersection)
            {
                intersectionA = this as Point;
            }
            else
            {
                if (LastSweepLine == Algorithm.SweepLine)
                {
                    intersectionA = LastIntersection;
                }
                else
                {
                    intersectionA    = LineIntersection.FindIntersection(line1, Algorithm.SweepLine, tolerance);
                    LastSweepLine    = Algorithm.SweepLine;
                    LastIntersection = intersectionA;
                }
            }
            Point intersectionB;

            if (Type == EventType.Intersection)
            {
                intersectionB = thatEvent as Point;
            }
            else
            {
                if (thatEvent.LastSweepLine == thatEvent.Algorithm.SweepLine)
                {
                    intersectionB = thatEvent.LastIntersection;
                }
                else
                {
                    intersectionB              = LineIntersection.FindIntersection(line2, thatEvent.Algorithm.SweepLine, tolerance);
                    thatEvent.LastSweepLine    = thatEvent.Algorithm.SweepLine;
                    thatEvent.LastIntersection = intersectionB;
                }
            }
            if (intersectionA != null && intersectionB != null)
            {
                int resulted = intersectionA.Y.CompareTo(intersectionB.Y);
                if (resulted != 0)
                {
                    return(resulted);
                }
            }
            //if Y is same use slope as comparison
            double slope1 = line1.Slope;

            //if Y is same use slope as comparison
            double slope2 = line2.Slope;

            var result = slope1.CompareTo(slope2);

            if (result != 0)
            {
                return(result);
            }

            //if slope is the same use diff of X co-ordinate
            result = line1.Left.X.CompareTo(line2.Left.X);
            if (result != 0)
            {
                return(result);
            }

            //if diff of X co-ordinate is same use diff of Y co-ordinate
            result = line1.Left.Y.CompareTo(line2.Left.Y);

            //at this point this is guaranteed to be not same.
            //since we don't let duplicate lines with input HashSet of lines.
            //see line equals override in Line class.
            return(result);
        }
 internal static Point Intersection(this Line lineA, Line lineB, double tolerance)
 {
     return(LineIntersection.FindIntersection(lineA, lineB, tolerance));
 }
 internal static bool Intersects(this Line lineA, Line lineB, double tolerance)
 {
     return(LineIntersection.FindIntersection(lineA, lineB, tolerance) != null);
 }
 public static Point Intersection(this Line lineA, Line lineB, int precision = 5)
 {
     return(LineIntersection.Find(lineA, lineB, precision));
 }
 public static bool Intersects(this Line lineA, Line lineB, int precision = 5)
 {
     return(LineIntersection.Find(lineA, lineB, precision) != null);
 }
        public int CompareTo(Event thatEvent)
        {
            if (Equals(thatEvent))
            {
                return(0);
            }

            if (!Segment.HasValue || !thatEvent.Segment.HasValue)
            {
                throw new InvalidOperationException();
            }

            var line1 = Segment.Value;
            var line2 = thatEvent.Segment.Value;

            Point intersectionA;

            if (Type == EventType.Intersection)
            {
                intersectionA = Point;
            }
            else
            {
                if (LastSweepLine == Algorithm.SweepLine)
                {
                    intersectionA = LastIntersection;
                }
                else
                {
                    intersectionA    = LineIntersection.FindIntersection(line1, Algorithm.SweepLine, tolerance) ?? default;
                    LastSweepLine    = Algorithm.SweepLine;
                    LastIntersection = intersectionA;
                }
            }

            Point intersectionB;

            if (Type == EventType.Intersection)
            {
                intersectionB = thatEvent.Point;
            }
            else
            {
                if (thatEvent.LastSweepLine == thatEvent.Algorithm.SweepLine)
                {
                    intersectionB = thatEvent.LastIntersection;
                }
                else
                {
                    intersectionB              = LineIntersection.FindIntersection(line2, thatEvent.Algorithm.SweepLine, tolerance) ?? default;
                    thatEvent.LastSweepLine    = thatEvent.Algorithm.SweepLine;
                    thatEvent.LastIntersection = intersectionB;
                }
            }

            var result = intersectionA.Y.CompareTo(intersectionB.Y);

            if (result != 0)
            {
                return(result);
            }

            //if Y is same use slope as comparison
            double slope1 = line1.Slope;

            //if Y is same use slope as comparison
            double slope2 = line2.Slope;

            result = slope1.CompareTo(slope2);
            if (result != 0)
            {
                return(result);
            }

            //if slope is the same use diff of X co-ordinate
            result = line1.Left.X.CompareTo(line2.Left.X);
            if (result != 0)
            {
                return(result);
            }

            //if diff of X co-ordinate is same use diff of Y co-ordinate
            result = line1.Left.Y.CompareTo(line2.Left.Y);

            //at this point this is guaranteed to be not same.
            //since we don't let duplicate lines with input HashSet of lines.
            //see line equals override in Line class.
            return(result);
        }