Skip to main content

raygeo.geo.shape.line

Line segment geometry queries.

Provides line-line intersection (infinite lines), line-segment intersection, closest point on a line or segment to a given point, line-segment-vs-polygon intersections, point-on-segment tests, point-in-rectangle tests, rectangle containment checks, and angle-at-vertex computation.

Functions

does_line_cross_polygon()

does_line_cross_polygon(
a: types.Point,
b: types.Point,
polygon: list[types.Point],
) -> bool

Check if a line segment crosses the interior of a polygon.

Returns True when the segment strictly crosses the polygon boundary — touching a vertex or grazing an edge at an endpoint is not considered a crossing.

ParameterTypeDescription
atypes.PointSegment start point (x, y).
btypes.PointSegment end point (x, y).
polygonlist[types.Point]Polygon vertices [(x1, y1), (x2, y2), ...].
ReturnsboolTrue if the segment crosses the polygon interior.
ComplexityO(n) time, O(1) space

Check if line segment crosses polygon interior. Left: crosses (red). Right: touches boundary (gray).

Check if line segment crosses polygon interior. Left: crosses (red). Right: touches boundary (gray).

does_line_segment_intersect_circle()

does_line_segment_intersect_circle(
p1: types.Point,
p2: types.Point,
circle_center: types.Point,
circle_radius: float,
) -> bool

Check if a line segment intersects a circle.

ParameterTypeDescription
p1types.PointStart of the line segment.
p2types.PointEnd of the line segment.
circle_centertypes.PointCircle center (x, y).
circle_radiusfloatCircle radius.
ReturnsboolTrue if the segment intersects the circle.
ComplexityO(1) time, O(1) space

does_line_segment_intersect_rect()

does_line_segment_intersect_rect(
p1: types.Point,
p2: types.Point,
rect: types.Rect,
) -> bool

Check if a line segment intersects a rectangle.

ParameterTypeDescription
p1types.PointStart of the line segment.
p2types.PointEnd of the line segment.
recttypes.RectRectangle (x_min, y_min, x_max, y_max).
ReturnsboolTrue if the segment intersects the rectangle.
ComplexityO(1) time, O(1) space

get_angle_at_vertex()

get_angle_at_vertex(p0: types.Point, p1: types.Point, p2: types.Point) -> float

Compute the angle at vertex p1.

ParameterTypeDescription
p0types.PointPrevious point.
p1types.PointVertex point.
p2types.PointNext point.
ReturnsfloatAngle in radians.
ComplexityO(1) time, O(1) space

get_interior_angle()

get_interior_angle(p0: types.Point, p1: types.Point, p2: types.Point) -> float

Interior angle at vertex p1 formed by edges p0→p1 and p1→p2.

Returns 0.0 when any two adjacent points coincide (degenerate input).

ParameterTypeDescription
p0types.PointPrevious point.
p1types.PointVertex point.
p2types.PointNext point.
ReturnsfloatAngle in radians in [0, π].
ComplexityO(1) time, O(1) space

get_line_closest_point()

get_line_closest_point(
line_p1: types.Point,
line_p2: types.Point,
x: float,
y: float,
) -> types.Point

Get the closest point on an infinite line to a given point. The result may lie beyond the segment endpoints (unclamped projection).

ParameterTypeDescription
line_p1types.PointFirst point on the line.
line_p2types.PointSecond point on the line.
xfloatX coordinate of target point.
yfloatY coordinate of target point.
Returnstypes.PointClosest point (x, y) on the infinite line.
ComplexityO(1) time, O(1) space

get_line_line_intersection()

get_line_line_intersection(
p1: types.Point,
p2: types.Point,
p3: types.Point,
p4: types.Point,
) -> Optional[types.Point]

Get the intersection of two infinite lines.

ParameterTypeDescription
p1types.PointFirst point on line 1.
p2types.PointSecond point on line 1.
p3types.PointFirst point on line 2.
p4types.PointSecond point on line 2.
ReturnsOptional[types.Point]Intersection point (x, y) or None.
ComplexityO(1) time, O(1) space

Line-line and segment intersection

Line-line and segment intersection

get_line_segment_closest_point()

get_line_segment_closest_point(
seg_p1: types.Point,
seg_p2: types.Point,
x: float,
y: float,
) -> tuple[float, types.Point, float]

Get closest point on a line segment to a point.

ParameterTypeDescription
seg_p1types.PointStart of the line segment.
seg_p2types.PointEnd of the line segment.
xfloatX coordinate of target point.
yfloatY coordinate of target point.
Returnstuple[float, types.Point, float]Tuple of (parameter, closest_point, distance).
ComplexityO(1) time, O(1) space

get_line_segment_intersection()

get_line_segment_intersection(
p1: types.Point,
p2: types.Point,
p3: types.Point,
p4: types.Point,
) -> Optional[types.Point]

Get the intersection of two line segments.

ParameterTypeDescription
p1types.PointStart of segment 1.
p2types.PointEnd of segment 1.
p3types.PointStart of segment 2.
p4types.PointEnd of segment 2.
ReturnsOptional[types.Point]Intersection point (x, y) or None.
ComplexityO(1) time, O(1) space

get_line_segment_length()

get_line_segment_length(p1: types.Point, p2: types.Point) -> float

Compute the length of a line segment.

ParameterTypeDescription
p1types.PointStart point (x, y).
p2types.PointEnd point (x, y).
ReturnsfloatDistance between the two points.
ComplexityO(1) time, O(1) space

get_line_segment_polygon_intersections()

get_line_segment_polygon_intersections(
p1: types.Point,
p2: types.Point,
polygon: Sequence[types.Polygon],
) -> list[float]

Get t-values where a line segment intersects a polygon.

ParameterTypeDescription
p1types.PointStart of the line segment.
p2types.PointEnd of the line segment.
polygonSequence[types.Polygon]Polygon to check against.
Returnslist[float]List of t-values of intersection points.
ComplexityO(n) time, O(1) space

get_point_line_distance()

get_point_line_distance(
point: types.Point,
line_p1: types.Point,
line_p2: types.Point,
) -> float

Get the distance from a point to a line segment. The projection is clamped to the segment, so distance is measured to the nearest endpoint when the perpendicular falls outside.

ParameterTypeDescription
pointtypes.PointPoint (x, y).
line_p1types.PointFirst point on the segment.
line_p2types.PointSecond point on the segment.
ReturnsfloatDistance (clamped to segment).
ComplexityO(1) time, O(1) space

Perpendicular distance from a point to a line

Perpendicular distance from a point to a line

get_segment_segment_distance()

get_segment_segment_distance(
a: tuple[float, float],
b: tuple[float, float],
c: tuple[float, float],
d: tuple[float, float],
) -> float

Minimum Euclidean distance between two line segments.

ParameterTypeDescription
atuple[float, float]Start of segment 1.
btuple[float, float]End of segment 1.
ctuple[float, float]Start of segment 2.
dtuple[float, float]End of segment 2.
ReturnsfloatMinimum distance between the two segments.
ComplexityO(1) time, O(1) space

Minimum Euclidean distance between 2 segments. Left: crossing (0). Centre: parallel. Right: skew.

Minimum Euclidean distance between 2 segments. Left: crossing (0). Centre: parallel. Right: skew.

interpolated_segment_3d()

interpolated_segment_3d(
from_x: float,
from_y: float,
to_x: float,
to_y: float,
z: float,
n: int,
) -> list[tuple[float, float, float]]

Generate linearly interpolated 3D points along a 2D segment.

Returns n points from from to to at height z. The start is not included; the end is included.

ParameterTypeDescription
from_xfloatX coordinate of the start.
from_yfloatY coordinate of the start.
to_xfloatX coordinate of the end.
to_yfloatY coordinate of the end.
zfloatZ height for all points.
nintNumber of points to generate.
Returnslist[tuple[float, float, float]]List of (x, y, z) points.
ComplexityO(n) time, O(1) space

Linearly interpolated 3D points along a 2D segment

Linearly interpolated 3D points along a 2D segment

is_point_on_line_segment()

is_point_on_line_segment(
point: types.Point,
seg_p1: types.Point,
seg_p2: types.Point,
) -> bool

Check if a point is on a line segment.

ParameterTypeDescription
pointtypes.PointPoint (x, y) to test.
seg_p1types.PointStart of the line segment.
seg_p2types.PointEnd of the line segment.
ReturnsboolTrue if the point lies on the segment.
ComplexityO(1) time, O(1) space

longest_line_through_point()

longest_line_through_point(
pt: tuple[float, float],
bbox: tuple[float, float, float, float],
) -> tuple[tuple[float, float], tuple[float, float]]

Find the longest axis-aligned line through a point within a rectangle.

Returns (start, end) — a horizontal line when the bounding box is wider than tall, otherwise a vertical line.

ParameterTypeDescription
pttuple[float, float](x, y) point.
bboxtuple[float, float, float, float](x_min, y_min, x_max, y_max) rectangle.
Returnstuple[tuple[float, float], tuple[float, float]]((x1, y1), (x2, y2)) start and end of the line.
ComplexityO(1) time, O(1) space

Longest axis-aligned line through a point in a bbox. Left: wider→horizontal. Right: taller→vertical.

Longest axis-aligned line through a point in a bbox. Left: wider→horizontal. Right: taller→vertical.