Overview
Thespherical_math module provides coordinate transformation functions for astronomical calculations in horizon-based coordinate systems. It handles conversions between sky coordinates (altitude/azimuth) and Cartesian vectors, great-circle distance calculations, and geodesic operations on the celestial sphere.
Location: widgets/spherical_math.py
Type Definitions
SkyCoord
Tuple representing sky coordinates in the local horizon frame.alt_deg(float): Altitude angle in degrees (-90° to +90°)- +90° = zenith (directly overhead)
- 0° = horizon
- -90° = nadir (directly below)
az_deg(float): Azimuth angle in degrees (0° to 360°)- 0° = North
- 90° = East
- 180° = South
- 270° = West
Functions
sky_to_vector()
Converts horizon coordinates (altitude, azimuth) to a 3D unit vector.coord(SkyCoord):(alt_deg, az_deg)tuple
tuple:(x, y, z)unit vector in local horizon frame where:x: Points to North horizony: Points to East horizonz: Points to zenith
vector_to_sky()
Converts a 3D vector to horizon coordinates (altitude, azimuth).v(Sequence[float]): Vector[x, y, z]in local horizon frame (automatically normalized)
SkyCoord:(alt_deg, az_deg)tuple
angular_distance()
Calculates the great-circle angular distance between two sky coordinates.a(SkyCoord): First coordinate(alt1, az1)b(SkyCoord): Second coordinate(alt2, az2)
float: Angular separation in degrees (0° to 180°)
slerp_arc_points()
Generates interpolated points along the shortest great-circle arc between two coordinates.a(SkyCoord): Start coordinateb(SkyCoord): End coordinaten_points(int): Number of interpolated points (default: 64)
List[SkyCoord]: List of interpolated coordinates fromatob
- Drawing constellation lines across the sky
- Animating smooth camera pans
- Sampling measurement ruler paths
destination_point()
Calculates the destination point after traveling a given distance along a bearing (geodesic/direct problem).center(SkyCoord): Starting coordinate(alt, az)bearing_deg(float): Initial bearing in degrees (0° = North, 90° = East)distance_deg(float): Angular distance to travel (degrees)
SkyCoord: Destination coordinate(alt, az)
(alt, az) as (lat, lon) on a unit sphere:
- Drawing circles/arcs around sky coordinates
- Rectangle/square measurement tool corners
- Field-of-view boundaries
screen_to_sky()
Adapter function for converting screen coordinates to sky coordinates using a projection function.sx(float): Screen X coordinate (pixels)sy(float): Screen Y coordinate (pixels)unproject_fn(Callable): Projection inverse function(sx, sy) -> (alt, az) | None
Optional[SkyCoord]: Sky coordinate(alt, az)orNoneif unprojection fails
- Mouse/touch interaction with sky canvas
- Measurement tool coordinate capture
- Object identification under cursor
angular_delta_signed()
Calculates the signed shortest angular difference between two azimuth angles.a_az_deg(float): First azimuth angle (degrees)b_az_deg(float): Second azimuth angle (degrees)
float: Signed differenceb - ain degrees, range[-180°, +180°)- Positive:
bis clockwise froma - Negative:
bis counter-clockwise froma
- Positive:
- Smooth azimuth interpolation (avoiding 360°/0° jumps)
- Camera rotation direction determination
- Constellation boundary wrapping
Helper Functions
_clamp()
Clamps a value to a specified range.v(float): Input valuelo(float): Minimum valuehi(float): Maximum value
float:max(lo, min(hi, v))
_wrap_angle_180()
Wraps angle to range[-180°, +180°).
_wrap_angle_360()
Wraps angle to range[0°, 360°).
Complete Example
Coordinate System Reference
Horizon (Horizontal) Coordinates
- 0° = North
- Increases clockwise (East = 90°, South = 180°, West = 270°)
- +90° = Zenith (straight up)
- 0° = Horizon
- -90° = Nadir (straight down, usually not visible)
Vector Frame
x = cos(alt) * cos(az)- North componenty = cos(alt) * sin(az)- East componentz = sin(alt)- Zenith component
Performance Notes
- All functions use native Python
math(no numpy required) - Vector operations are O(1) constant time
slerp_arc_points()is O(n) where n = number of points- Safe for real-time interactive tools (sub-millisecond)
- Automatic normalization prevents numerical drift
See Also
- AstroEngine - Celestial body position calculations
- Measurement Tools - Uses spherical math for ruler/circle tools
- Sky Widget - Main astronomical canvas