raygeo.geo

Various geometry shapes and operations Geometry types and operations for 2D/3D path data.
The central type is Geometry — a mutable sequence of drawing commands (move, line, arc, bezier) that represents one or more closed or open paths. Geometry supports construction (add_rect, add_circle, etc.), analysis (area, distance, bounding rect), and manipulation (transform, simplify, linearize, fit curves, grow/shrink, split, clip).
Shape sub-modules provide primitive-specific operations: arc bounding and intersection, bezier splitting and flattening, circle containment tests, polygon boolean algebra and offsetting, and line intersection.
Algorithm sub-modules provide higher-level geometric processing such as polyline simplification, smoothing, curve fitting, and Minkowski sums for toolpath generation.
Arc
A circular-arc cutting command.
center_offset
center_offset: tuple[float, float, float]
Centre offset from the start point (3D).
clockwise
clockwise: bool
Whether the arc is clockwise (computed from the normal).
end
end: tuple[float, float, float]
Endpoint of the arc in 3D space.
normal
normal: tuple[float, float, float]
Plane normal of the arc. A positive Z component means CCW in XY.
Bezier
A cubic-Bezier curve cutting command.
control1
control1: tuple[float, float, float]
First control point in 3D space.
control2
control2: tuple[float, float, float]
Second control point in 3D space.
end
end: tuple[float, float, float]
Endpoint of the curve in 3D space.
Geometry
A sequence of geometric commands (Move, Line, Arc, Bezier).
The primary building block for vector geometry in raygeo. Geometry objects can be constructed procedurally, parsed from SVG, or obtained by converting an ~raygeo.ops.Ops sequence.
data
data: list[Any]
The commands as a list of typed command objects.
last_move_to
last_move_to: tuple[float, float, float]
The coordinates of the last move-to command.
uniform_scalable
uniform_scalable: bool
Whether the geometry uses uniform scalable arcs.
arc_to()
arc_to(
x: float,
y: float,
i: float = 0.0,
j: float = 0.0,
clockwise: bool = True,
z: float = 0.0,
) -> Geometry
Draw an arc to the given coordinates.
| Parameter | Type | Description |
|---|---|---|
x | float | X coordinate. |
y | float | Y coordinate. |
i | float = 0.0 | I offset from current point to center. |
j | float = 0.0 | J offset from current point to center. |
clockwise | bool = True | Whether the arc is clockwise. |
z | float = 0.0 | Z coordinate (default 0.0). |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(1) time, O(1) space |
arc_to_as_bezier()
arc_to_as_bezier(
x: float,
y: float,
i: float,
j: float,
clockwise: bool = True,
z: float = 0.0,
) -> Geometry
Draw an arc, converting it to bezier curves.
| Parameter | Type | Description |
|---|---|---|
x | float | End X coordinate. |
y | float | End Y coordinate. |
i | float | I offset to center. |
j | float | J offset to center. |
clockwise | bool = True | Arc direction. |
z | float = 0.0 | End Z coordinate. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(1) time, O(1) space |
area()
area() -> float
Return the signed area of the geometry.
| Parameter | Type | Description |
|---|---|---|
| Returns | float | The signed area in mm². |
| Complexity | O(n) time, O(1) space |
bezier_to()
bezier_to(
x: float,
y: float,
c1x: float,
c1y: float,
c2x: float,
c2y: float,
*,
c1z: float = 0.0,
c2z: float = 0.0,
z: float = 0.0,
) -> Geometry
Draw a cubic bezier curve.
| Parameter | Type | Description |
|---|---|---|
x | float | End X coordinate. |
y | float | End Y coordinate. |
c1x | float | First control point X. |
c1y | float | First control point Y. |
c2x | float | Second control point X. |
c2y | float | Second control point Y. |
c1z | float | First control point Z (default 0.0). |
c2z | float | Second control point Z (default 0.0). |
z | float | End Z coordinate (default 0.0). |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(1) time, O(1) space |
cleanup()
cleanup(tolerance: float) -> Geometry
Remove duplicate segments from the geometry.
| Parameter | Type | Description |
|---|---|---|
tolerance | float | Maximum deviation for equality. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n log n) average time, O(n) space |
clear()
clear() -> Geometry
Remove all commands from the geometry.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(1) time, O(1) space |
close_all_contours()
close_all_contours() -> Geometry
Close all open contours in the geometry.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(n) space |
close_gaps()
close_gaps(tolerance: Optional[float] = None) -> Geometry
Close gaps between sub-paths.
| Parameter | Type | Description |
|---|---|---|
tolerance | Optional[float] = None | Max gap to close. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(n) space |
close_path()
close_path() -> Geometry
Close the current sub-path.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(1) time, O(1) space |
convert_arcs_to_beziers()
convert_arcs_to_beziers() -> None
Convert all Arc commands to Bezier curve approximations in-place.
After this call, the geometry will only contain Move, Line, and Bezier commands.
| Parameter | Type | Description |
|---|---|---|
| Returns | None | |
| Complexity | O(n) time, O(n) space where n = number of commands |

Overlay showing Bezier curves (with control points) closely matching the original arcs
copy()
copy() -> Geometry
Return a deep copy of this geometry.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | A deep copy of the geometry. |
| Complexity | O(n) time, O(n) space |
distance()
distance() -> float
Return the total path distance.
| Parameter | Type | Description |
|---|---|---|
| Returns | float | The total path distance in mm. |
| Complexity | O(n) time, O(1) space |
encloses()
encloses(other: Geometry) -> bool
Check if this geometry encloses another.
| Parameter | Type | Description |
|---|---|---|
other | Geometry | The potentially enclosed geometry. |
| Returns | bool | True if this geometry encloses the other. |
| Complexity | O(n log n) average time, O(n) space |
extend()
extend(other: Geometry) -> Geometry
Append another geometry's commands to this one.
| Parameter | Type | Description |
|---|---|---|
other | Geometry | The geometry to append. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(n) space |
filter()
filter(indices: set[int]) -> Geometry
Return a new Geometry containing only commands at the given indices.
| Parameter | Type | Description |
|---|---|---|
indices | set[int] | Set of command indices to keep. |
| Returns | Geometry | A new Geometry with the filtered commands. |
| Complexity | O(n) time, O(n) space |
filter_to_external_contours()
filter_to_external_contours() -> Geometry
Filter to only external (outermost) contours.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n log n) average time, O(n) space |
find_closest_point()
find_closest_point(
x: float,
y: float,
) -> Optional[tuple[int, float, tuple[float, float]]]
Find the closest point on the path to (x, y).
| Parameter | Type | Description |
|---|---|---|
x | float | X coordinate. |
y | float | Y coordinate. |
| Returns | Optional[tuple[int, float, tuple[float, float]]] | Tuple of (segment_index, t, point) or None. |
| Complexity | O(n) time, O(1) space |
fit_arcs()
fit_arcs(tolerance: float) -> Geometry
Fit arcs only to the linearized geometry.
| Parameter | Type | Description |
|---|---|---|
tolerance | float | Maximum deviation. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n log n) average time, O(n) space |
fit_curves()
fit_curves(
tolerance: float,
beziers: bool = True,
arcs: bool = True,
on_progress: Optional[Any] = None,
) -> Geometry
Fit curves (beziers and arcs) to the linearized geometry.
| Parameter | Type | Description |
|---|---|---|
tolerance | float | Maximum deviation. |
beziers | bool = True | Whether to fit bezier curves. |
arcs | bool = True | Whether to fit arcs. |
on_progress | Optional[Any] = None | Optional progress callback called with (current, total). |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n log n) average time, O(n) space |
flip_x()
flip_x() -> Geometry
Mirror the geometry along the X axis.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(1) space |
flip_y()
flip_y() -> Geometry
Mirror the geometry along the Y axis.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(1) space |
from_dict()
@classmethod from_dict(data: dict) -> Geometry
Create a Geometry from a dictionary.
| Parameter | Type | Description |
|---|---|---|
data | dict | A dictionary as produced by to_dict. |
| Returns | Geometry | A new Geometry instance. |
| Complexity | O(n) time, O(n) space |
from_points()
@classmethod from_points(points: Any, close: bool = True) -> Geometry
Create a Geometry from a sequence of points.
| Parameter | Type | Description |
|---|---|---|
points | Any | A sequence of (x, y) or (x, y, z) coordinate tuples. |
close | bool = True | Whether to close the path. |
| Returns | Geometry | A new Geometry instance. |
| Complexity | O(n) time, O(n) space |
get_command_at()
get_command_at(index: int) -> Optional[Any]
Get the command at the given index as a typed command object.
| Parameter | Type | Description |
|---|---|---|
index | int | Command index (negative returns None). |
| Returns | Optional[Any] | The typed command or None. |
| Complexity | O(1) time, O(1) space |
get_last_point()
get_last_point() -> tuple[float, float, float]
Get the last point in the geometry.
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[float, float, float] | The last point as (x, y, z), or (0, 0, 0) if empty. |
| Complexity | O(1) time, O(1) space |
get_outward_normal_at()
get_outward_normal_at(
segment_index: int,
t: float,
) -> Optional[tuple[float, float]]
Get the outward normal at parameter t on a segment.
| Parameter | Type | Description |
|---|---|---|
segment_index | int | Index of the segment. |
t | float | Parameter in [0, 1]. |
| Returns | Optional[tuple[float, float]] | Normal vector or None. |
| Complexity | O(1) time, O(1) space |
get_point_at()
get_point_at(
segment_index: int,
t: float,
) -> Optional[tuple[float, float, float]]
Get the point at parameter t on a segment.
| Parameter | Type | Description |
|---|---|---|
segment_index | int | Index of the segment. |
t | float | Parameter in [0, 1]. |
| Returns | Optional[tuple[float, float, float]] | The 3D point or None. |
| Complexity | O(1) time, O(1) space |
get_positions_at_distances()
get_positions_at_distances(
distances: Sequence[float],
) -> list[tuple[int, float, tuple[float, float]]]
Given a list of distances along the path, returns the corresponding (segment_index, t, point) for each distance.
Distances are clamped to [0, total_length].
| Parameter | Type | Description |
|---|---|---|
distances | Sequence[float] | List of distances along the path. |
| Returns | list[tuple[int, float, tuple[float, float]]] | List of (segment_index, t, (x, y)) tuples. |
| Complexity | O(n + m) time, O(m) space where n is the number of segments and m the number of distances |
get_tangent_at()
get_tangent_at(segment_index: int, t: float) -> Optional[tuple[float, float]]
Get the tangent vector at parameter t on a segment.
| Parameter | Type | Description |
|---|---|---|
segment_index | int | Index of the segment. |
t | float | Parameter in [0, 1]. |
| Returns | Optional[tuple[float, float]] | The normalized tangent vector or None. |
| Complexity | O(1) time, O(1) space |
get_typed_command_at()
get_typed_command_at(index: int) -> Move | Line | Arc | Bezier | None
Get the typed command at the given index.
| Parameter | Type | Description |
|---|---|---|
index | int | Command index. |
| Returns | Move | Line | Arc | Bezier | None | |
| Complexity | O(1) time, O(1) space |
get_valid_contours_data()
get_valid_contours_data() -> list[dict]
Get valid contour data from the geometry's contours.
| Parameter | Type | Description |
|---|---|---|
| Returns | list[dict] | List of dicts with keys "geo", "vertices", "is_closed", "original_index". |
| Complexity | O(n) time, O(n) space |
grow()
grow(amount: float) -> Geometry
Offset (grow/shrink) the geometry by the given amount.
| Parameter | Type | Description |
|---|---|---|
amount | float | Positive to grow, negative to shrink. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n log n) average time, O(n) space |
has_self_intersections()
has_self_intersections(fail_on_t_junction: bool = False) -> bool
Check if the geometry has self-intersections.
| Parameter | Type | Description |
|---|---|---|
fail_on_t_junction | bool = False | Whether to fail on T-junctions. |
| Returns | bool | True if the geometry has self-intersections. |
| Complexity | O(n²) worst-case time, O(1) space |
intersects_with()
intersects_with(other: Geometry) -> bool
Check if this geometry intersects with another.
| Parameter | Type | Description |
|---|---|---|
other | Geometry | The other geometry. |
| Returns | bool | True if the geometries intersect. |
| Complexity | O(n * m) worst-case time, O(1) space where n and m are the number of segments in each geometry |
is_closed()
is_closed(tolerance: float = 1e-06) -> bool
Check if the geometry forms a closed path.
| Parameter | Type | Description |
|---|---|---|
tolerance | float = 1e-06 | Max gap between start and end point. |
| Returns | bool | True if the geometry forms a closed path. |
| Complexity | O(n) time, O(1) space |
is_empty()
is_empty() -> bool
Check if the geometry has no commands.
| Parameter | Type | Description |
|---|---|---|
| Returns | bool | True if the geometry has no commands. |
| Complexity | O(1) time, O(1) space |
iter_commands()
iter_commands() -> list[Any]
Iterate over all commands as typed command objects.
| Parameter | Type | Description |
|---|---|---|
| Returns | list[Any] | List of typed command objects. |
| Complexity | O(n) time, O(n) space |
iter_typed_commands()
iter_typed_commands() -> list[Move | Line | Arc | Bezier]
Iterate over all commands as typed command objects.
| Parameter | Type | Description |
|---|---|---|
| Returns | list[Move | Line | Arc | Bezier] | |
| Complexity | O(n) time, O(n) space |
line_to()
line_to(x: float, y: float, z: float = 0.0) -> Geometry
Draw a line to the given coordinates.
| Parameter | Type | Description |
|---|---|---|
x | float | X coordinate. |
y | float | Y coordinate. |
z | float = 0.0 | Z coordinate (default 0.0). |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(1) time, O(1) space |
linearize()
linearize(tolerance: float) -> Geometry
Convert all curves to line segments.
| Parameter | Type | Description |
|---|---|---|
tolerance | float | Maximum deviation from curves. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(n) space |
map_to_frame()
map_to_frame(
origin: tuple[float, float],
p_width: tuple[float, float],
p_height: tuple[float, float],
anchor_y: Optional[float] = None,
stable_src_height: Optional[float] = None,
anchor_x: Optional[float] = None,
stable_src_width: Optional[float] = None,
) -> Geometry
Map the geometry into a rectangular frame.
| Parameter | Type | Description |
|---|---|---|
origin | tuple[float, float] | Frame origin (x, y). |
p_width | tuple[float, float] | Frame width vector. |
p_height | tuple[float, float] | Frame height vector. |
anchor_y | Optional[float] = None | Y anchor position. |
stable_src_height | Optional[float] = None | Stable source height for anchoring. |
anchor_x | Optional[float] = None | X anchor position. |
stable_src_width | Optional[float] = None | Stable source width for anchoring. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(n) space |
move_to()
move_to(x: float, y: float, z: float = 0.0) -> Geometry
Move the pen to the given coordinates.
| Parameter | Type | Description |
|---|---|---|
x | float | X coordinate. |
y | float | Y coordinate. |
z | float = 0.0 | Z coordinate (default 0.0). |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(1) time, O(1) space |
normalize_winding_orders()
normalize_winding_orders() -> Geometry
Normalize winding orders (outer CCW, inner CW) of all contours.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n log n) average time, O(n) space |
rect()
rect() -> tuple[float, float, float, float]
Return the bounding rectangle (x_min, y_min, x_max, y_max).
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[float, float, float, float] | (x_min, y_min, x_max, y_max). |
| Complexity | O(n) time, O(1) space |
remove_inner_edges()
remove_inner_edges() -> Geometry
Remove inner edges (shared between contours).
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(n) space |
reverse_contour()
reverse_contour() -> Geometry
Reverse the winding direction of all contours.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(n) space |
segment_bounds()
segment_bounds(index: int) -> Optional[tuple[float, float, float, float]]
Return the bounding box of a single segment at the given index. Returns None for Move commands or if the index is out of bounds.
| Parameter | Type | Description |
|---|---|---|
index | int | Segment index. |
| Returns | Optional[tuple[float, float, float, float]] | (x_min, y_min, x_max, y_max) or None. |
| Complexity | O(1) time, O(1) space |
segments()
segments() -> list[list[tuple[float, float, float]]]
Return the geometry split into segments of connected commands.
| Parameter | Type | Description |
|---|---|---|
| Returns | list[list[tuple[float, float, float]]] | List of segments, each a list of 3D points. |
| Complexity | O(n) time, O(n) space |
segments_in_frame()
segments_in_frame(x1: float, y1: float, x2: float, y2: float) -> list[int]
Return indices of all segments whose bounding box intersects the given rectangle. Excludes Move commands.
| Parameter | Type | Description |
|---|---|---|
x1 | float | First corner X. |
y1 | float | First corner Y. |
x2 | float | Second corner X. |
y2 | float | Second corner Y. |
| Returns | list[int] | List of segment indices. |
| Complexity | O(n) time, O(n) space |
simplify()
simplify(tolerance: float) -> Geometry
Simplify the geometry using Ramer-Douglas-Peucker.
| Parameter | Type | Description |
|---|---|---|
tolerance | float | Maximum deviation from original. |
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n log n) average time, O(n) space |
split_inner_and_outer_contours()
split_inner_and_outer_contours() -> tuple[list[Geometry], list[Geometry]]
Split contours into inner and outer groups.
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[list[Geometry], list[Geometry]] | Tuple of (inner_contours, outer_contours), each a list of Geometry. |
| Complexity | O(n log n) average time, O(n) space |
split_into_components()
split_into_components() -> list[Geometry]
Split the geometry into connected components.
| Parameter | Type | Description |
|---|---|---|
| Returns | list[Geometry] | List of Geometry objects, one per connected component. |
| Complexity | O(n log n) average time, O(n) space |
split_into_contours()
split_into_contours() -> list[Geometry]
Split the geometry into individual contours.
| Parameter | Type | Description |
|---|---|---|
| Returns | list[Geometry] | List of Geometry objects, one per contour. |
| Complexity | O(n) time, O(n) space |
to_dict()
to_dict() -> dict
Serialize the geometry to a dictionary.
| Parameter | Type | Description |
|---|---|---|
| Returns | dict | A dictionary representation of the geometry. |
| Complexity | O(n) time, O(n) space |
to_polygons()
to_polygons(tolerance: float = 0.01) -> list[list[tuple[float, float]]]
Convert the geometry to a list of polygons.
| Parameter | Type | Description |
|---|---|---|
tolerance | float = 0.01 | Max deviation for linearization. |
| Returns | list[list[tuple[float, float]]] | List of polygons, each a list of (x, y) vertices. |
| Complexity | O(n) time, O(n) space |
transform()
transform(matrix: Matrix | types.TransformMatrix) -> Geometry
Apply an affine transformation matrix.
| Parameter | Type | Description |
|---|---|---|
matrix | Matrix | types.TransformMatrix | A ~raygeo.geo.Matrix or a 4x4 matrix as list of lists. |
| Returns | Geometry | A new transformed Geometry. |
| Complexity | O(n) time, O(n) space |
upgrade_to_scalable()
upgrade_to_scalable() -> Geometry
Convert all arcs to bezier curves for uniform scaling.
| Parameter | Type | Description |
|---|---|---|
| Returns | Geometry | The geometry (for method chaining). |
| Complexity | O(n) time, O(n) space |
Line
A straight-line cutting command.
end
end: tuple[float, float, float]
Endpoint of the line in 3D space.
Matrix
A 3x3 affine transformation matrix for 2D graphics.
Provides an object-oriented interface for matrix operations, including translations, rotations, and scaling.
compose()
@classmethod
compose(
tx: float,
ty: float,
angle_deg: float,
sx: float,
sy: float,
skew_angle_deg: float,
) -> Matrix
Compose a matrix from translation, rotation, scale, and skew.
| Parameter | Type | Description |
|---|---|---|
tx | float | Translation x. |
ty | float | Translation y. |
angle_deg | float | Rotation angle in degrees. |
sx | float | Scale x. |
sy | float | Scale y. |
skew_angle_deg | float | Skew angle in degrees. |
| Returns | Matrix | A new Matrix. |
copy()
copy() -> Matrix
Return a deep copy of this matrix.
| Parameter | Type | Description |
|---|---|---|
| Returns | Matrix |
decompose()
decompose() -> tuple[float, float, float, float, float, float]
Decompose the matrix into translation, rotation, scale, and skew.
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[float, float, float, float, float, float] | (tx, ty, angle_deg, sx, sy, skew_angle_deg). |
flip_horizontal()
@classmethod
flip_horizontal(
center: Optional[tuple[float, float]] = None,
) -> Matrix
Create a horizontal flip matrix.
| Parameter | Type | Description |
|---|---|---|
center | Optional[tuple[float, float]] = None | |
| Returns | Matrix |
flip_vertical()
@classmethod
flip_vertical(
center: Optional[tuple[float, float]] = None,
) -> Matrix
Create a vertical flip matrix.
| Parameter | Type | Description |
|---|---|---|
center | Optional[tuple[float, float]] = None | |
| Returns | Matrix |
for_cairo()
for_cairo() -> tuple[float, float, float, float, float, float]
Returns the matrix components in Cairo order (xx, yx, xy, yy, x0, y0).
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[float, float, float, float, float, float] |
from_list()
@classmethod from_list(data: Sequence[Sequence[float]]) -> Matrix
Create a Matrix from a list of lists.
| Parameter | Type | Description |
|---|---|---|
data | Sequence[Sequence[float]] | |
| Returns | Matrix |
get()
get(row: int, col: int) -> float
Get a single element from the 3x3 matrix (row-major indexing).
| Parameter | Type | Description |
|---|---|---|
row | int | |
col | int | |
| Returns | float |
get_abs_scale()
get_abs_scale() -> tuple[float, float]
Extract the absolute scale components (|sx|, |sy|).
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[float, float] |
get_determinant_2x2()
get_determinant_2x2() -> float
Calculate the determinant of the top-left 2x2 sub-matrix.
| Parameter | Type | Description |
|---|---|---|
| Returns | float |
get_rotation()
get_rotation() -> float
Extract the rotation angle in degrees.
| Parameter | Type | Description |
|---|---|---|
| Returns | float |
get_scale()
get_scale() -> tuple[float, float]
Extract the signed scale components (sx, sy).
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[float, float] |
get_translation()
get_translation() -> tuple[float, float]
Extract the translation component (tx, ty).
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[float, float] |
get_x_axis_angle()
get_x_axis_angle() -> float
Calculate the angle of the transformed X-axis in degrees.
| Parameter | Type | Description |
|---|---|---|
| Returns | float |
get_y_axis_angle()
get_y_axis_angle() -> float
Calculate the angle of the transformed Y-axis in degrees.
| Parameter | Type | Description |
|---|---|---|
| Returns | float |
has_zero_scale()
has_zero_scale(tolerance: float = 1e-06) -> bool
Check if the matrix has zero scale on any axis.
| Parameter | Type | Description |
|---|---|---|
tolerance | float = 1e-06 | Threshold below which a scale is considered zero. |
| Returns | bool |
identity()
@classmethod identity() -> Matrix
Create an identity matrix.
| Parameter | Type | Description |
|---|---|---|
| Returns | Matrix |
invert()
invert() -> Matrix
Compute the inverse of the matrix.
Will raise an error if the matrix is singular.
| Parameter | Type | Description |
|---|---|---|
| Returns | Matrix |
is_close()
is_close(other: Matrix, tol: float = 1e-06) -> bool
Check if two matrices are effectively equal within tolerance.
| Parameter | Type | Description |
|---|---|---|
other | Matrix | The matrix to compare against. |
tol | float = 1e-06 | The absolute tolerance parameter. |
| Returns | bool |
is_flipped()
is_flipped() -> bool
Check if the matrix includes a reflection (flip).
| Parameter | Type | Description |
|---|---|---|
| Returns | bool |
is_identity()
is_identity() -> bool
Check if the matrix is an identity matrix.
| Parameter | Type | Description |
|---|---|---|
| Returns | bool |
post_rotate()
post_rotate(
angle_deg: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Apply a rotation after this matrix's transformation.
| Parameter | Type | Description |
|---|---|---|
angle_deg | float | Rotation angle in degrees. |
center | Optional[tuple[float, float]] = None | Optional center point to rotate around. |
| Returns | Matrix |
post_scale()
post_scale(
sx: float,
sy: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Apply a scale after this matrix's transformation.
| Parameter | Type | Description |
|---|---|---|
sx | float | Scale factor for x-axis. |
sy | float | Scale factor for y-axis. |
center | Optional[tuple[float, float]] = None | Optional center point to scale around. |
| Returns | Matrix |
post_shear()
post_shear(
shx: float,
shy: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Apply a shear after this matrix's transformation.
| Parameter | Type | Description |
|---|---|---|
shx | float | Shear factor for x-axis. |
shy | float | Shear factor for y-axis. |
center | Optional[tuple[float, float]] = None | Optional center point to shear around. |
| Returns | Matrix |
post_translate()
post_translate(tx: float, ty: float) -> Matrix
Apply a translation after this matrix's transformation.
| Parameter | Type | Description |
|---|---|---|
tx | float | |
ty | float | |
| Returns | Matrix |
pre_rotate()
pre_rotate(
angle_deg: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Apply a rotation before this matrix's transformation.
| Parameter | Type | Description |
|---|---|---|
angle_deg | float | Rotation angle in degrees. |
center | Optional[tuple[float, float]] = None | Optional center point to rotate around. |
| Returns | Matrix |
pre_scale()
pre_scale(
sx: float,
sy: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Apply a scale before this matrix's transformation.
| Parameter | Type | Description |
|---|---|---|
sx | float | Scale factor for x-axis. |
sy | float | Scale factor for y-axis. |
center | Optional[tuple[float, float]] = None | Optional center point to scale around. |
| Returns | Matrix |
pre_shear()
pre_shear(
shx: float,
shy: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Apply a shear before this matrix's transformation.
| Parameter | Type | Description |
|---|---|---|
shx | float | Shear factor for x-axis. |
shy | float | Shear factor for y-axis. |
center | Optional[tuple[float, float]] = None | Optional center point to shear around. |
| Returns | Matrix |
pre_translate()
pre_translate(tx: float, ty: float) -> Matrix
Apply a translation before this matrix's transformation.
| Parameter | Type | Description |
|---|---|---|
tx | float | |
ty | float | |
| Returns | Matrix |
rotation()
@classmethod
rotation(
angle_deg: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Create a rotation matrix.
| Parameter | Type | Description |
|---|---|---|
angle_deg | float | Rotation angle in degrees. |
center | Optional[tuple[float, float]] = None | Optional (x, y) center point to rotate around. |
| Returns | Matrix |
scale()
@classmethod
scale(
sx: float,
sy: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Create a scaling matrix.
| Parameter | Type | Description |
|---|---|---|
sx | float | Scale factor for x-axis. |
sy | float | Scale factor for y-axis. |
center | Optional[tuple[float, float]] = None | Optional (x, y) center point to scale around. |
| Returns | Matrix |
set_translation()
set_translation(tx: float, ty: float) -> Matrix
Returns a new matrix with the same rotation/scale/shear but new translation.
| Parameter | Type | Description |
|---|---|---|
tx | float | New translation x. |
ty | float | New translation y. |
| Returns | Matrix |
shear()
@classmethod
shear(
shx: float,
shy: float,
center: Optional[tuple[float, float]] = None,
) -> Matrix
Create a shearing matrix.
| Parameter | Type | Description |
|---|---|---|
shx | float | Shear factor for x-axis. |
shy | float | Shear factor for y-axis. |
center | Optional[tuple[float, float]] = None | Optional (x, y) center point to shear around. |
| Returns | Matrix |
to_4x4_list()
to_4x4_list() -> list[list[float]]
Converts the matrix to a 4x4 list of lists (row-major). The 3x3 Matrix is placed in the top-left 2x2 of the 4x4, with translation in the last column, and Z preserved.
| Parameter | Type | Description |
|---|---|---|
| Returns | list[list[float]] |
to_4x4_numpy()
to_4x4_numpy() -> numpy.NDArray[numpy.float64]
Return the matrix as a 4x4 numpy array (affine, Z-preserving).
| Parameter | Type | Description |
|---|---|---|
| Returns | numpy.NDArray[numpy.float64] |
to_graphene()
to_graphene() -> tuple[float, float, float, float, float, float]
Returns (xx, yx, xy, yy, x0, y0) for GTK/Graphene.
| Parameter | Type | Description |
|---|---|---|
| Returns | tuple[float, float, float, float, float, float] |
to_list()
to_list() -> list[list[float]]
Returns a copy of the matrix data as a list of lists.
| Parameter | Type | Description |
|---|---|---|
| Returns | list[list[float]] |
to_numpy()
to_numpy() -> numpy.NDArray[numpy.float64]
Return the matrix as a 3x3 numpy array.
| Parameter | Type | Description |
|---|---|---|
| Returns | numpy.NDArray[numpy.float64] |
transform_point()
transform_point(*args: float | tuple[float, float]) -> tuple[float, float]
Apply the affine transformation to a 2D point.
| Parameter | Type | Description |
|---|---|---|
*args | float | tuple[float, float] | |
| Returns | tuple[float, float] |
transform_rectangle()
transform_rectangle(
*args: float | tuple[float, float, float, float],
) -> tuple[float, float, float, float]
Transform a rectangle and return the new axis-aligned bounding box.
| Parameter | Type | Description |
|---|---|---|
*args | float | tuple[float, float, float, float] | |
| Returns | tuple[float, float, float, float] |
transform_vector()
transform_vector(*args: float | tuple[float, float]) -> tuple[float, float]
Apply the transformation to a 2D vector, ignoring translation.
| Parameter | Type | Description |
|---|---|---|
*args | float | tuple[float, float] | |
| Returns | tuple[float, float] |
translation()
@classmethod translation(tx: float, ty: float) -> Matrix
Create a translation matrix.
| Parameter | Type | Description |
|---|---|---|
tx | float | Translation in x. |
ty | float | Translation in y. |
| Returns | Matrix |
without_translation()
without_translation() -> Matrix
Returns a new matrix with translation set to zero.
| Parameter | Type | Description |
|---|---|---|
| Returns | Matrix |
Move
A rapid-move command with an endpoint but no cutting.
end
end: tuple[float, float, float]
Endpoint of the move in 3D space.