Matrices and bounds
Vector outlines and warps both move points through a matrix, and a drawing program has to know how big a shape is. This page is the part of that API that is not about drawing: map points and boxes, combine and undo transforms, and measure an outline.
import os
import libstb as stb
os.makedirs("out", exist_ok=True)
# A matrix is built by calls that each apply to a point first; read the chain from the bottom up.
m = stb.Matrix().translate(100, 50).rotate(0.5).scale(2, 2)
print("a new matrix is the identity:", "yes" if stb.Matrix().is_identity else "no", "| this one:", "yes" if m.is_identity else "no")
x, y = m.map(10, 0)
print(f"(10, 0) goes to ({round(x)}, {round(y)})")
print("two points at once:", [(round(px), round(py)) for px, py in m.map_points([(0, 0), (40, 0)])])
# A box mapped through a rotation is the box around its four corners: larger than the box.
x0, y0, x1, y1 = m.map_rect(0, 0, 40, 20)
print(f"the box (0, 0, 40, 20) becomes ({round(x0)}, {round(y0)}, {round(x1)}, {round(y1)})")
# Matrices combine: then() applies this one first and the next second. inverse() undoes one, or is None.
grow_then_move = stb.Matrix.scaling(2).then(stb.Matrix.translation(5, 0))
print("scale 2 then move 5: (1, 0) goes to", tuple(round(v) for v in grow_then_move.map(1, 0)))
back = m.inverse().map(*m.map(10, 0))
print("inverse undoes it: (10, 0) comes back as", tuple(round(v) for v in back))
print("a matrix that flattens has no inverse:", "yes" if stb.Matrix.scaling(0, 1).inverse() is None else "no")
# An outline has two boxes: the one around all its points, control points included, and the exact one of what it draws.
heart = stb.Outline().move_to(60, 190).cubic_to(-20, 120, 30, 40, 60, 90).cubic_to(90, 40, 140, 120, 60, 190).close()
control = heart.bounds
exact = heart.tight_bounds
print("control box:", tuple(round(v) for v in control))
print("exact box:", tuple(round(v) for v in exact))
print("the pen is at:", tuple(round(v) for v in heart.current_point))
moved = heart.transformed(stb.Matrix().translate(60, 0))
print("moved copy, exact box:", tuple(round(v) for v in moved.tight_bounds))
# Both boxes on the heart. A canvas rectangle takes whole pixels.
img = stb.Image(memoryview(bytearray([250, 248, 240, 255] * (260 * 220))).cast("B", (220, 260, 4)))
canvas = stb.Canvas(img)
canvas.add_outline(heart).fill("#f4a9a8")
for box, color in ((control, "#e63946"), (exact, "#1d3557")):
x0, y0, x1, y1 = (round(v) for v in box)
canvas.add_rect(x0, y0, x1 - x0, y1 - y0).no_fill().stroke(1.5, color)
canvas.finish().write("out/bounds.png")
a new matrix is the identity: yes | this one: no
(10, 0) goes to (118, 60)
two points at once: [(100, 50), (170, 88)]
the box (0, 0, 40, 20) becomes (81, 50, 170, 123)
scale 2 then move 5: (1, 0) goes to (7, 0)
inverse undoes it: (10, 0) comes back as (10, 0)
a matrix that flattens has no inverse: yes
control box: (-20, 40, 140, 190)
exact box: (17, 74, 103, 190)
the pen is at: (60, 190)
moved copy, exact box: (77, 74, 163, 190)

The matrix
A matrix is a 2D affine transform in the SVG convention: x' = a x + c y + e and y' = b x + d y + f. A new one is the identity. You build one by calls, translate, rotate (radians, clockwise on a screen whose y grows downwards), scale and shear, and each call changes the matrix so that its operation is applied to a point first, before everything the matrix did already. The chain reads like the SVG transform attribute, from right to left: translate(100, 50).rotate(0.5).scale(2, 2) scales a point, then rotates it, then moves it.
translation, scaling, rotation and shearing make a matrix of one kind in one call, with no chain.
Mapping
- map(x, y) returns where a point goes.
map_pointsdoes the same for many points, with the same arithmetic. - map_rect(x0, y0, x1, y1) returns the box around the four mapped corners of a box. After a rotation it is larger than the box it started from, because the rotated box is not aligned any more.
Combining
a.then(b) applies a first and b second. That is the same as a chain read the other way: scaling(2).then(translation(5, 0)) doubles and then moves by 5, so the point (1, 0) goes to (7, 0), as the example prints. inverse() returns the matrix that undoes this one, and nothing, None, when there is none: a matrix that flattens the plane has no inverse. is_identity tells whether a matrix does nothing at all.
Bounds of an outline
An outline has two boxes. bounds is the box around all its points, the control points of the curves included, and it is cheap. tight_bounds is the exact box of what the outline draws: the extremes of the curves, not their control points. It is always inside bounds, and the picture shows the difference on the heart, whose control points lie well outside the curve. Both are empty for an empty outline.
current_point is where the next line_to would start, and transformed(matrix) returns a copy of the outline moved through a matrix, so you can measure the result without drawing it.
API at a glance
| A matrix | stb.Matrix().translate(x, y).rotate(a).scale(sx, sy) |
|---|---|
| One kind in one call | stb.Matrix.rotation(a), translation, scaling, shearing |
| A point | m.map(x, y), m.map_points(points) |
| A box | m.map_rect(x0, y0, x1, y1) |
| Combine, undo | a.then(b), m.inverse(), m.is_identity |
| Boxes of an outline | o.bounds, o.tight_bounds |
| The pen, a moved copy | o.current_point, o.transformed(m) |