Example #1
0
    /// <summary>
    /// Finds, provided it exists, the intersection point with the specified <see cref="Line2D"/>.
    /// </summary>
    ///
    /// <param name="other"><see cref="Line2D"/> to find intersection with.</param>
    ///
    /// <returns>Returns intersection point with the specified <see cref="Line2D"/>, 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="OverlappingLinesException"/>.
    /// </para></remarks>
    ///
    /// <exception cref="OverlappingLinesException">Thrown if this segment is a portion of
    /// <paramref name="other"/> line.</exception>
    ///
    public Vector2?GetIntersectionWith(Line2D other)
    {
        Vector2?result;

        if ((_line.Slope == other.Slope) || (_line.IsVertical && other.IsVertical))
        {
            if (_line.Intercept == other.Intercept)
            {
                throw new OverlappingLineLineSegmentException(this, other, "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);
    }
Example #2
0
        private static void Line2DTest()
        {
            Line2D line1 = new Line2D(1, 1, 1);
            Line2D line2 = new Line2D(1, 1, 0);
            Line2D line3 = new Line2D(1, -1, -3);

            Console.WriteLine("Line1: {0}", line1);
            Console.WriteLine("Line2: {0}", line2);
            Console.WriteLine("Line3: {0}", line3);

            Point2D intersection = line1.GetIntersectionWith(line2);

            if (intersection == null)
            {
                Console.WriteLine("No intersection between line1 and line2.");
            }
            else
            {
                Console.WriteLine("Intersection: {0}", intersection);
            }

            intersection = line1.GetIntersectionWith(line3);
            if (intersection == null)
            {
                Console.WriteLine("No intersection between line1 and line3.");
            }
            else
            {
                Console.WriteLine("Intersection: {0}", intersection);
            }

            Point2D p1    = new Point2D(1, 5);
            Point2D p2    = new Point2D(2, 4);
            Line2D  line4 = new Line2D(p1, p2);

            Console.WriteLine("Line4: {0}", line4);

            Line2D line5 = new Line2D(0, 1, -3);

            Console.WriteLine("line5: {0}", line5);
        }