garmentiq.landmark.derivation.line_intersect

Intersection of two lines in the image plane.

 1"""Intersection of two lines in the image plane."""
 2import numpy as np
 3from typing import Tuple, Optional
 4
 5
 6def _find_line_line_intersection(
 7    p1: Tuple[float, float],
 8    v1: Tuple[float, float],
 9    p2: Tuple[float, float],
10    v2: Tuple[float, float],
11) -> Optional[Tuple[float, float]]:
12    """
13    Calculates the intersection point of two lines, each defined by a point and a direction vector.
14
15    Args:
16        p1 (Tuple[float, float]): A point (x1, y1) on the first line.
17        v1 (Tuple[float, float]): The direction vector (dx1, dy1) of the first line.
18        p2 (Tuple[float, float]): A point (x2, y2) on the second line.
19        v2 (Tuple[float, float]): The direction vector (dx2, dy2) of the second line.
20
21    Returns:
22        Optional[Tuple[float, float]]: The (x, y) coordinates of the intersection point,
23                                       or None if the lines are parallel or collinear (no unique intersection).
24    """
25    x1, y1 = p1
26    dx1, dy1 = v1
27    x2, y2 = p2
28    dx2, dy2 = v2
29
30    denominator = dx2 * dy1 - dy2 * dx1
31    if np.isclose(denominator, 0):  # Lines are parallel or collinear
32        return None
33
34    qp_x = x1 - x2
35    qp_y = y1 - y2
36    t = (qp_x * dy1 - qp_y * dx1) / denominator
37    ix = x2 + t * dx2
38    iy = y2 + t * dy2
39
40    return ix, iy