Skip to main content

Module math

Module math 

Source
Expand description

Geometric math utilities.

Port of agg_math.h and agg_sqrt_tables.cpp — distances, intersections, cross products, triangle operations, fast integer sqrt, and Bessel functions.

Constants§

INTERSECTION_EPSILON
Epsilon for intersection calculations.
VERTEX_DIST_EPSILON
Coinciding points maximal distance (epsilon).

Functions§

besj
Bessel function of the first kind of order n.
calc_distance
Euclidean distance between two points.
calc_intersection
Calculate the intersection point of two line segments: (ax,ay)→(bx,by) and (cx,cy)→(dx,dy). Returns Some((x, y)) if they intersect, None if parallel.
calc_line_point_distance
Signed distance from point (x, y) to the infinite line through (x1,y1)→(x2,y2). Positive means left side, negative means right side. If the line segment is degenerate (length < VERTEX_DIST_EPSILON), returns the distance from (x,y) to (x1,y1).
calc_orthogonal
Calculate the orthogonal displacement vector of magnitude thickness perpendicular to the line (x1,y1)→(x2,y2).
calc_polygon_area
Signed area of a polygon defined by a slice of points with x and y fields.
calc_polygon_area_vd
Signed area of a polygon defined by a slice of VertexDist. Same algorithm as calc_polygon_area but works with VertexDist slices (needed by vcgen_contour).
calc_segment_point_sq_distance
Squared distance from point (x, y) to the closest point on segment (x1,y1)→(x2,y2).
calc_segment_point_sq_distance_with_u
Squared distance from point (x, y) to the closest point on segment (x1,y1)→(x2,y2), given pre-computed parameter u.
calc_segment_point_u
Compute the parameter u for the projection of point (x, y) onto the line segment (x1,y1)→(x2,y2). Returns 0 if the segment is degenerate.
calc_sq_distance
Squared Euclidean distance between two points.
calc_triangle_area
Signed area of triangle (x1,y1), (x2,y2), (x3,y3).
cross_product
Cross product of vectors (x2-x1, y2-y1) and (x-x2, y-y2). The sign indicates which side of the line (x1,y1)→(x2,y2) the point (x,y) is on.
dilate_triangle
Dilate a triangle by distance d, producing 6 output points (two per edge). Returns ([x0..x5], [y0..y5]).
fast_sqrt
Fast integer square root using lookup tables. No divisions, multiplications, or loops — just bit shifts and table lookups. Port of C++ fast_sqrt (portable C path).
intersection_exists
Quick check whether two line segments (x1,y1)→(x2,y2) and (x3,y3)→(x4,y4) intersect (boundary excluded).
point_in_triangle
Test if point (x, y) is inside triangle (x1,y1), (x2,y2), (x3,y3).