コード例 #1
0
        /// <summary>
        /// Compute the intersection of a point p and the line p1-p2.
        /// </summary>
        /// <param name="p">The test point.</param>
        /// <param name="p1">The starting coordinate of the line.</param>
        /// <param name="p2">The ending coordinate of the line.</param>
        /// <remarks>
        /// This function computes the boolean value of the HasIntersection test.
        /// The actual value of the intersection (if there is one)
        /// is equal to the value of p.
        /// </remarks>
        public override void ComputeIntersection(Coordinate p, Coordinate p1, Coordinate p2)
        {
            if (p1 == null)
            {
                throw new ArgumentNullException("p1");
            }
            if (p2 == null)
            {
                throw new ArgumentNullException("p2");
            }
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }

            m_bIsProper = false;
            // do between check first, since it is faster than the orientation test
            if (Envelope.Intersects(p1, p2, p))
            {
                if ((RobustCGAlgorithms.OrientationIndex(p1, p2, p) == 0) &&
                    (RobustCGAlgorithms.OrientationIndex(p2, p1, p) == 0))
                {
                    m_bIsProper = true;
                    if (p.Equals(p1) || p.Equals(p2))
                    {
                        m_bIsProper = false;
                    }
                    result = (int)IntersectState.DoIntersect;

                    return;
                }
            }

            result = (int)IntersectState.DonotIntersect;
        }
コード例 #2
0
        /// <summary>
        /// Computes the intersection of the lines p1-p2 and q1-q2.
        /// </summary>
        /// <param name="p1">The starting coordinate of first line.</param>
        /// <param name="p2">The ending coordinate of first line.</param>
        /// <param name="q1">The starting coordinate of seocnd line.</param>
        /// <param name="q2">The ending coordinate of second line.</param>
        /// <returns>
        /// A integer, which is one of the <see cref="IntersectState"/> values,
        /// specifying the intersection state of the lines.
        /// </returns>
        protected override int ComputeIntersect(Coordinate p1, Coordinate p2,
                                                Coordinate q1, Coordinate q2)
        {
            if (p1 == null)
            {
                throw new ArgumentNullException("p1");
            }
            if (p2 == null)
            {
                throw new ArgumentNullException("p2");
            }
            if (q1 == null)
            {
                throw new ArgumentNullException("q1");
            }
            if (q2 == null)
            {
                throw new ArgumentNullException("q2");
            }

            m_bIsProper = false;

            // first try a fast test to see if the envelopes of the lines intersect
            if (!Envelope.Intersects(p1, p2, q1, q2))
            {
                return((int)IntersectState.DonotIntersect);
            }

            // 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 = RobustCGAlgorithms.OrientationIndex(p1, p2, q1);
            int Pq2 = RobustCGAlgorithms.OrientationIndex(p1, p2, q2);

            if ((Pq1 > 0 && Pq2 > 0) || (Pq1 < 0 && Pq2 < 0))
            {
                return((int)IntersectState.DonotIntersect);
            }

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

            if ((Qp1 > 0 && Qp2 > 0) || (Qp1 < 0 && Qp2 < 0))
            {
                return((int)IntersectState.DonotIntersect);
            }

            bool collinear = Pq1 == 0 && Pq2 == 0 && Qp1 == 0 && Qp2 == 0;

            if (collinear)
            {
                return(ComputeCollinearIntersection(p1, p2, q1, q2));
            }

            // 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)
            {
                m_bIsProper = false;
                if (Pq1 == 0)
                {
                    intPt[0] = new Coordinate(q1);
                }

                if (Pq2 == 0)
                {
                    intPt[0] = new Coordinate(q2);
                }

                if (Qp1 == 0)
                {
                    intPt[0] = new Coordinate(p1);
                }

                if (Qp2 == 0)
                {
                    intPt[0] = new Coordinate(p2);
                }
            }
            else
            {
                m_bIsProper = true;
                intPt[0]    = Intersection(p1, p2, q1, q2);
            }

            return((int)IntersectState.DoIntersect);
        }