Example #1
0
        /// <summary>
        /// Computes the (approximate) intersection point between two line segments
        /// using homogeneous coordinates.
        /// Notice that this algorithm is
        /// not numerically stable; i.e. it can produce intersection points which
        /// lie outside the envelope of the line segments themselves.  In order
        /// to increase the precision of the calculation input points should be normalized
        /// before passing them to this routine.
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="q1"></param>
        /// <param name="q2"></param>
        /// <returns></returns>
        public static Coordinate Intersection(Coordinate p1, Coordinate p2, Coordinate q1, Coordinate q2)
        {
            HCoordinate l1        = new HCoordinate(new HCoordinate(p1), new HCoordinate(p2));
            HCoordinate l2        = new HCoordinate(new HCoordinate(q1), new HCoordinate(q2));
            HCoordinate intHCoord = new HCoordinate(l1, l2);
            Coordinate  intPt     = intHCoord.Coordinate;

            return(intPt);
        }
        /// <summary>
        /// This method computes the actual value of the intersection point.
        /// To obtain the maximum precision from the intersection calculation,
        /// the coordinates are normalized by subtracting the minimum
        /// ordinate values (in absolute value).  This has the effect of
        /// removing common significant digits from the calculation to
        /// maintain more bits of precision.
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="q1"></param>
        /// <param name="q2"></param>
        /// <returns></returns>
        private Coordinate Intersection(Coordinate p1, Coordinate p2, Coordinate q1, Coordinate q2)
        {
            Coordinate n1     = new Coordinate(p1);
            Coordinate n2     = new Coordinate(p2);
            Coordinate n3     = new Coordinate(q1);
            Coordinate n4     = new Coordinate(q2);
            Coordinate normPt = new Coordinate();

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

            Coordinate intPt = HCoordinate.Intersection(n1, n2, n3, n4);

            intPt.X += normPt.X;
            intPt.Y += normPt.Y;

            /*
             *
             * MD - May 4 2005 - This is still a problem.  Here is a failure case:
             *
             * LINESTRING (2089426.5233462777 1180182.3877339689, 2085646.6891757075 1195618.7333999649)
             * LINESTRING (1889281.8148903656 1997547.0560044837, 2259977.3672235999 483675.17050843034)
             * int point = (2097408.2633752143, 1144595.8008114607)
             */
            if (!IsInSegmentEnvelopes(intPt))
            {
                Trace.WriteLine("Intersection outside segment envelopes: " + intPt);
            }

            /*
             * // disabled until a better solution is found
             * if (!IsInSegmentEnvelopes(intPt))
             * {
             *  Trace.WriteLine("first value outside segment envelopes: " + intPt);
             *
             *  IteratedBisectionIntersector ibi = new IteratedBisectionIntersector(p1, p2, q1, q2);
             *  intPt = ibi.Intersection;
             * }
             * if (!IsInSegmentEnvelopes(intPt))
             * {
             *  Trace.WriteLine("ERROR - outside segment envelopes: " + intPt);
             *
             *  IteratedBisectionIntersector ibi = new IteratedBisectionIntersector(p1, p2, q1, q2);
             *  Coordinate testPt = ibi.Intersection;
             * }
             */

            if (PrecisionModel != null)
            {
                PrecisionModel.MakePrecise(intPt);
            }

            return(intPt);
        }
Example #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="p1"></param>
 /// <param name="p2"></param>
 public HCoordinate(HCoordinate p1, HCoordinate p2)
 {
     _x = p1._y * p2._w - p2._y * p1._w;
     _y = p2._x * p1._w - p1._x * p2._w;
     _w = p1._x * p2._y - p2._x * p1._y;
 }