Beispiel #1
0
        public Either <Null, Point, LineSegment> IntersectWithSegment(LineSegment lineSegment)
        {
            return(new Line(this).IntersectWithSegment(lineSegment).Switch <Either <Null, Point, LineSegment> >
                   (
                       @null => @null,
                       point =>
            {
                if (point == BasePoint || BasePoint.DirectionTo(point) == Direction)
                {
                    return point;
                }

                return Null.Instance;
            },
                       segment =>
            {
                var points = segment.EndPoints
                             .Where(point =>
                {
                    var towardsEndPoint = new Direction(BasePoint, point);
                    return towardsEndPoint == Direction || point == BasePoint;
                })
                             .ToList();
                if (points.Count == 2)
                {
                    return segment;
                }
                if (points.Count == 1)
                {
                    var segmentPoint = points[0];
                    if (segmentPoint != BasePoint)
                    {
                        return new LineSegment(BasePoint, segmentPoint);
                    }

                    return segmentPoint;
                }

                return Null.Instance;
            }
                   ));
        }