Exemple #1
0
        public static Intersection FindIntersection(this Polygon polygon, IntPoint start, IntPoint end, QuadTree <int> edgeQuadTree = null)
        {
            Intersection bestIntersection = Intersection.None;

            IntPoint segmentDelta = end - start;
            var      edgeIterator = new PolygonEdgeIterator(polygon, 1, edgeQuadTree);

            foreach (var i in edgeIterator.GetTouching(new Quad(start, end)))
            {
                IntPoint edgeStart = polygon[i];

                // if we share a vertex we cannot be crossing the line
                IntPoint edgeEnd = polygon[(i + 1) % polygon.Count];
                if (start == edgeStart || start == edgeEnd || end == edgeStart || end == edgeEnd ||
                    start == polygon[i] || end == polygon[i])
                {
                    bestIntersection = Intersection.Colinear;
                }
                else
                {
                    var result = GetIntersection(start, end, edgeStart, edgeEnd);
                    if (result == Intersection.Intersect)
                    {
                        return(Intersection.Intersect);
                    }
                    else if (result == Intersection.Colinear)
                    {
                        bestIntersection = Intersection.Colinear;
                    }
                }
            }

            return(bestIntersection);
        }
Exemple #2
0
        public static IEnumerable <Tuple <int, IntPoint> > FindCrossingPoints(this Polygon polygon, IntPoint start, IntPoint end, QuadTree <int> edgeQuadTree = null)
        {
            IntPoint segmentDelta  = end - start;
            long     segmentLength = segmentDelta.Length();
            var      edgeIterator  = new PolygonEdgeIterator(polygon, 1, edgeQuadTree);

            foreach (var i in edgeIterator.GetTouching(new Quad(start, end)))
            {
                IntPoint edgeStart    = polygon[i];
                IntPoint edgeEnd      = polygon[(i + 1) % polygon.Count];
                IntPoint intersection = new IntPoint();

                if (OnSegment(edgeStart, start, edgeEnd))
                {
                    yield return(new Tuple <int, IntPoint>(i, start));
                }
                else if (OnSegment(edgeStart, end, edgeEnd))
                {
                    yield return(new Tuple <int, IntPoint>(i, end));
                }
                else if (DoIntersect(start, end, edgeStart, edgeEnd) &&
                         CalcIntersection(start, end, edgeStart, edgeEnd, out intersection))
                {
                    IntPoint pointRelStart = intersection - start;
                    yield return(new Tuple <int, IntPoint>(i, intersection));
                }
            }
        }