Repository: ManimCommunity/manim
File: manim/utils/space_ops.py (Python)

Code before:

    Returns
    -------
    np.ndarray
        The intersection points of the two lines which are intersecting.

    Raises
    ------
    ValueError
        Error is produced if the two lines don't intersect with each other
        or if the coordinates don't lie on the xy-plane.
    """
    if any(np.array([line1, line2])[:, :, 2].reshape(-1)):
        # checks for z coordinates != 0
        raise ValueError("Coords must be in the xy-plane.")

    # algorithm from https://stackoverflow.com/a/42727584
    padded = (
        np.pad(np.array(i)[:, :2], ((0, 0), (0, 1)), constant_values=1)
        for i in (line1, line2)
    )
    line1, line2 = (np.cross(*i) for i in padded)
    x, y, z = np.cross(line1, line2)

    if z == 0:
        raise ValueError("Lines do not intersect.")

    return np.array([x / z, y / z, 0])


def find_intersection(
    p0s: Sequence[np.ndarray],
    v0s: Sequence[np.ndarray],
    p1s: Sequence[np.ndarray],
    v1s: Sequence[np.ndarray],
    threshold: float = 1e-5,
) -> Sequence[np.ndarray]:
    """
    Return the intersection of a line passing through p0 in direction v0
    with one passing through p1 in direction v1 (or array of intersections
    from arrays of such points/directions).
    For 3d values, it returns the point on the ray p0 + v0 * t closest to the
    ray p1 + v1 * t
    """
    # algorithm from https://en.wikipedia.org/wiki/Skew_lines#Nearest_points
    result = []

    for p0, v0, p1, v1 in zip(*[p0s, v0s, p1s, v1s]):
        normal = np.cross(v1, np.cross(v0, v1))
        result += [p0 + np.dot(p1 - p0, normal) / np.dot(v0, normal) * v0]
    return result

Review comment (suggestion):
```suggestion
        raise ValueError("Line segments do not intersect.")
```

Code after:
    Returns
    -------
    np.ndarray
        The intersection points of the two lines which are intersecting.

    Raises
    ------
    ValueError
        Error is produced if the two lines don't intersect with each other
        or if the coordinates don't lie on the xy-plane.
    """
    if any(np.array([line1, line2])[:, :, 2].reshape(-1)):
        # checks for z coordinates != 0
        raise ValueError("Coords must be in the xy-plane.")

    # algorithm from https://stackoverflow.com/a/42727584
    padded = (
        np.pad(np.array(i)[:, :2], ((0, 0), (0, 1)), constant_values=1)
        for i in (line1, line2)
    )
    line1, line2 = (np.cross(*i) for i in padded)
    x, y, z = np.cross(line1, line2)

    if z == 0:
        raise ValueError(
            "The lines are parallel, there is no unique intersection point."
        )

    return np.array([x / z, y / z, 0])


def find_intersection(
    p0s: Sequence[np.ndarray],
    v0s: Sequence[np.ndarray],
    p1s: Sequence[np.ndarray],
    v1s: Sequence[np.ndarray],
    threshold: float = 1e-5,
) -> Sequence[np.ndarray]:
    """
    Return the intersection of a line passing through p0 in direction v0
    with one passing through p1 in direction v1 (or array of intersections
    from arrays of such points/directions).
    For 3d values, it returns the point on the ray p0 + v0 * t closest to the
    ray p1 + v1 * t
    """
    # algorithm from https://en.wikipedia.org/wiki/Skew_lines#Nearest_points
    result = []

    for p0, v0, p1, v1 in zip(*[p0s, v0s, p1s, v1s]):
        normal = np.cross(v1, np.cross(v0, v1))
        result += [p0 + np.dot(p1 - p0, normal) / np.dot(v0, normal) * v0]