feat: Add perpendicular distance calculation utility function

Implemented a utility function `calculate_perpendicular_distance` in the `app/utils/__init__.py` module. The function calculates the perpendicular distance between a point and a line using NumPy, with type hints and runtime type checking using jaxtyping and typeguard.
This commit is contained in:
2025-03-04 17:34:27 +08:00
parent ab64051237
commit a06b3e8399
3 changed files with 835 additions and 2 deletions

View File

@ -0,0 +1,23 @@
from typing import Any
import numpy as np
from jaxtyping import Float, Num, jaxtyped
from typeguard import typechecked
from app._typing import NDArray
@jaxtyped(typechecker=typechecked)
def calculate_perpendicular_distance(
point: Num[NDArray, "2"],
line: Num[NDArray, "2 2"],
) -> np.floating[Any]:
"""
Calculate the perpendicular distance between a point and a line.
"""
line_start, line_end = line
distance = np.linalg.norm(
np.cross(line_end - line_start, line_start - point)
) / np.linalg.norm(line_end - line_start)
return distance