geom2d.line

Basic 2D line/segment geometry.

class geom2d.line.Line(p1: TPoint | Sequence[TPoint], p2: TPoint | None = None)

Two dimensional immutable line segment defined by two points.

Parameters:
  • p1 – Start point as 2-tuple (x, y).

  • p2 – End point as 2-tuple (x, y).

angle() float

The angle of this line segment in radians.

bisector() Line

Perpendicular bisector line.

Essentially this line segment rotated 90deg about its midpoint.

Returns:

A line that is perpendicular to and passes through the midpoint of this line.

distance_to_point(p: Sequence[float], segment: bool = False) float

Distance from line to point.

The Euclidean distance from the specified point and its normal projection on to this line or segment.

See http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html http://paulbourke.net/geometry/pointlineplane/

Parameters:
  • p – point to project on to line

  • segment – if True and if the point projection lies outside the two end points that define this line segment then return the shortest distance to either of the two endpoints. Default is False.

end_tangent_angle() float

The angle of this line segment in radians.

extend(amount: float, from_midpoint: bool = False) Line

Extend/shrink line.

A line segment that is longer (or shorter) than this line by amount amount.

Parameters:
  • amount – The distance to extend the line. The line length will be increased by this amount. If amount is less than zero the length will be decreased.

  • from_midpoint – Extend the line an equal amount on both ends relative to the midpoint. The amount length on both ends will be amount/2. Default is False.

Returns:

A new Line.

flipped() Line

Return a Line segment flipped 180deg around the first point.

static from_polar(startp: Sequence[float], length: float, angle: float) Line

Create a Line given a start point, magnitude (length), and angle.

general_equation() tuple[float, float, float]

Compute the coefficients of the general equation of this line.

Where the equation is of the form ax + by - c = 0.

See: http://www.cut-the-knot.org/Curriculum/Calculus/StraightLine.shtml

Returns:

A 3-tuple (a, b, c)

intersection(other: Sequence[Sequence[float]], segment: bool = False, seg_a: bool = False, seg_b: bool = False) P | None

Intersection point (if any) of this line and another line.

See:

<http://paulbourke.net/geometry/pointlineplane/> and <http://mathworld.wolfram.com/Line-LineIntersection.html> and <http://geomalgorithms.com/a05-_intersect-1.html>

Parameters:
  • other – line to test for intersection. A 4-tuple containing line endpoints.

  • segment – if True then the intersection point must lie on both segments.

  • seg_a – If True the intersection point must lie on this line segment.

  • seg_b – If True the intersection point must lie on the other line segment.

Returns:

A point if they intersect otherwise None.

intersection_mu(other: Sequence[Sequence[float]], segment: bool = False, seg_a: bool = False, seg_b: bool = False) float | None

Line intersection.

http://paulbourke.net/geometry/pointlineplane/ and http://mathworld.wolfram.com/Line-LineIntersection.html and https://stackoverflow.com/questions/20677795/how-do-i-compute-the-intersection-point-of-two-lines (second SO answer)

Parameters:
  • other – line to test for intersection. A 4-tuple containing line endpoints.

  • segment – if True then the intersection point must lie on both segments.

  • seg_a – If True the intersection point must lie on this line segment.

  • seg_b – If True the intersection point must lie on the other line segment.

Returns:

The unit distance from the segment starting point to the point of intersection if they intersect. Otherwise None if the lines or segments do not intersect.

intersects(other: Sequence[Sequence[float]], segment: bool = False) bool

Return True if this segment intersects another segment.

is_parallel(other: Sequence[Sequence[float]], inline: bool = False) bool

Determine if this line segment is parallel with another line.

Parameters:
  • other (tuple) – The other line as a tuple of two points.

  • inline (bool) – The other line must also be inline with this segment.

Returns:

True if the other segment is parallel. If inline is

True then the other segment must also be inline. Otherwise False.

Return type:

bool

length() float

Return the length of this line segment.

midpoint() P

The midpoint of this line segment.

mu(p: Sequence[float]) float

Unit distance of colinear point.

The unit distance from the first end point of this line segment to the specified collinear point. It is assumed that the point is collinear, but this is not checked.

normal_projection(p: P) float

Unit distance to normal projection of point.

The unit distance mu from this segment’s first point that corresponds to the projection of the specified point on to this line.

Parameters:

p – point to project on to line

Returns:

A value between 0.0 and 1.0 if the projection lies between the segment endpoints. The return value will be < 0.0 if the projection lies south of the first point, and > 1.0 if it lies north of the second point.

normal_projection_point(p: Sequence[float], segment: bool = False) P

Normal projection of point to line.

Parameters:
  • p – point to project on to line

  • segment – if True and if the point projection lies outside the two end points that define this line segment then return the closest endpoint. Default is False.

Returns:

The point on this line segment that corresponds to the projection of the specified point.

offset(distance: float) Line

Offset of this line segment.

Parameters:

distance – The distance to offset the line by.

Returns:

A line segment parallel to this one and offset by distance. If offset is < 0 the offset line will be to the right of this line, otherwise to the left. If offset is zero or the line segment length is zero then this line is returned.

property p1: P

The start point of this line segment.

property p2: P

The end point of this line segment.

path_reversed() Line

Line segment with start and end points reversed.

point_at(mu: float) P

Point at unit distance.

The point that is unit distance mu from this segment’s first point. The segment’s first point would be at mu=0.0 and the second point would be at mu=1.0.

Parameters:

mu – Unit distance from p1

Returns:

The point at mu

point_on_line(p: Sequence[float], segment: bool = False) bool

True if the point lies on the line defined by this segment.

Parameters:
  • p – the point to test

  • segment – If True, the point must be collinear AND lie between the two endpoints. Default is False.

same_side(pt1: P, pt2: P) bool

True if the given points lie on the same side of this line.

shift(amount: float) Line

Shift this segment forward or backwards by amount.

Forward means shift in the direction P1->P2.

Parameters:

amount – The distance to shift the line. If amount is less than zero the segment will be shifted backwards.

Returns:

A copy of this Line shifted by the specified amount.

slope() float

Return the slope of this line.

If the line is vertical a NaN is returned.

slope_intercept() tuple[float, float]

The slope-intercept equation for this line.

Where the equation is of the form: y = mx + b, where m is the slope and b is the y intercept.

Returns:

Slope and intercept as 2-tuple (m, b)

start_tangent_angle() float

The angle of this line segment in radians.

subdivide(mu: float) tuple[Line, Line]

Subdivide this line.

Creates two lines at the given unit distance from the start point.

Parameters:

mu – location of subdivision, where 0.0 < mu < 1.0

Returns:

A tuple containing two Lines.

to_polar() tuple[float, float]

Convert this line segment to polar coordinates.

Returns:

A tuple containing (length, angle)

to_svg_path(scale: float = 1, add_prefix: bool = True, add_move: bool = False) str

Line to SVG path string.

Parameters:
  • scale – Scale factor. Default is 1.

  • add_prefix – Prefix with the command prefix if True.

  • add_move – Prefix with M command if True.

Returns:

A string with the SVG path ‘d’ attribute that corresponds with this line.

transform(matrix: TMatrix) Line

A copy of this line with the transform matrix applied to it.

which_side(p: Sequence[float], inline: bool = False) int

Determine which side of this line a point lies.

Parameters:
  • p – Point to test

  • inline – If True return 0 if the point is inline. Default is False.

Returns:

1 if the point lies to the left of this line else -1. If inline is True and the point is inline then 0.

which_side_angle(angle: float, inline: bool = False) int

Find which side of line determined by angle.

Determine which side of this line lies a vector from the second end point with the specified direction angle.

Parameters:
  • angle – Angle in radians of the vector

  • inline – If True return 0 if the point is inline.

Returns:

1 if the vector direction is to the left of this line else -1. If inline is True and the point is inline then 0.