skidl.geometry module

Geometric primitives and transformations for SKiDL.

This module provides classes and functions to handle geometric operations needed for positioning and transforming components. It includes support for points/vectors, transformation matrices, and unit conversions between millimeters and thousandths-of-inch (mils).

class skidl.geometry.BBox(*pts)[source]

Bases: object

A bounding box defined by minimum and maximum points.

BBox represents a rectangular area that can contain points or other bounding boxes. It provides methods for combining bounding boxes, testing if points are inside, and calculating intersections.

Parameters:

*pts – One or more Point objects defining the initial bounding box.

add(*objs)[source]

Update the bounding box to include multiple points and/or bounding boxes.

Parameters:

*objs – Points or BBoxes to include in this bounding box.

Returns:

The updated bounding box (self).

Return type:

BBox

property area

Calculate the area of the bounding box.

Returns:

Area (width × height).

Return type:

float

property ctr

Get the center point of the bounding box.

Returns:

Center point.

Return type:

Point

property h

Get the height of the bounding box.

Returns:

Height of the bounding box.

Return type:

float

intersection(bbox)[source]

Calculate the intersection of this bounding box with another.

Parameters:

bbox (BBox) – Another bounding box.

Returns:

A new bounding box representing the intersection,

or None if there is no intersection.

Return type:

BBox or None

intersects(bbox)[source]

Check if this bounding box intersects with another.

Parameters:

bbox (BBox) – Another bounding box to check for intersection.

Returns:

True if the bounding boxes intersect.

Return type:

bool

is_inside(pt)[source]

Check if a point is inside or on the boundary of this bounding box.

Parameters:

pt (Point) – The point to check.

Returns:

True if the point is inside or on the boundary.

Return type:

bool

property ll

Get the lower-left corner of the bounding box.

Returns:

Lower-left corner point.

Return type:

Point

property lr

Get the lower-right corner of the bounding box.

Returns:

Lower-right corner point.

Return type:

Point

resize(vector)[source]

Expand or contract the bounding box by a given amount in all directions.

Parameters:

vector (Point) – Amount to expand by (positive values) or contract by (negative values) in x and y directions.

Returns:

A new resized bounding box.

Return type:

BBox

round()[source]

Round the bounding box limits to integers.

Returns:

A new bounding box with rounded coordinates.

Return type:

BBox

snap_resize(grid_spacing)[source]

Resize the bounding box to align min and max points to a grid.

This expands the bounding box outward so that its corners align with the given grid spacing.

Parameters:

grid_spacing (float) – Grid spacing to align to.

Returns:

A new bounding box with grid-aligned corners.

Return type:

BBox

property ul

Get the upper-left corner of the bounding box.

Returns:

Upper-left corner point.

Return type:

Point

property ur

Get the upper-right corner of the bounding box.

Returns:

Upper-right corner point.

Return type:

Point

property w

Return the bounding box width.

class skidl.geometry.Point(x, y)[source]

Bases: object

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

The Point class represents both points in space and vectors for geometric operations. It supports various arithmetic operations and can be transformed using the Tx transformation matrix.

Parameters:
  • x (float) – The x-coordinate.

  • y (float) – The y-coordinate.

flip_xy()[source]

Swap the x and y coordinates in place.

property magnitude

Calculate the distance of the point from origin (vector length).

Returns:

The Euclidean distance from origin.

Return type:

float

mask(msk)[source]

Apply a binary mask to the coordinates.

Parameters:

msk (list or tuple) – A pair of values to multiply with x and y.

Returns:

New point with masked coordinates.

Return type:

Point

max(pt)[source]

Create a new point using the maximum x and y from two points.

Parameters:

pt (Point) – Another point to compare with.

Returns:

New point with maximum x and y values.

Return type:

Point

min(pt)[source]

Create a new point using the minimum x and y from two points.

Parameters:

pt (Point) – Another point to compare with.

Returns:

New point with minimum x and y values.

Return type:

Point

property norm

Calculate a unit vector in the same direction.

Returns:

Normalized vector with length 1, or (0,0) if zero length.

Return type:

Point

round()[source]

Round coordinates to nearest integers.

Returns:

New point with rounded coordinates.

Return type:

Point

snap(grid_spacing)[source]

Snap point coordinates to a grid.

Parameters:

grid_spacing (float) – Grid interval to snap to.

Returns:

New point with coordinates snapped to the grid.

Return type:

Point

xprod(pt)[source]

Calculate the cross product of this vector and another.

For 2D vectors, the cross product returns a scalar representing the z-component of the 3D cross product.

Parameters:

pt (Point) – Another point/vector.

Returns:

The cross product value.

Return type:

float

class skidl.geometry.Segment(p1, p2)[source]

Bases: object

A line segment between two points.

Represents a straight line segment with endpoints p1 and p2.

Parameters:
  • p1 (Point) – First endpoint.

  • p2 (Point) – Second endpoint.

flip_xy()[source]

Swap the x and y coordinates of both endpoints.

intersects(other)[source]

Check if this segment intersects with another segment.

Note: This method is not fully implemented and will raise an error.

Parameters:

other (Segment) – Another segment to check for intersection.

Raises:

NotImplementedError – This method is not fully implemented.

round()[source]

Round the segment endpoints to integers.

Returns:

A new segment with rounded endpoint coordinates.

Return type:

Segment

shadows(other)[source]

Check if two segments overlap when projected onto axes.

This tests if the segments would overlap when viewed from above or the side, even if they don’t physically intersect.

Parameters:

other (Segment) – Another segment to check.

Returns:

True if segments shadow (overlap) each other on x or y axis.

Return type:

bool

class skidl.geometry.Tx(a=1, b=0, c=0, d=1, dx=0, dy=0)[source]

Bases: object

A 2D transformation matrix for geometric operations.

This class implements a 3x3 transformation matrix for 2D operations like rotation, scaling, flipping, and translation. The matrix has the following structure:

[ a b 0 ] [ c d 0 ] [ dx dy 1 ]

Where the transformed coordinates are calculated as: x’ = a*x + c*y + dx y’ = b*x + d*y + dy

Parameters:
  • a (float, optional) – Scaling/rotation factor for x coordinate. Defaults to 1.

  • b (float, optional) – Rotation factor for y contribution to x. Defaults to 0.

  • c (float, optional) – Rotation factor for x contribution to y. Defaults to 0.

  • d (float, optional) – Scaling/rotation factor for y coordinate. Defaults to 1.

  • dx (float, optional) – Translation in x direction. Defaults to 0.

  • dy (float, optional) – Translation in y direction. Defaults to 0.

analyze_transform()[source]

Analyze a transformation matrix to extract rotation angle and mirroring components.

This function removes any translation from the transformation matrix and then computes the equivalent rotation angle and identifies any mirroring in the X or Y axis.

Parameters:

tx (Tx) – A transformation matrix to analyze.

Returns:

(rotation_degrees, mirror_x, mirror_y) where:
  • rotation_degrees (float): The rotation angle in degrees (0-360)

  • mirror_x (bool): True if there’s mirroring in the X axis

  • mirror_y (bool): True if there’s mirroring in the Y axis

Return type:

tuple

Examples

>>> tx = Tx.rot_90cw()  # 90° clockwise rotation
>>> angle, mx, my = analyze_transform(tx)
>>> angle
-90.0
>>> tx = Tx(a=-1)  # Mirror in X
>>> angle, mx, my = analyze_transform(tx)
>>> mx
True
flip_x()[source]

Return a new Tx with a horizontal flip (mirror across y-axis).

Returns:

A new transformation matrix with the flip applied.

Return type:

Tx

flip_y()[source]

Return a new Tx with a vertical flip (mirror across x-axis).

Returns:

A new transformation matrix with the flip applied.

Return type:

Tx

classmethod from_symtx(symtx)[source]

Create a transformation matrix from a string of symbolic operations.

Parameters:

symtx (str) – A string of H, V, L, R operations that are applied in sequence left-to-right. H = horizontal flip, V = vertical flip, L = rotate 90° left (CCW), R = rotate 90° right (CW)

Returns:

A transformation matrix that implements the sequence of operations.

Return type:

Tx

Examples

>>> tx = Tx.from_symtx("RH")  # Rotate right, then flip horizontally
move(vec)[source]

Return a new Tx with an additional translation applied.

Parameters:

vec (Point) – The vector to translate by.

Returns:

A new transformation matrix with the translation applied.

Return type:

Tx

no_translate()[source]

Return a new Tx with the same rotation/scaling but no translation.

Returns:

A new transformation matrix with translation set to (0,0).

Return type:

Tx

property origin

Get the translation component of the transformation.

Returns:

A Point object representing the (dx, dy) translation.

Return type:

Point

rot(degs)[source]

Return a new Tx with a rotation by the given angle in degrees.

Parameters:

degs (float) – The rotation angle in degrees.

Returns:

A new transformation matrix with the rotation applied.

Return type:

Tx

rot_90cw()[source]

Return a new Tx with a 90-degree clockwise rotation applied.

Returns:

A new transformation matrix with the rotation applied.

Return type:

Tx

property scale

Get the scaling factor of the transformation.

This calculates the length of a unit vector after transformation.

Returns:

The scaling factor.

Return type:

float

skidl.geometry.Vector

alias of Point

skidl.geometry.to_mils(mm)[source]

Convert millimeters to thousandths-of-inch (mils).

Parameters:

mm (float) – Value in millimeters.

Returns:

Equivalent value in mils.

Return type:

float

skidl.geometry.to_mms(mils)[source]

Convert thousandths-of-inch (mils) to millimeters.

Parameters:

mils (float) – Value in mils.

Returns:

Equivalent value in millimeters.

Return type:

float