Transforms

Moving, turning and scaling the coordinate system.

transform

transform()

Saves the transform for a with block.

Think of it as “save the coordinate system”: inside the block, move, turn or scale freely; when the block ends, even through an exception or a return, the transform is back to what it was::

with kiln.transform():
    kiln.translate(x, y)
    kiln.rotate(angle)
    kiln.rect(-10, -10, 20, 20)

The transform is reset to identity at the start of every frame. A scope that spans methods (entered in update, left in draw) goes through contextlib.ExitStack.

Returns

Name Type Description
_Transform A context manager.

translate

translate(x, y, z=0.0)

Moves the origin of the coordinate system.

Parameters

Name Type Description Default
x float Offset along x, in the current (possibly rotated or scaled) axes. required
y float Offset along y. required
z float Accepted and ignored: Kiln is 2D. 0.0

rotate

rotate(radians)

Rotates the coordinate system around the origin, in radians.

A positive angle turns clockwise on screen, because y points down: after rotate(math.radians(90)) a circle drawn at (50, 0) lands at (0, 50), below the origin. To rotate around a point, translate there first.

Parameters

Name Type Description Default
radians float Angle in radians (math.radians converts degrees). required

scale

scale(x, y=None)

Scales the coordinate system around the origin.

Outline and line widths do not scale: stroke(width=) is in points on screen whatever the transform, including a non-uniform scale or a shear, so every edge of a stretched shape keeps the same thickness.

Parameters

Name Type Description Default
x float Scale along x (and along y when y is omitted). required
y float | None Scale along y; None means uniform. None

reset_matrix

reset_matrix(m=None)

Resets the transform to identity, or replaces it with m.

Inside with transform(): the end of the block still restores the transform from before it.

Parameters

Name Type Description Default
m np.ndarray | Sequence[Sequence[float]] | None None for the identity, or a 3x3 (last row 0, 0, 1) or 2x3 array-like, column-vector convention, as returned by current_matrix. None

Raises

Name Type Description
ValueError m is not an affine 3x3 or 2x3 matrix.

current_matrix

current_matrix()

Returns the current transform.

Returns

Name Type Description
np.ndarray A new 3x3 float64 numpy array in the column-vector convention,
np.ndarray [[m00, m01, tx], [m10, m11, ty], [0, 0, 1]]: a point (x, y) is
np.ndarray placed at (M @ [x, y, 1])[:2]. Editing it does not change the
np.ndarray transform; pass it to reset_matrix.

apply_matrix

apply_matrix(m)

Applies a matrix inside the current coordinate system.

The current transform becomes current @ m, like translate and friends.

Parameters

Name Type Description Default
m np.ndarray | Sequence[Sequence[float]] A 3x3 (last row 0, 0, 1) or 2x3 array-like, column-vector convention, as returned by current_matrix. required

Raises

Name Type Description
ValueError m is not an affine 3x3 or 2x3 matrix.