/// <summary>
/// Finds, provided it exists, the intersection point with the specified <see cref="Line"/>.
/// </summary>
///
/// <param name="other"><see cref="Line"/> to find intersection with.</param>
///
/// <returns>Returns intersection point with the specified <see cref="Line"/>, or <see langword="null"/>, if
/// the line does not intersect with this segment.</returns>
///
/// <remarks><para>If the line and the segment do not intersect, the method returns <see langword="null"/>. If the line
/// and the segment share multiple points, the method throws an <see cref="InvalidOperationException"/>.
/// </para></remarks>
///
/// <exception cref="InvalidOperationException">Thrown if this segment is a portion of
/// <paramref name="other"/> line.</exception>
///
public Point?GetIntersectionWith(Line other)
{
Point?result;
if ((line.Slope == other.Slope) || (line.IsVertical && other.IsVertical))
{
if (line.Intercept == other.Intercept)
{
throw new InvalidOperationException("Segment is a portion of the specified line.");
}
// unlike Line.GetIntersectionWith(Line), this does not throw on parallel distinct lines
result = null;
}
else
{
result = line.GetIntersectionWith(other);
}
if ((result.HasValue) && (LocateProjection(result.Value) != ProjectionLocation.SegmentAB))
{
// the intersection is on this segment's extended line, but not on the segment itself
result = null;
}
return(result);
}