예제 #1
0
        /// <summary>
        /// 两直线是否相交
        /// </summary>
        /// <param name="otherLine"></param>
        /// <param name="intersectionPoint"></param>
        /// <returns></returns>
        public bool IntersectsWithLine(LineEquation otherLine, out Point intersectionPoint)
        {
            intersectionPoint = new Point(0, 0);
            if (IsVertical && otherLine.IsVertical)
            {
                return(false);
            }
            if (IsVertical || otherLine.IsVertical)
            {
                intersectionPoint = GetIntersectionPointIfOneIsVertical(otherLine, this);
                return(true);
            }
            double delta           = A * otherLine.B - otherLine.A * B;
            bool   hasIntersection = Math.Abs(delta - 0) > 0.0001f;

            if (hasIntersection)
            {
                double x = (otherLine.B * C - B * otherLine.C) / delta;
                double y = (A * otherLine.C - otherLine.A * C) / delta;
                intersectionPoint = new Point(TypeParse.StrToInt(x), TypeParse.StrToInt(y));;
            }


            /* float delta = A1*B2 - A2*B1;
             * float x = (B2 * C1 - B1 * C2) / delta;
             *  float y = (A1 * C2 - A2 * C1) / delta;*/
            return(hasIntersection);
        }
예제 #2
0
        private static Point GetIntersectionPointIfOneIsVertical(LineEquation line1, LineEquation line2)
        {
            LineEquation verticalLine    = line2.IsVertical ? line2 : line1;
            LineEquation nonVerticalLine = line2.IsVertical ? line1 : line2;

            double y = (verticalLine.Start.X - nonVerticalLine.Start.X) *
                       (nonVerticalLine.End.Y - nonVerticalLine.Start.Y) /
                       ((nonVerticalLine.End.X - nonVerticalLine.Start.X)) +
                       nonVerticalLine.Start.Y;
            double x = verticalLine.Start.X;

            return(new Point(TypeParse.StrToInt(x), TypeParse.StrToInt(y)));
        }