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:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user