Esempio n. 1
0
        protected virtual SLExpression VisitTwoPointsLine(TwoPointsLineExpression twoPointsLineExpression)
        {
            var point1 = Visit(twoPointsLineExpression.Point1);
            var point2 = Visit(twoPointsLineExpression.Point2);

            if (point1 != twoPointsLineExpression.Point1 || point2 != twoPointsLineExpression.Point2)
            {
                return(new TwoPointsLineExpression
                {
                    Point1 = (PointExpression)point1,
                    Point2 = (PointExpression)point2
                });
            }
            return(twoPointsLineExpression);
        }
Esempio n. 2
0
        private static CoordinatesPointExpression GetIntersectionPoint(TwoPointsLineExpression line1, TwoPointsLineExpression line2)
        {
            var l1p1 = (CoordinatesPointExpression)line1.Point1;
            var l1p2 = (CoordinatesPointExpression)line1.Point2;
            var l2p1 = (CoordinatesPointExpression)line2.Point1;
            var l2p2 = (CoordinatesPointExpression)line2.Point2;

            // y = ax + c
            var a1 = (l1p1.Y - l1p2.Y) / (l1p1.X - l1p2.X);
            var c1 = l1p1.Y - l1p1.X * a1;
            // Ax + By = C
            var A1 = -a1;
            var B1 = 1;
            var C1 = c1;

            var a2 = (l2p1.Y - l2p2.Y) / (l2p1.X - l2p2.X);
            var c2 = l2p1.Y - l2p1.X * a2;
            var A2 = -a2;
            var B2 = 1;
            var C2 = c2;

            var delta = A1 * B2 - A2 * B1;

            if (delta == 0)
            {
                throw new ArgumentException("Can't compute intersection of parallel lines.");
            }

            var x = (B2 * C1 - B1 * C2) / delta;
            var y = (A1 * C2 - A2 * C1) / delta;

            return(new CoordinatesPointExpression
            {
                X = x,
                Y = y,
            });
        }