Struct math2d::Matrix3x2f
source · #[repr(C)]pub struct Matrix3x2f {
pub a: f32,
pub b: f32,
pub c: f32,
pub d: f32,
pub x: f32,
pub y: f32,
}
Expand description
2D Affine Transformation Matrix.
Mathematically you can think of this matrix as if it were the following:
[a, b, 0]
[c, d, 0]
[x, y, 1]
Composing matrices
Affine transformations are performed in “Row-Major” order. What this means, if you’re familiar with linear algebra, is that when you compose multiple affine transformations together, the matrix representing the set of operations that should happen “first” must be the left hand operand of the multiplication operator.
This is also why points and vectors are the left-hand operand when multiplied with matrices.
Fields§
§a: f32
Horizontal scaling / cosine of rotation
b: f32
Vertical shear / sine of rotation
c: f32
Horizontal shear / negative sine of rotation
d: f32
Vertical scaling / cosine of rotation
x: f32
Horizontal translation (always orthogonal regardless of rotation)
y: f32
Vertical translation (always orthogonal regardless of rotation)
Implementations§
source§impl Matrix3x2f
impl Matrix3x2f
sourcepub const IDENTITY: Matrix3x2f = _
pub const IDENTITY: Matrix3x2f = _
The 2D affine identity matrix.
sourcepub fn new(parts: [[f32; 2]; 3]) -> Matrix3x2f
pub fn new(parts: [[f32; 2]; 3]) -> Matrix3x2f
Construct the matrix from an array of the row values.
sourcepub fn from_slice(values: &[f32]) -> Matrix3x2f
pub fn from_slice(values: &[f32]) -> Matrix3x2f
Constructs the matrix from a slice of 6 values as
[a, b, c, d, x, y]
.
Panics if values
does not contain exactly 6 elements.
sourcepub fn from_tuple(values: (f32, f32, f32, f32, f32, f32)) -> Matrix3x2f
pub fn from_tuple(values: (f32, f32, f32, f32, f32, f32)) -> Matrix3x2f
Constructs the matrix from a tuple of 6 values as
(a, b, c, d, x, y)
.
sourcepub fn translation(trans: impl Into<Vector2f>) -> Matrix3x2f
pub fn translation(trans: impl Into<Vector2f>) -> Matrix3x2f
Creates an affine translation matrix that translates points by the passed vector. The linear part of the matrix is the identity.
sourcepub fn scaling(
scale: impl Into<Vector2f>,
center: impl Into<Point2f>
) -> Matrix3x2f
pub fn scaling(
scale: impl Into<Vector2f>,
center: impl Into<Point2f>
) -> Matrix3x2f
Creates a scaling matrix that performs scaling around a specified point of origin. This is equivalent to translating the center point back to the origin, scaling around the origin by the scaling value, and then translating the center point back to its original location.
sourcepub fn rotation(angle: f32, center: impl Into<Point2f>) -> Matrix3x2f
pub fn rotation(angle: f32, center: impl Into<Point2f>) -> Matrix3x2f
Creates a rotation matrix that performs rotation around a specified point of origin. This is equivalent to translating the center point back to the origin, rotating around the origin by the specified angle, and then translating the center point back to its original location.
sourcepub fn skew(angle_x: f32, angle_y: f32, center: impl Into<Point2f>) -> Matrix3x2f
pub fn skew(angle_x: f32, angle_y: f32, center: impl Into<Point2f>) -> Matrix3x2f
Creates a matrix that skews an object by a tangent angle around the center point.
sourcepub fn linear_transpose(&self) -> Matrix3x2f
pub fn linear_transpose(&self) -> Matrix3x2f
Computes the transpose of the linear part of this matrix i.e. swap(b, c).
sourcepub fn determinant(&self) -> f32
pub fn determinant(&self) -> f32
Returns the determinant of the matrix. Since this matrix is conceptually 3x3, and the
bottom-right element is always 1, this value works out to be a * d - b * c
.
sourcepub fn is_invertible(&self) -> bool
pub fn is_invertible(&self) -> bool
Determines if the inverse
or try_inverse
functions would succeed if called. A
matrix is invertible if its determinant is nonzero. Since we’re dealing with floats,
we check that the absolute value of the determinant is greater than f32::EPSILON.
sourcepub fn inverse(&self) -> Matrix3x2f
pub fn inverse(&self) -> Matrix3x2f
Calculates the inverse of this matrix. Panics if the matrix is not invertible (see above).
sourcepub fn try_inverse(&self) -> Option<Matrix3x2f>
pub fn try_inverse(&self) -> Option<Matrix3x2f>
Calculates the inverse of the matrix. Returns None if the determinant is less than f32::EPSILON.
sourcepub fn unchecked_inverse(&self, det: f32) -> Matrix3x2f
pub fn unchecked_inverse(&self, det: f32) -> Matrix3x2f
Performs the inverse of the matrix without checking for invertibility.
WARNING: If this matrix is not invertible, you may get NaN or INF!
sourcepub fn compose(
scaling: impl Into<Vector2f>,
rotation: f32,
translation: impl Into<Vector2f>
) -> Matrix3x2f
pub fn compose(
scaling: impl Into<Vector2f>,
rotation: f32,
translation: impl Into<Vector2f>
) -> Matrix3x2f
Compose a matrix from a scaling, rotation, and translation value (combined in that order).
sourcepub fn decompose(&self) -> Decomposition
pub fn decompose(&self) -> Decomposition
Decomposes a simple affine transformation into its scaling, rotation, and translation parts.
sourcepub fn transform_point(&self, point: impl Into<Point2f>) -> Point2f
pub fn transform_point(&self, point: impl Into<Point2f>) -> Point2f
A more explicit way to do point * matrix
, while also allowing any type
that may be converted into a Point2F with a From/Into impl.
sourcepub fn transform_vector(&self, vec: impl Into<Vector2f>) -> Vector2f
pub fn transform_vector(&self, vec: impl Into<Vector2f>) -> Vector2f
A more explicit way to do vec * matrix
, while also allowing any type
that may be converted into a Vector2F with a From/Into impl.
sourcepub fn to_row_major(&self) -> [[f32; 3]; 3]
pub fn to_row_major(&self) -> [[f32; 3]; 3]
Returns this matrix as a 3x3 float array using the mathematical form described above.
sourcepub fn to_column_major(&self) -> [[f32; 3]; 3]
pub fn to_column_major(&self) -> [[f32; 3]; 3]
Returns the matrix as a 3x3 float array in column major form, i.e. the transpose of the row-major version.
sourcepub fn is_approx_eq(&self, other: &Matrix3x2f, epsilon: f32) -> bool
pub fn is_approx_eq(&self, other: &Matrix3x2f, epsilon: f32) -> bool
Checks if two matrices are approximately equal given an epsilon value.
sourcepub fn is_identity(&self) -> bool
pub fn is_identity(&self) -> bool
Checks if this matrix is equal to the identity matrix within 1e-5
Trait Implementations§
source§impl Clone for Matrix3x2f
impl Clone for Matrix3x2f
source§fn clone(&self) -> Matrix3x2f
fn clone(&self) -> Matrix3x2f
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Matrix3x2f
impl Debug for Matrix3x2f
source§impl Default for Matrix3x2f
impl Default for Matrix3x2f
source§impl<'de> Deserialize<'de> for Matrix3x2f
impl<'de> Deserialize<'de> for Matrix3x2f
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl From<D2D_MATRIX_3X2_F> for Matrix3x2f
impl From<D2D_MATRIX_3X2_F> for Matrix3x2f
source§fn from(m: D2D_MATRIX_3X2_F) -> Matrix3x2f
fn from(m: D2D_MATRIX_3X2_F) -> Matrix3x2f
source§impl From<DWRITE_MATRIX> for Matrix3x2f
impl From<DWRITE_MATRIX> for Matrix3x2f
source§fn from(m: DWRITE_MATRIX) -> Matrix3x2f
fn from(m: DWRITE_MATRIX) -> Matrix3x2f
source§impl From<Matrix3x2f> for D2D_MATRIX_3X2_F
impl From<Matrix3x2f> for D2D_MATRIX_3X2_F
source§fn from(m: Matrix3x2f) -> D2D_MATRIX_3X2_F
fn from(m: Matrix3x2f) -> D2D_MATRIX_3X2_F
source§impl From<Matrix3x2f> for DWRITE_MATRIX
impl From<Matrix3x2f> for DWRITE_MATRIX
source§fn from(m: Matrix3x2f) -> DWRITE_MATRIX
fn from(m: Matrix3x2f) -> DWRITE_MATRIX
source§impl From<Matrix3x2f> for RowMatrix3x2<f32>
impl From<Matrix3x2f> for RowMatrix3x2<f32>
source§fn from(mat: Matrix3x2f) -> RowMatrix3x2<f32>
fn from(mat: Matrix3x2f) -> RowMatrix3x2<f32>
source§impl Mul<Matrix3x2f> for Matrix3x2f
impl Mul<Matrix3x2f> for Matrix3x2f
§type Output = Matrix3x2f
type Output = Matrix3x2f
*
operator.source§fn mul(self, rhs: Matrix3x2f) -> Matrix3x2f
fn mul(self, rhs: Matrix3x2f) -> Matrix3x2f
*
operation. Read more