Matrix
A 2D affine transformation (a 2×3 matrix), in the layout of CSS and SVG
matrix(a, b, c, d, e, f):
Import
Section titled “Import”Cargo feature geometry (enabled by default). To compile only this module:
or in Cargo.toml:
Definition
Section titled “Definition”Examples
Section titled “Examples”Methods
Section titled “Methods”rotate
Section titled “rotate”A rotation around the origin. With the y axis pointing down (screens, CSS, SVG), a
positive angle turns clockwise, like CSS rotate().
Parameters
| Parameter | Type | Description |
|---|---|---|
radians | f64 | The angle, in radians. |
Returns
Self — The matrix.
Examples
multiply
Section titled “multiply”Multiplies self by other, in the mathematical order: other applies first, then
self, as in a CSS transform list (transform: self other).
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Self | The transformation applied first. |
Returns
Self — The product, with m.multiply(n).apply(p) == m.apply(n.apply(p)) (up to rounding).
Examples
invert
Section titled “invert”The matrix that undoes self, e.g. to turn a pointer position into the coordinates of a
transformed layer.
A matrix that flattens the plane onto a line or a point (determinant 0) cannot be
undone. There is no tolerance: a nearly flat matrix is inverted, with very large entries,
as long as they stay finite.
Returns
Option<Self> — The inverse, or None when self cannot be inverted (or has a non-finite entry).
Examples
rect_to_rect
Section titled “rect_to_rect”The translation and scale that put one rectangle exactly on another, for animating or
fitting between two boxes. An axis where from has no extent is only translated (scale
1).
Parameters
| Parameter | Type | Description |
|---|---|---|
from | Rect | The rectangle to move. |
to | Rect | The rectangle to reach. |
Returns
Self — The matrix that maps from onto to.
Examples
to_css
Section titled “to_css”The CSS and SVG transform value, matrix(a, b, c, d, e, f).
Entries are written as Rust prints them, without rounding (and 0 for -0); a
non-finite entry gives a value CSS rejects.
Returns
String — For instance "matrix(2, 0, 0, 2, 10, -5)".
Examples
Transforms a point.
Parameters
| Parameter | Type | Description |
|---|---|---|
point | Point | The point to transform. |
Returns
Point — (a·x + c·y + e, b·x + d·y + f).
Examples
bounding_box
Section titled “bounding_box”Transforms a rectangle and returns its bounding box: the smallest axis-aligned rectangle containing it. After a rotation or a skew, the box is larger than the rectangle.
Parameters
| Parameter | Type | Description |
|---|---|---|
rect | Rect | The rectangle to transform. |
Returns
Rect — The bounding box of the four transformed corners, with a non-negative size.
Examples
