Skip to content

line

twod.line

a line segment for humans™

Classes

Line(start: Point = None, end: Point = None) dataclass

A line segment defined by two points.

The Line class represents a geometric line segment with start and end points and provides many helpful properties and methods for geometric operations.

line = Line(Point(0, 0), Point(3, 4)) line.length 5.0 line.angle_degrees 53.13010235415598

Attributes
length: float property

The length of this line segment.

length_squared: float property

The squared length of this line segment (faster than length).

vector: Point property

The vector from start to end point.

direction: Point property

The unit vector in the direction from start to end.

angle_radians: float property

The angle of this line in radians, measured counter-clockwise from positive x-axis.

angle_degrees: float property

The angle of this line in degrees, measured counter-clockwise from positive x-axis.

slope: float property

The slope of this line (rise over run).

midpoint: Point property

The midpoint of this line segment.

is_point: bool property

True if this line has zero length (start == end).

is_horizontal: bool property

True if this line is horizontal (same y coordinates).

is_vertical: bool property

True if this line is vertical (same x coordinates).

Functions
reverse() -> Line

Return a new line with start and end points swapped.

extend(distance: float, from_end: bool = True) -> Line

Extend the line by a given distance.

Parameters:

Name Type Description Default
distance float

Distance to extend (positive extends outward, negative inward)

required
from_end bool

If True, extend from end point; if False, extend from start point

True

Returns:

Type Description
Line

New Line extended by the specified distance

point_at_parameter(t: float) -> Point

Get point on line at parameter t.

Parameters:

Name Type Description Default
t float

Parameter where 0 = start point, 1 = end point

required

Returns:

Type Description
Point

Point on the line at parameter t

distance_to_point(point: Point) -> float

Calculate the shortest distance from a point to this line segment.

Parameters:

Name Type Description Default
point Point

Point to calculate distance to

required

Returns:

Type Description
float

Shortest distance from point to line segment

closest_point_on_line(point: Point) -> Point

Find the closest point on this line segment to the given point.

Parameters:

Name Type Description Default
point Point

Point to find closest point to

required

Returns:

Type Description
Point

Closest point on the line segment

intersects_line(other: Line) -> bool

Check if this line segment intersects with another line segment.

Parameters:

Name Type Description Default
other Line

Other line segment to check intersection with

required

Returns:

Type Description
bool

True if the line segments intersect

intersection_point(other: Line) -> Optional[Point]

Find the intersection point of two line segments.

Parameters:

Name Type Description Default
other Line

Other line segment to find intersection with

required

Returns:

Type Description
Optional[Point]

Intersection point if it exists, None otherwise

parallel_to(other: Line) -> bool

Check if this line is parallel to another line.

Parameters:

Name Type Description Default
other Line

Other line to check parallelism with

required

Returns:

Type Description
bool

True if lines are parallel

perpendicular_to(other: Line) -> bool

Check if this line is perpendicular to another line.

Parameters:

Name Type Description Default
other Line

Other line to check perpendicularity with

required

Returns:

Type Description
bool

True if lines are perpendicular

angle_between(other: Line) -> float

Calculate the angle between this line and another line in radians.

Parameters:

Name Type Description Default
other Line

Other line to calculate angle with

required

Returns:

Type Description
float

Angle between lines in radians (0 to π)

contains_point(point: Point, tolerance: float = 1e-10) -> bool

Check if a point lies on this line segment.

Parameters:

Name Type Description Default
point Point

Point to check

required
tolerance float

Tolerance for floating point comparison

1e-10

Returns:

Type Description
bool

True if point lies on the line segment