示例#1
0
        //i didnt review this one for correctness
        public LineEquation GetIntersectionWithLineForRay(Rect rectangle)
        {
            LineEquation intersectionLine;

            if (Start == End)
            {
                return(null);
            }

            IEnumerable <LineEquation> lines = rectangle.LineSegments();

            intersectionLine = new LineEquation(new Point(0, 0), new Point(0, 0));
            var intersections = new Dictionary <LineEquation, Point>();

            foreach (LineEquation equation in lines)
            {
                Point?intersectionPoint = GetIntersectionWithLineSegment(equation);

                if (intersectionPoint.HasValue)
                {
                    intersections[equation] = intersectionPoint.Value;
                }
            }

            if (!intersections.Any())
            {
                return(null);
            }

            var intersectionPoints = new SortedDictionary <double, Point>();

            foreach (var intersection in intersections)
            {
                if (End.IsBetweenTwoPoints(Start, intersection.Value) ||
                    intersection.Value.IsBetweenTwoPoints(Start, End))
                {
                    double distanceToPoint = Start.DistanceToPoint(intersection.Value);
                    intersectionPoints[distanceToPoint] = intersection.Value;
                }
            }

            if (intersectionPoints.Count == 1)
            {
                Point endPoint = intersectionPoints.First().Value;
                intersectionLine = new LineEquation(Start, endPoint);

                return(intersectionLine);
            }

            if (intersectionPoints.Count == 2)
            {
                Point start = intersectionPoints.First().Value;
                Point end   = intersectionPoints.Last().Value;
                intersectionLine = new LineEquation(start, end);

                return(intersectionLine);
            }

            return(null);
        }
示例#2
0
        public Point?GetIntersectionWithLineSegment(LineEquation otherLine)
        {
            Point?intersectionPoint = GetIntersectionWithLine(otherLine);

            if (intersectionPoint.HasValue &&
                intersectionPoint.Value.IsBetweenTwoPoints(otherLine.Start, otherLine.End))
            {
                return(intersectionPoint);
            }

            return(default(Point?));
        }
示例#3
0
        public Point?GetIntersectionWithLine(LineEquation otherLine)
        {
            double determinant = A * otherLine.B - otherLine.A * B;

            if (determinant.IsZero()) //lines are parallel
            {
                return(default(Point?));
            }

            //Cramer's Rule

            double x = (otherLine.B * C - B * otherLine.C) / determinant;
            double y = (A * otherLine.C - otherLine.A * C) / determinant;

            Point intersectionPoint = new Point(x, y);

            return(intersectionPoint);
        }