Geometry Module¶
Coordinate transformation utilities for the path generator.
geometry
¶
Coordinate transformation utilities for the path generator.
This module provides functions for transforming coordinates between screen space and a normalized "unit frame" where path generation is resolution-independent.
The unit frame transforms any start→target pair such that: - Start point becomes (0, 0) - Target point becomes (1, 0) - All distances are normalized relative to the start-target distance
get_unit_transform(start, target)
¶
Compute the rotation matrix and distance for unit-frame transformation.
Calculates the transform needed to map screen coordinates to a normalized unit frame where start=(0,0) and target=(1,0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
ndarray
|
Starting point as (x, y) array in screen coordinates. |
required |
target
|
ndarray
|
Target point as (x, y) array in screen coordinates. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, float]
|
Tuple containing: - R: 2x2 rotation matrix that aligns the start→target vector with +X axis. - D: Distance between start and target (used for scaling). |
Example
start = np.array([100, 200]) target = np.array([300, 200]) R, D = get_unit_transform(start, target) D 200.0
R is identity since target is already along +X from start¶
Note
To convert screen → unit: P_unit = (P_screen - start) @ R.T / D
To convert unit → screen: P_screen = start + (P_unit * D) @ R
Source code in src/pathgenerator/geometry.py
rotate_scale_path_to_hit_target(path_xy, start_xy, target_xy, *, scale_to_distance=True)
¶
Rotate and scale a path so its endpoint exactly matches the target.
After path simulation, there may be small numerical errors that cause the final point to not exactly hit the target. This function applies a rotation (and optionally uniform scaling) around the start point to correct this.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_xy
|
ndarray
|
Path as (N, 2) numpy array of screen coordinates. |
required |
start_xy
|
Tuple[float, float]
|
Start point (should match path_xy[0]). |
required |
target_xy
|
Tuple[float, float]
|
Desired endpoint for the path. |
required |
scale_to_distance
|
bool
|
If True, uniformly scale the path so the endpoint distance matches exactly. If False, only rotate (preserves path length). |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Transformed path as (N, 2) numpy array with path[-1] == target_xy. |
Example
path = np.array([[0, 0], [50, 10], [95, 5]]) # Slightly off-target target = (100, 0) fixed = rotate_scale_path_to_hit_target(path, (0, 0), target) fixed[-1] array([100., 0.])
Note
If either the current endpoint or target is very close to the start (< 1e-6 distance), the path is returned unchanged to avoid division by zero.