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.
24 lines
587 B
Python
24 lines
587 B
Python
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
|