public bool TryFindIdenticalPoint(LineSegment2D lineSegment, out Point2D point)
 {
     if (PointList[0].Equals(lineSegment.PointList[0]) || PointList[0].Equals(lineSegment.PointList[1]))
     {
         point = PointList[0];
         return(true);
     }
     else if (PointList[1].Equals(lineSegment.PointList[0]) || PointList[1].Equals(lineSegment.PointList[1]))
     {
         point = PointList[1];
         return(true);
     }
     else
     {
         point = null;
         return(false);
     }
 }
 public Shape2D IntersectingShape(LineSegment2D lineSegment)
 {
     if (Equals(lineSegment))
     {
         return(new LineSegment2D(PointList[0].Clone(), PointList[1].Clone()));
     }
     else if (TryFindIdenticalPoint(lineSegment, out Point2D point))
     {
         return(point.Clone());
     }
     else if (IsOnSameLine(lineSegment))
     {
         return(IntersectingShapeOnLine(lineSegment));
     }
     else
     {
         return(IntersectingShapeGenericCase(lineSegment));
     }
 }
        private Shape2D IntersectingShapeGenericCase(LineSegment2D lineSegment)
        {
            double r0x = PointList[0].X;
            double r0y = PointList[0].Y;
            double r1x = PointList[1].X - PointList[0].X;
            double r1y = PointList[1].Y - PointList[0].Y;
            double s0x = lineSegment.PointList[0].X;
            double s0y = lineSegment.PointList[0].Y;
            double s1x = lineSegment.PointList[1].X - lineSegment.PointList[0].X;
            double s1y = lineSegment.PointList[1].Y - lineSegment.PointList[0].Y;

            double u = (s1x * (s0y - r0y) - s0x * s1y + r0x * s1y) / (r1y * s1x - r1x * s1y);
            double v = (r1x * (s0y - r0y) - s0x * r1y + r0x * r1y) / (r1y * s1x - r1x * s1y);

            if (!Utilities.LessThanValue(u, 0) && !Utilities.GreaterThanValue(u, 1) &&
                !Utilities.LessThanValue(v, 0) && !Utilities.GreaterThanValue(v, 1))
            {
                return(new Point2D(r0x + u * r1x, r0y + u * r1y));
            }
            else
            {
                return(new EmptyShape2D());
            }
        }