Пример #1
0
        public static LineSegment2IntersectionResult <double> ComputeIntersection(Point2 <double> p1, Point2 <double> p2, Point2 <double> q1, Point2 <double> q2)
        {
            if (p1 == null || p2 == null || q1 == null || q2 == null)
            {
                return(null);
            }

            //quick rejection
            if (!Coordinate2Utils.EnvelopesIntersect(p1, p2, q1, q2))
            {
                return(new LineSegment2IntersectionResult <double>());
            }
            // for each endpoint, compute which side of the other segment it lies
            // if both endpoints lie on the same side of the other segment, the segments do not intersect
            int Pq1 = Coordinate2Utils.OrientationIndex(p1, p2, q1);
            int Pq2 = Coordinate2Utils.OrientationIndex(p1, p2, q2);

            if ((Pq1 > 0 && Pq2 > 0) || (Pq1 < 0 && Pq2 < 0))
            {
                return(new LineSegment2IntersectionResult <double>());
            }

            int Qp1 = Coordinate2Utils.OrientationIndex(q1, q2, p1);
            int Qp2 = Coordinate2Utils.OrientationIndex(q1, q2, p2);

            if ((Qp1 > 0 && Qp2 > 0) || (Qp1 < 0 && Qp2 < 0))
            {
                return(new LineSegment2IntersectionResult <double>());
            }
            //end quick rejection

            if (Pq1 == 0 && Pq2 == 0 && Qp1 == 0 && Qp2 == 0) //collinear intersection
            {
                bool p1q1p2 = Coordinate2Utils.PointInEnvelope(p1, p2, q1);
                bool p1q2p2 = Coordinate2Utils.PointInEnvelope(p1, p2, q2);
                bool q1p1q2 = Coordinate2Utils.PointInEnvelope(q1, q2, p1);
                bool q1p2q2 = Coordinate2Utils.PointInEnvelope(q1, q2, p2);

                if (p1q1p2 && p1q2p2)
                {
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q1, q2));
                }
                if (q1p1q2 && q1p2q2)
                {
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, p1, p2));
                }
                if (p1q1p2 && q1p1q2)
                {
                    if (q1.Equals(p1) && !p1q2p2 && !q1p2q2)
                    {
                        return(new LineSegment2IntersectionResult <double>(q1));
                    }
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q1, p1));
                }
                if (p1q1p2 && q1p2q2)
                {
                    if (q1.Equals(p2) && !p1q2p2 && !q1p1q2)
                    {
                        return(new LineSegment2IntersectionResult <double>(q1));
                    }
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q1, p2));
                }
                if (p1q2p2 && q1p1q2)
                {
                    if (q2.Equals(p1) && !p1q1p2 && !q1p2q2)
                    {
                        return(new LineSegment2IntersectionResult <double>(q2));
                    }
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q2, p1));
                }
                if (p1q2p2 && q1p2q2)
                {
                    if (q2.Equals(p2) && !p1q1p2 && !q1p1q2)
                    {
                        return(new LineSegment2IntersectionResult <double>(q2));
                    }
                    return(new LineSegment2IntersectionResult <double>(LineIntersectionType.CollinearIntersection, q2, p2));
                }
                return(new LineSegment2IntersectionResult <double>());
            }//end collinear

            // At this point we know that there is a single intersection point (since the lines are not collinear).
            // Check if the intersection is an endpoint. If it is, copy the endpoint as the intersection point. Copying the point rather than computing it
            // ensures the point has the exact value, which is important for robustness. It is sufficient to simply check for an endpoint which is on
            // the other line, since at this point we know that the inputLines must intersect.
            if (Pq1 == 0 || Pq2 == 0 || Qp1 == 0 || Qp2 == 0)
            {
                // Check for two equal endpoints.  This is done explicitly rather than by the orientation tests below in order to improve robustness.
                if (p1.Equals(q1) || p1.Equals(q2))
                {
                    return(new LineSegment2IntersectionResult <double>(p1));
                }
                else if (p2.Equals(q1) || p2.Equals(q2))
                {
                    return(new LineSegment2IntersectionResult <double>(p2));
                }
                // Now check to see if any endpoint lies on the interior of the other segment.
                else if (Pq1 == 0)
                {
                    return(new LineSegment2IntersectionResult <double>(q1));
                }
                else if (Pq2 == 0)
                {
                    return(new LineSegment2IntersectionResult <double>(q2));
                }
                else if (Qp1 == 0)
                {
                    return(new LineSegment2IntersectionResult <double>(p1));
                }
                else if (Qp2 == 0)
                {
                    return(new LineSegment2IntersectionResult <double>(p2));
                }
            } //end exact at endpoint

            //intersectWNormalization
            Coordinate2 <double> n1     = new Coordinate2 <double>(p1);
            Coordinate2 <double> n2     = new Coordinate2 <double>(p2);
            Coordinate2 <double> n3     = new Coordinate2 <double>(q1);
            Coordinate2 <double> n4     = new Coordinate2 <double>(q2);
            Coordinate2 <double> normPt = new Coordinate2 <double>();

            Coordinate2Utils.NormalizeToEnvCentre(n1, n2, n3, n4, normPt);

            //safeHCoordinateIntersection
            Coordinate2 <double> intPt = null;
            // unrolled computation
            double px = n1.Y - n2.Y;
            double py = n2.X - n1.X;
            double pw = n1.X * n2.Y - n2.X * n1.Y;

            double qx = n3.Y - n4.Y;
            double qy = n4.X - n3.X;
            double qw = n3.X * n4.Y - n4.X * n3.Y;

            double x = py * qw - qy * pw;
            double y = qx * pw - px * qw;
            double w = px * qy - qx * py;

            double xInt = x / w;
            double yInt = y / w;

            if (double.IsNaN(xInt) || double.IsNaN(yInt) || double.IsInfinity(xInt) || double.IsInfinity(yInt))
            {
                intPt = MeanNearest(n1, n2, n3, n4);
                xInt  = intPt.X + normPt.X;
                yInt  = intPt.Y + normPt.Y;
                return(new LineSegment2IntersectionResult <double>(p1.Factory.ConstructPoint(xInt, yInt)));
            }
            else
            {
                xInt += normPt.X;
                yInt += normPt.Y;
                intPt = new Coordinate2 <double>(xInt, yInt);
            }
            //End safeHCoordinateIntersection
            //End intersectWNormalization

            if (!(Coordinate2Utils.PointInEnvelope(p1, p2, intPt) || Coordinate2Utils.PointInEnvelope(q1, q2, intPt)))
            {
                intPt = MeanNearest(p1, p2, q1, q2);
            }

            return(new LineSegment2IntersectionResult <double>(p1.Factory.ConstructPoint(intPt.X, intPt.Y)));
        }