pygeometry2d

class pygeometry2d.geometry.Arc(center, radius, start_angle, end_angle)[source]

Bases: object

A class representing a 2D arc defined by a center, radius, and angles.

property diameter: float
discretize(number_of_segments)[source]

Discretizes the arc into a list of points.

Return type:

list[XY]

Parameters:

number_of_segments (int) – The number of segments to divide the arc into.

Returns:

The list of points representing the arc.

Return type:

list[XY]

intersection(line)[source]

Finds the intersection points between the arc and a line.

Return type:

list[XY]

Parameters:

line (Line) – The line to intersect with.

Returns:

The list of intersection points.

Return type:

list[XY]

is_point_on_edge(point)[source]

Checks if a point lies on the edge of the arc.

Return type:

bool

Parameters:

point (XY) – The point to check.

Returns:

True if the point is on the edge, False otherwise.

Return type:

bool

property length: float
class pygeometry2d.geometry.BoundingBox(min_point, max_point)[source]

Bases: object

A class representing a 2D bounding box defined by two corner points.

is_point_inside(point)[source]

Checks whether a point lies inside (or on the boundary of) the bounding box, considering a small tolerance defined by _geom_fuzz.

Return type:

bool

Parameters:

point (XY) – The point to test.

Returns:

True if the point is inside or on the edges within tolerance.

Return type:

bool

property mid: XY

Returns the midpoint of the bounding box.

Returns:

The midpoint.

Return type:

XY

to_outline()[source]

Converts the bounding box to a line representing its outline.

Return type:

Line

Returns:

The outline of the bounding box.

Return type:

Line

class pygeometry2d.geometry.Circle(center, diameter)[source]

Bases: Arc

A class representing a 2D circle, which is a special case of an arc.

class pygeometry2d.geometry.GeomUtils[source]

Bases: object

A utility class for geometric operations.

static angle_to_nearest_orthogonal_angle(angle)[source]

Finds the nearest orthogonal angle to a given angle.

Return type:

float

Parameters:

angle (float) – The angle in radians.

Returns:

The nearest orthogonal angle.

Return type:

float

static angle_to_vector(angle)[source]

Converts an angle to a unit vector.

Return type:

XY

Parameters:

angle (float) – The angle in radians.

Returns:

The unit vector.

Return type:

XY

static arc_by_3_points(point1, point2, point3)[source]

Computes the arc passing through three points. The points must be in order.

Return type:

tuple[XY, float, float, float] | None

Parameters:
  • point1 (XY) – The first point.

  • point2 (XY) – The second point.

  • point3 (XY) – The third point.

Returns:

The center, radius, start angle, and end angle of the arc, or None if the points are collinear.

Return type:

tuple[XY, float, float, float] | None

static boundary(points)[source]

Computes the convex hull of a set of points.

Return type:

Polyline

Parameters:

points (list[XY]) – The list of points.

Returns:

The convex hull as a polyline.

Return type:

Polyline

static circle_by_3_points(point1, point2, point3)[source]

Computes the circle passing through three points.

Return type:

tuple[XY, float] | None

Parameters:
  • point1 (XY) – The first point.

  • point2 (XY) – The second point.

  • point3 (XY) – The third point.

Returns:

The center and radius of the circle, or None if the points are collinear.

Return type:

tuple[XY, float] | None

static deg_to_rad(angle)[source]

Converts an angle from degrees to radians.

Return type:

float

Parameters:

angle (float) – The angle in degrees.

Returns:

The angle in radians.

Return type:

float

static get_min_max_point(point_list)[source]

Computes the bounding box of a list of points.

Return type:

BoundingBox

Parameters:

point_list (list[XY]) – The list of points.

Returns:

The bounding box.

Return type:

BoundingBox

static has_same_direction(angle1, angle2)[source]

Checks if two angles have the same direction.

Return type:

bool

Parameters:
  • angle1 (float) – The first angle in radians.

  • angle2 (float) – The second angle in radians.

Returns:

True if the angles have the same direction, False otherwise.

Return type:

bool

static is_horizontal(angle)[source]

Checks if an angle is horizontal.

Return type:

bool

Parameters:

angle (float) – The angle in radians.

Returns:

True if the angle is horizontal, False otherwise.

Return type:

bool

static is_ortho(angle)[source]

Checks if an angle is either horizontal or vertical.

Return type:

bool

Parameters:

angle (float) – The angle in radians.

Returns:

True if the angle is horizontal or vertical, False otherwise.

Return type:

bool

static is_vertical(angle)[source]

Checks if an angle is vertical.

Return type:

bool

Parameters:

angle (float) – The angle in radians.

Returns:

True if the angle is vertical, False otherwise.

Return type:

bool

static join(lines)[source]

Joins a list of polylines and lines into a list of connected polylines.

Return type:

list[Polyline]

Parameters:

lines (list[Polyline | Line]) – The list of polylines and lines.

Returns:

The list of connected polylines.

Return type:

list[Polyline]

static normalize_angle(angle)[source]

Normalizes an angle to the range [0, 2π).

Return type:

float

Parameters:

angle (float) – The angle in radians.

Returns:

The normalized angle.

Return type:

float

static optimize_segments(segments)[source]

Optimizes a list of line segments by merging consecutive segments.

Return type:

list[Line]

Parameters:

segments (list[Line]) – The list of line segments.

Returns:

The optimized list of line segments.

Return type:

list[Line]

static rad_to_deg(angle)[source]

Converts an angle from radians to degrees.

Return type:

float

Parameters:

angle (float) – The angle in radians.

Returns:

The angle in degrees.

Return type:

float

class pygeometry2d.geometry.Line(x0, y0, x1=None, y1=None, is_unbound=False)[source]

Bases: object

A class representing a 2D line segment or an infinite line.

property angle: float

Returns the normalized angle of the line in radians.

Returns:

The angle in radians.

Return type:

float

static basis_x()[source]

Returns the x-axis as a line.

Return type:

Line

Returns:

The x-axis line.

Return type:

Line

static basis_y()[source]

Returns the y-axis as a line.

Return type:

Line

Returns:

The y-axis line.

Return type:

Line

discretize(number_of_segments)[source]

Discretizes the line into a list of points.

Return type:

list[XY]

Parameters:

number_of_segments (int) – The number of segments to divide the line into.

Returns:

The list of points representing the line.

Return type:

list[XY]

distance(point)[source]

Returns the shortest distance from the point to the line.

Return type:

float

Parameters:

point (XY) – The point to measure the distance from.

Returns:

The distance from the point to the line.

Return type:

float

property general_equation_coefficients: tuple[float, float, float]

Returns the coefficients of the general equation of the line (Ax + By + C = 0).

Returns:

The coefficients (A, B, C).

Return type:

tuple[float, float, float]

has_same_direction(other)[source]

Checks if the line has the same direction as another line or vector.

Return type:

bool

Parameters:

other (XY | Line | Any) – The other line or vector.

Returns:

True if the directions are the same, False otherwise.

Return type:

bool

intersection(other_line, extend_segments_to_infinity=False)[source]

Finds the intersection point between two lines.

Return type:

XY | None

Parameters:
  • other_line (Line) – The other line to intersect with.

  • extend_segments_to_infinity (bool) – Whether to treat the lines as infinite. Defaults to False.

Returns:

The intersection point, or None if no intersection exists.

Return type:

XY | None

property inverted_angle: float

Returns the inverted angle of the line in radians.

Returns:

The inverted angle in radians.

Return type:

float

is_consecutive(other_line)[source]

Checks if the end of this line is the start of another line.

Return type:

bool

Parameters:

other_line (Line) – The other line to check.

Returns:

True if the lines are consecutive, False otherwise.

Return type:

bool

property is_horizontal: bool

Checks if the line is horizontal.

Returns:

True if the line is horizontal, False otherwise.

Return type:

bool

property is_ortho: bool

Checks if the line is either horizontal or vertical.

Returns:

True if the line is horizontal or vertical, False otherwise.

Return type:

bool

is_point_above(point)[source]

Checks if a point is above the line.

Return type:

bool

Parameters:

point (XY) – The point to check.

Returns:

True if the point is above the line, False otherwise.

Return type:

bool

is_point_in(point)[source]

Checks if a point lies on the line segment.

Return type:

bool

Parameters:

point (XY) – The point to check.

Returns:

True if the point is on the line segment, False otherwise.

Return type:

bool

property is_vertical: bool

Checks if the line is vertical.

Returns:

True if the line is vertical, False otherwise.

Return type:

bool

property length: float

Returns the length of the line.

Returns:

The length of the line.

Return type:

float

property mid: XY

Returns the midpoint of the line.

Returns:

The midpoint.

Return type:

XY

mirror(point)[source]

Returns the mirror image of a point across the line.

Return type:

XY

Parameters:

point (XY) – The point to mirror.

Returns:

The mirrored point.

Return type:

XY

offset(offset)[source]

Returns a new line offset by a given distance.

Return type:

Line

Parameters:

offset (float) – The distance to offset the line.

Returns:

The offset line.

Return type:

Line

perpendicular()[source]

Returns a perpendicular line at the origin.

Return type:

Line

Returns:

The perpendicular line.

Return type:

Line

property reduced_equation_coefficients: tuple[float, float]

Returns the coefficients of the reduced equation of the line (y = mx + b).

Returns:

The coefficients (m, b).

Return type:

tuple[float, float]

reversed()[source]

Returns a reversed version of the line.

Return type:

Line

Returns:

The reversed line.

Return type:

Line

to_points()[source]

Returns the start and end points of the line.

Return type:

list[XY]

Returns:

The start and end points.

Return type:

list[XY]

to_polyline()[source]

Converts the line to a polyline.

Return type:

Polyline

Returns:

The polyline representation of the line.

Return type:

Polyline

to_readable_direction()[source]

Returns a line with a consistent direction for readability.

Return type:

Line

Returns:

The line with a consistent direction.

Return type:

Line

to_unbound()[source]

Converts the line segment to an infinite line.

Return type:

Line

Returns:

The infinite line.

Return type:

Line

class pygeometry2d.geometry.Polyline(points)[source]

Bases: object

A class representing a polyline, which is a sequence of connected line segments.

property area: float

Returns the absolute area of the polyline.

Returns:

The absolute area.

Return type:

float

center()[source]

Returns the centroid of the polyline.

Return type:

XY

Returns:

The centroid.

Return type:

XY

close()[source]

Closes the polyline by adding the first point at the end if it’s not closed yet.

Return type:

Polyline

Returns:

The closed polyline.

Return type:

Polyline

closed()[source]

Returns a closed copy of the polyline.

Return type:

Polyline

Returns:

The closed polyline.

Return type:

Polyline

copy()[source]

Returns a copy of the polyline.

Return type:

Polyline

Returns:

A copy of the polyline.

Return type:

Polyline

enclosing_polyline(offset, end_point_offset=None)[source]

Returns a polyline that encloses the original polyline with a given offset.

Return type:

Polyline | None

Parameters:
  • offset (float) – The offset distance.

  • end_point_offset (float | None) – The offset for the end points. Defaults to the main offset.

Returns:

The enclosing polyline, or None if the polyline has fewer than 2 points.

Return type:

Polyline | None

property end: XY

Returns the end point of the polyline.

Returns:

The end point.

Return type:

XY

intersection(geometry_object)[source]

Finds the intersection points between the polyline and another geometry object.

Return type:

list[XY]

Parameters:

geometry_object (Line | Polyline | Arc) – The geometry object to intersect with.

Returns:

The list of intersection points.

Return type:

list[XY]

property is_clockwise: bool

Checks if the polyline is oriented clockwise.

Returns:

True if clockwise, False otherwise.

Return type:

bool

property is_closed: bool

Checks if the polyline is closed.

Returns:

True if the polyline is closed, False otherwise.

Return type:

bool

property is_counter_clockwise: bool

Checks if the polyline is oriented counter-clockwise.

Returns:

True if counter-clockwise, False otherwise.

Return type:

bool

is_equivalent(other)[source]

Checks if two polylines are equivalent.

Return type:

bool

Parameters:

other (Polyline) – The other polyline to compare.

Returns:

True if the polylines are equivalent, False otherwise.

Return type:

bool

is_point_in_edge(point)[source]

Checks if a point lies on any edge of the polyline.

Return type:

bool

Parameters:

point (XY) – The point to check.

Returns:

True if the point is on an edge, False otherwise.

Return type:

bool

is_point_inside(point)[source]

Checks if a point lies inside or lies on any edge of the polyline.

Return type:

bool

Parameters:

point (XY) – The point to check.

Returns:

True if the point is inside, False otherwise.

Return type:

bool

is_scaled_equivalent(other)[source]

Checks if two polylines are equivalent after scaling.

Return type:

bool

Parameters:

other (Polyline) – The other polyline to compare.

Returns:

True if the polylines are scaled equivalents, False otherwise.

Return type:

bool

join(line)[source]

Joins the polyline with another line or polyline. If the ends do not match, return the polyline without modification.

Return type:

Polyline

Parameters:

line (Line | Polyline) – The line or polyline to join with.

Returns:

The joined polyline.

Return type:

Polyline

property length: float

Returns the total length of the polyline.

Returns:

The total length.

Return type:

float

moved(vector)[source]

Returns a copy of the moved polyline by a given vector.

Return type:

Polyline

Parameters:

vector (XY) – The vector to move by.

Returns:

The moved polyline.

Return type:

Polyline

property num_points: int

Returns the number of points in the polyline.

Returns:

The number of points.

Return type:

int

offset(offset)[source]

Returns a new polyline offset by a given distance.

Return type:

Polyline

Parameters:

offset (float) – The distance to offset.

Returns:

The offset polyline.

Return type:

Polyline

reverse()[source]

Reverses the order of points in the polyline.

Return type:

Polyline

Returns:

The reversed polyline.

Return type:

Polyline

reversed()[source]

Returns a reversed copy of the polyline.

Return type:

Polyline

Returns:

The reversed polyline.

Return type:

Polyline

rotated(center, angle)[source]

Returns a copy of the rotated polyline around a center by a given angle.

Return type:

Polyline

Parameters:
  • center (XY) – The center of rotation.

  • angle (float) – The angle in radians.

Returns:

The rotated polyline.

Return type:

Polyline

scaled(scale_factor, scale_point=None)[source]

Returns a scaled copy of the polyline by a given factor.

Return type:

Polyline

Parameters:
  • scale_factor (float) – The scaling factor.

  • scale_point (XY | None) – The point to scale around. Defaults to the origin.

Returns:

The scaled polyline.

Return type:

Polyline

property signed_area: float

Returns the signed area of the polyline.

Returns:

The signed area.

Return type:

float

property start: XY

Returns the start point of the polyline.

Returns:

The start point.

Return type:

XY

to_points()[source]

Returns a copy of the points in the polyline.

Return type:

list[XY]

Returns:

The list of points.

Return type:

list[XY]

to_segments()[source]

Converts the polyline to a list of line segments.

Return type:

list[Line]

Returns:

The list of line segments.

Return type:

list[Line]

class pygeometry2d.geometry.Rectangle(corner1, corner2)[source]

Bases: Polyline

A class representing a rectangle, which is a special case of a polyline.

property center: XY

Returns the centroid of the polyline.

Returns:

The centroid.

Return type:

XY

discretize(horizontal_partitions, vertical_partitions)[source]

Discretizes the rectangle into smaller rectangles.

Return type:

list[Rectangle]

Parameters:
  • horizontal_partitions (int) – The number of horizontal partitions.

  • vertical_partitions (int) – The number of vertical partitions.

Returns:

The list of smaller rectangles.

Return type:

list[Rectangle]

class pygeometry2d.geometry.XY(x, y)[source]

Bases: object

A class representing a 2D point or 2D vector with x and y coordinates.

property angle: float

Returns the normalized angle of the point vector in radians.

Returns:

The angle in radians.

Return type:

float

distance(other_point)[source]

Returns the Euclidean distance between two points.

Return type:

float

Parameters:

other_point (XY) – The other point.

Returns:

The distance between the points.

Return type:

float

dot_product(v2)[source]

Returns the dot product of two vectors.

Return type:

float

Parameters:

v2 (XY) – The other vector.

Returns:

The dot product.

Return type:

float

property length: float

Returns the Euclidean length of the point vector.

Returns:

The length of the vector.

Return type:

float

static mid(point1, point2)[source]

Returns the midpoint between two points.

Return type:

XY

Parameters:
  • point1 (XY) – The first point.

  • point2 (XY) – The second point.

Returns:

The midpoint between the two points.

Return type:

XY

normalize()[source]

Returns a normalized version of the point vector.

Return type:

XY

Returns:

The normalized vector.

Return type:

XY

offset(delta_x, delta_y)[source]

Returns a new point offset by the given deltas.

Return type:

XY

Parameters:
  • delta_x (float) – The x offset.

  • delta_y (float) – The y offset.

Returns:

The offset point.

Return type:

XY

perpendicular()[source]

Returns a perpendicular vector.

Return type:

XY

Returns:

The perpendicular vector.

Return type:

XY

rotate(angle, center=None)[source]

Rotates the point around a center by a given angle.

Return type:

XY

Parameters:
  • angle (float) – The angle in radians.

  • center (XY | None) – The center of rotation. Defaults to the origin.

Returns:

The rotated point.

Return type:

XY

property x: float
property y: float
static zero()[source]
Return type:

XY

pygeometry2d.geometry.set_precision(decimal_precision)[source]

Sets the global decimal precision for geometric calculations.

Return type:

None

Parameters:

decimal_precision (int) – The number of decimal places for precision.

pygeometry2d.geometry.set_temp_precision(decimal_precision)[source]

Temporarily sets the global precision within a context.

Return type:

Generator[None, None, None]

Parameters:

decimal_precision (int) – The number of decimal places to use within the context.

Yields:

None – Restores the previous precision after execution.