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:
- property area¶
Calculate the area of the bounding box.
- Returns:
Area (width × height).
- Return type:
- property h¶
Get the height of the bounding box.
- Returns:
Height of the bounding box.
- Return type:
- property ll¶
Get the lower-left corner of the bounding box.
- Returns:
Lower-left corner point.
- Return type:
- property lr¶
Get the lower-right corner of the bounding box.
- Returns:
Lower-right corner point.
- Return type:
- round()[source]¶
Round the bounding box limits to integers.
- Returns:
A new bounding box with rounded coordinates.
- Return type:
- 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.
- property ul¶
Get the upper-left corner of the bounding box.
- Returns:
Upper-left corner point.
- Return type:
- property ur¶
Get the upper-right corner of the bounding box.
- Returns:
Upper-right corner point.
- Return type:
- 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.
- property magnitude¶
Calculate the distance of the point from origin (vector length).
- Returns:
The Euclidean distance from origin.
- Return type:
- property norm¶
Calculate a unit vector in the same direction.
- Returns:
Normalized vector with length 1, or (0,0) if zero length.
- Return type:
- round()[source]¶
Round coordinates to nearest integers.
- Returns:
New point with rounded coordinates.
- Return type:
- 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.
- 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:
- 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.
- 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:
- 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:
- 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:
Examples
>>> tx = Tx.from_symtx("RH") # Rotate right, then flip horizontally
- 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:
- property origin¶
Get the translation component of the transformation.
- Returns:
A Point object representing the (dx, dy) translation.
- Return type: