• Calculates the intersection point of two line segments, if it exists.

    This function determines whether two line segments intersect and returns the intersection point (if any) along with a boolean indicating whether an intersection exists within the bounds of the segments.

    Parameters

    • line1: T_Line

      The first line segment, represented as an array of two points [T_Point, T_Point].

    • line2: T_Line

      The second line segment, represented as an array of two points [T_Point, T_Point].

    Returns { doesIntersect: boolean; pt: T_Point | null }

    An object containing:

    • pt: The intersection point as [x, y]. This value will be the calculated intersection point, even if it is outside the bounds of the segments.
    • doesIntersect: A boolean indicating whether the intersection lies within both line segments.
    // Define two line segments
    const line1: T_Line = [[0, 0], [10, 10]];
    const line2: T_Line = [[0, 10], [10, 0]];

    // Check for intersection
    const result = lineLineIntersection(line1, line2);
    console.log(result);
    // Output: { pt: [5, 5], doesIntersect: true }

    // Define two non-intersecting line segments
    const line3: T_Line = [[0, 0], [5, 5]];
    const line4: T_Line = [[6, 6], [10, 10]];

    const result2 = lineLineIntersection(line3, line4);
    console.log(result2);
    // Output: { pt: [5.5, 5.5], doesIntersect: false }