geom2d.box

Basic 2D bounding box geometry.

class geom2d.box.Box(p1: TBox | TPoint, p2: TPoint | None = None)

Bounding box.

Two dimensional immutable rectangle defined by two points, the lower left corner and the upper right corner respectively.

The sides are always assumed to be aligned with the X and Y axes.

Useful as clipping rectangle or bounding box.

all_points_inside(points: Iterable[TPoint]) bool

Return True if the given set of points lie inside this rectangle.

property bottomright: P

The bottom right corner of the box rectangle.

bounding_box() Box

Bounding box - self.

buffered(distance: float) Box

Expand or shrink box boundaries by distance.

Parameters:

distance – The distance to offset. The box will shrink if the distance is negative.

Returns:

A copy of this box with it’s boundaries expanded or shrunk by the specified distance. Also known as buffering.

property center: P

Return the center point of this rectangle.

clip_arc(_arc: Arc) Arc | None

Use this Box to clip an Arc.

If the given circular arc is clipped by this rectangle then return a new arc with clipped end-points.

This only returns a single clipped arc even if the arc could be clipped into two sub-arcs… For now this is considered a pathological condition.

Parameters:

arc – The arc segment to clip.

Returns:

A new clipped arc or None if the arc segment is entirely outside this clipping rectangle. If the arc segment is entirely within the rectangle this returns the same (unclipped) arc segment.

clip_line(ln: Sequence[Sequence[float]]) Line | None

Use this box to clip a line segment.

If the given line segment is clipped by this rectangle then return a new line segment with clipped end-points.

If the line segment is entirely within the rectangle this returns the same (unclipped) line segment.

If the line segment is entirely outside the rectangle this returns None.

Uses the Liang-Barsky line clipping algorithm. Translated C++ code from: http://hinjang.com/articles/04.html

Parameters:

ln – The line segment to clip.

Returns:

A new clipped line segment or None if the segment is outside this clipping rectangle.

clip_line_stdeq(a: float, b: float, c: float) Line | None

Clip a line defined by the standard form equation ax + by = c.

The endpoints of the line will lie on the box edges.

The clipped line endpoints will be oriented bottom->top, left->right

Returns:

A Line segment or None if no intersection.

diagonal() float

Length of diagonal.

static from_path(path: Iterable[tuple[tuple[float, ...]]]) Box | None

Create a Box from the bounding box of path segments.

This does not compute the actual bounding box of Arc or Bezier segments, just the naive box around the endpoints. TODO: compute actual bounding box.

Returns:

A geom.Box or None if there are zero segments.

static from_points(points: Iterable[TPoint]) Box

Create a Box from the bounding box of the given points.

Returns:

A geom.Box or None if there are zero points.

property height: float

Height of rectangle. (along Y axis).

intersection(other: Sequence[Sequence[float]]) Box | None

The intersection of this Box rectangle and another.

Returns None if the rectangles do not intersect.

line_inside(ln: Sequence[Sequence[float]]) bool

True if the line segment is inside this rectangle.

property p1: P

The lower left corner of the box rectangle.

property p2: P

The upper right corner of the box rectangle.

point_inside(p: Sequence[float]) bool

True if the point is inside this rectangle.

start_tangent_angle() float

Tangent at start point.

The angle in radians of a line tangent to this shape beginning at the first point. It’s pretty obvious this will always be PI/2…

This is just to provide an orthogonal interface for geometric shapes…

The corner point order for rectangles is clockwise from lower left.

property topleft: P

The upper left corner of the box rectangle.

transform(matrix: TMatrix) Box

Apply transform to this Box.

Note: rotations just scale since a Box is always aligned to

the X and Y axes.

union(other: Sequence[Sequence[float]]) Box

Return a Box that is the union of this rectangle and another.

vertices() tuple[P, P, P, P]

Get the four vertices of the box as a tuple of four points.

property width: float

Width of rectangle. (along X axis).

property xmax: float

Maximum X value of bounding box.

property xmin: float

Minimum X value of bounding box.

property ymax: float

Maximum X value of bounding box.

property ymin: float

Minimum Y value of bounding box.

geom2d.box.bounding_box(points: Iterable[TPoint]) Box

Simple bounding box of a collection of points.

Parameters:

points – an iterable collection of point 2-tuples (x,y).