Ejemplo n.º 1
0
        /// <summary>
        /// Computes the distance from a line segment AB to a line segment CD
        /// <para/>
        /// Note: NON-ROBUST!
        /// </summary>
        /// <param name="A">The first point of the first line</param>
        /// <param name="B">The second point of the first line (must be different to A)</param>
        /// <param name="C">The first point of the second line</param>
        /// <param name="D">The second point of the second line (must be different to C)</param>
        /// <returns>The distance from a line segment AB to a line segment CD</returns>
        public static double SegmentToSegment(Coordinate A, Coordinate B,
                                              Coordinate C, Coordinate D)
        {
            // check for zero-length segments
            if (A.Equals(B))
            {
                return(DistanceComputer.PointToSegment(A, C, D));
            }
            if (C.Equals(D))
            {
                return(DistanceComputer.PointToSegment(D, A, B));
            }

            // AB and CD are line segments

            /*
             * from comp.graphics.algo
             *
             * Solving the above for r and s yields
             *
             *     (Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy)
             * r = ----------------------------- (eqn 1)
             *     (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)
             *
             *     (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
             * s = ----------------------------- (eqn 2)
             *     (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)
             *
             * Let P be the position vector of the
             * intersection point, then
             *   P=A+r(B-A) or
             *   Px=Ax+r(Bx-Ax)
             *   Py=Ay+r(By-Ay)
             * By examining the values of r & s, you can also determine some other limiting
             * conditions:
             *   If 0<=r<=1 & 0<=s<=1, intersection exists
             *      r<0 or r>1 or s<0 or s>1 line segments do not intersect
             *   If the denominator in eqn 1 is zero, AB & CD are parallel
             *   If the numerator in eqn 1 is also zero, AB & CD are collinear.
             */

            var noIntersection = false;

            if (!Envelope.Intersects(A, B, C, D))
            {
                noIntersection = true;
            }
            else
            {
                double denom = (B.X - A.X) * (D.Y - C.Y) - (B.Y - A.Y) * (D.X - C.X);

                if (denom == 0)
                {
                    noIntersection = true;
                }
                else
                {
                    double r_num = (A.Y - C.Y) * (D.X - C.X) - (A.X - C.X) * (D.Y - C.Y);
                    double s_num = (A.Y - C.Y) * (B.X - A.X) - (A.X - C.X) * (B.Y - A.Y);

                    double s = s_num / denom;
                    double r = r_num / denom;

                    if ((r < 0) || (r > 1) || (s < 0) || (s > 1))
                    {
                        noIntersection = true;
                    }
                }
            }
            if (noIntersection)
            {
                return(MathUtil.Min(
                           DistanceComputer.PointToSegment(A, C, D),
                           DistanceComputer.PointToSegment(B, C, D),
                           DistanceComputer.PointToSegment(C, A, B),
                           DistanceComputer.PointToSegment(D, A, B)));
            }
            // segments intersect
            return(0.0);
        }
Ejemplo n.º 2
0
 public static double DistancePointLine(Coordinate p, Coordinate A, Coordinate B)
 {
     return(DistanceComputer.PointToSegment(p, A, B));
 }