Skip to content
Matrix2D

Matrix2D

Mojo struct 🡭

Matrix2D

@memory_only
struct Matrix2D

A general affine map of the plane.

x' = a * x + c * y + e
y' = b * x + d * y + f

in the (a, b, c, d, e, f) layout SVG’s matrix(), Cairo’s cairo_matrix_t and the HTML5 canvas’s setTransform share, so a matrix written for any of them reads the same here. Canvas keeps one as its current transform (see Canvas.save) and every drawing call maps its coordinates through it.

Where Transform2D’s pipeline is fixed as scale, rotate, translate, a Matrix2D is closed under composition: then of any two is another Matrix2D, which is what lets a canvas accumulate translate/rotate/scale calls in any order. Matrix2D(t) converts a Transform2D, so a chart’s data-to-pixel mapping can become the canvas transform.

Fields

  • a (Float64)
  • b (Float64)
  • c (Float64)
  • d (Float64)
  • e (Float64)
  • f (Float64)

Implemented traits

AnyType, Copyable, Deinitable, ImplicitlyCopyable, Movable

Methods

__init__

fn def __init__(out self, a: Float64, b: Float64, c: Float64, d: Float64, e: Float64, f: Float64)

The matrix with these coefficients – see the struct docstring for the layout.

Args:

  • a (Float64): Coefficient of x in x'.
  • b (Float64): Coefficient of x in y'.
  • c (Float64): Coefficient of y in x'.
  • d (Float64): Coefficient of y in y'.
  • e (Float64): Offset added to x'.
  • f (Float64): Offset added to y'.
  • self (Self)

Returns:

Self

fn def __init__(out self, transform: Transform2D)

The matrix of transform’s scale-then-rotate-then-translate pipeline: the same mapping transform.to_point computes.

Args:

  • transform (Transform2D): The mapping to express as a matrix.
  • self (Self)

Returns:

Self

identity

@staticmethod
fn def identity() -> Self

The map that leaves every point where it is.

Returns:

Self: The identity matrix.

translation

@staticmethod
fn def translation(tx: Float64, ty: Float64) -> Self

A shift by (tx, ty).

Args:

  • tx (Float64): Horizontal shift.
  • ty (Float64): Vertical shift.

Returns:

Self: The translation matrix.

scaling

@staticmethod
fn def scaling(sx: Float64, sy: Float64) -> Self

A scale about the origin, each axis by its own factor. A negative factor mirrors that axis.

Args:

  • sx (Float64): Horizontal factor.
  • sy (Float64): Vertical factor.

Returns:

Self: The scaling matrix.

rotation

@staticmethod
fn def rotation(angle: Float64) -> Self

A rotation about the origin by angle radians. Positive turns +x toward +y, which is clockwise on a y-down canvas.

Args:

  • angle (Float64): Radians.

Returns:

Self: The rotation matrix.

apply

fn def apply(self, x: Float64, y: Float64) -> FPoint

Map a point.

Args:

  • self (Self)
  • x (Float64): Point x.
  • y (Float64): Point y.

Returns:

FPoint: The mapped point.

then

fn def then(self, other: Self) -> Self

This map followed by other: self.then(o).apply(p) is o.apply(self.apply(p)).

Args:

  • self (Self)
  • other (Self): The map applied second.

Returns:

Self: The composed map.

determinant

fn def determinant(self) -> Float64

The area scale of the map, negative if it mirrors.

Args:

  • self (Self)

Returns:

Float64: The value a * d - b * c.

inverse

fn def inverse(self) -> Self

The map that undoes this one.

Args:

  • self (Self)

Returns:

Self: The inverse matrix.

Raises:

Error: The determinant is zero, so the map collapses the plane onto a line or point and cannot be undone.

is_identity

fn def is_identity(self) -> Bool

Whether the map leaves every point where it is.

Args:

  • self (Self)

Returns:

Bool: True for the identity matrix exactly.

is_translation

fn def is_translation(self) -> Bool

Whether the map is a pure shift.

Args:

  • self (Self)

Returns:

Bool: True if the linear part is the identity.

is_axis_aligned

fn def is_axis_aligned(self) -> Bool

Whether the map keeps axis-aligned rectangles axis-aligned: scale (possibly mirrored) and translation only, no rotation or shear.

Args:

  • self (Self)

Returns:

Bool: True if b and c are both zero.

is_similarity

fn def is_similarity(self) -> Bool

Whether the map keeps circles circular and angles intact: a rotation, a uniform scale and a translation, with no mirroring.

Args:

  • self (Self)

Returns:

Bool: True for such a map.

scale_factor

fn def scale_factor(self) -> Float64

The map’s length scale: the factor a similarity scales every length by, and the geometric mean of the two axis scales otherwise – what a stroke width or dash length is multiplied by to follow the map.

Args:

  • self (Self)

Returns:

Float64: The square root of the absolute determinant.

rotation_angle

fn def rotation_angle(self) -> Float64

The angle the map turns the +x axis by, in radians.

Args:

  • self (Self)

Returns:

Float64: The angle atan2(b, a).