Skip to main content

Mat2

Struct Mat2 

Source
pub struct Mat2 {
    pub col0: Vec2,
    pub col1: Vec2,
}
Expand description

A 2x2 column-major matrix used for 2D linear transformations such as rotation, scaling, and shearing.

The matrix is composed of two Vec2 column vectors:

[col0.x, col1.x]
[col0.y, col1.y]

§Example

use omelet::vec::Vec2;
use omelet::matrices::Mat2;

let col0 = Vec2::new(1.0, 0.0);
let col1 = Vec2::new(0.0, 1.0);
let identity = Mat2::new(col0, col1);

Fields§

§col0: Vec2

The first column of the matrix

§col1: Vec2

The second column of the matrix

Implementations§

Source§

impl Mat2

Source

pub const ZERO: Self

Source

pub const IDENTITY: Self

Source

pub const NAN: Self

Source

pub const INFINITY: Self

Source

pub fn new(col0: Vec2, col1: Vec2) -> Mat2

Constructs a new 2x2 matrix from two column vectors

The matrix is structured in column-major order:

[col0.x, col1.x]
[col0.y, col1.y]
Source

pub fn from_rows(r0: Vec2, r1: Vec2) -> Mat2

Constructs a matrix from row vectors

§Example
use omelet::vec::Vec2;
use omelet::matrices::Mat2;

let m = Mat2::from_rows(Vec2::new(1.0, 2.0), Vec2::new(3.0, 4.0));
assert_eq!(m, Mat2::new(Vec2::new(1.0, 3.0), Vec2::new(2.0, 4.0)));
Source

pub fn from_angle(radians: f32) -> Mat2

Constructs a matrix representing a counter-clockwise rotation

§Parameters
  • radians: Rotation angle in radians
§Example
use omelet::matrices::Mat2;
let rot90 = Mat2::from_angle(std::f32::consts::FRAC_PI_2);
Source

pub fn from_scale(scale: Vec2) -> Mat2

Constructs a scale matrix from a scale vector

§Example
use omelet::matrices::Mat2;
use omelet::vec::Vec2;
let scale = Mat2::from_scale(Vec2::new(2.0, 3.0));
assert_eq!(scale, Mat2::new(Vec2::new(2.0, 0.0), Vec2::new(0.0, 3.0)));
Source

pub fn transpose(&self) -> Mat2

Returns the transpose of the matrix

Swaps rows and columns:

[a, b]  =>  [a, c]
[c, d]  =>  [b, d]
Source

pub fn determinant(&self) -> f32

Returns the determinant of the matrix

Used for checking invertibility or computing area under transformations

det = a * d- b * c
Source

pub fn inverse(&self) -> Option<Mat2>

Returns the inverse of the matrix, if it exists

Returns None if the matrix is singular (non-invertible)

§Panics

Will not panic. Returns None instead of panicking on singular matrices

Source

pub fn is_invertible(&self) -> bool

Returns true if the matrix is invertible (non-zero determinant)

Source

pub fn to_array_2d_row_major(&self) -> [[f32; 2]; 2]

Returns a row major 2 dimensional array [[f32; 2]; 2]

Equivalent to [[col0.x, col1.x], [col0.y, col1.y]]

Source

pub fn to_array_row_major(&self) -> [f32; 4]

Returns a row major flat array [f32; 4]

Equivalent to [col0.x, col0.y, col1.x, col1.y]

Source

pub fn to_array_2d_col_major(&self) -> [[f32; 2]; 2]

Returns a column major 2 dimensional array [[f32; 2]; 2]

Equivalent to [[col0.x, col0.y], [col1.x, col1.y]]

Source

pub fn to_array_col_major(&self) -> [f32; 4]

Returns a column major flat array [f32; 4]

Equivalent to [col0.x, col0.y, col1.x, col1.y]

Source

pub fn to_tuple_2d_row_major(&self) -> Mat2Tuple2D

Returns a row major 2 dimensional tuple ((f32, f32), (f32, f32))

Equivalent to ((col0.x, col1.x), (col0.y, col1.y))

Source

pub fn to_tuple_row_major(&self) -> Mat2Tuple

Returns a row major tuple (f32, f32, f32, f32)

Equivalent to (col0.x, col0.y, col1.x, col1.y)

Source

pub fn to_tuple_2d_col_major(&self) -> Mat2Tuple2D

Returns a column major 2 dimensional tuple ((f32, f32), (f32, f32))

Equivalent to ((col0.x, col0.y), (col1.x, col1.y))

Source

pub fn to_tuple_col_major(&self) -> Mat2Tuple

Returns a column major tuple (f32, f32, f32, f32)

Equivalent to (col0.x, col0.y, col1.x, col1.y)

Source

pub fn from_2d_array(arr: [[f32; 2]; 2]) -> Mat2

Returns a Mat2 from a 2 dimensional array

§Paramneters
  • arr: 2 dimensional array [[f32; 2]; 2]
Source

pub fn from_array(arr: [f32; 4]) -> Mat2

Returns a Mat2 from a flat array

§Parameters
  • arr: Flat array [f32; 4]

Equivalent to Mat2{Vec2{arr[0], arr[1]}, Vec2[arr[2], arr[3]]}}

Source

pub fn from_2d_tuple(t: Mat2Tuple2D) -> Mat2

Returns a Mat2 from a 2 dimensional tuple

§Parameters
  • t: Tuple to use ((f32, f32), (f32, f32))
Source

pub fn from_tuple(t: Mat2Tuple) -> Mat2

Returns a Mat2 from a tuple

§Parameters
  • t: Tuple (f32, f32, f32, f32)

Equivalent to Mat2{Vec2{t.0, t.1}, Vec2{t.2, t.3}}

Source

pub fn col(&self, index: usize) -> Vec2

Returns the specified column vector.

§Panics

Panics if col_idx is not 0 or 1.

Source

pub fn row(&self, index: usize) -> Vec2

Source

pub fn to_mat3(&self) -> Mat3

Returns a 3x3 column-major matrix.

§Returns

A Mat3:

[a, c, 0]
[b, d, 0]
[0, 0, 1]
Source

pub fn abs(self) -> Mat2

Returns a matrix with the absolute value of each component

Useful for bounding box operations or eliminating directional signs

Source

pub fn signum(self) -> Mat2

Returns the sign (-1.0, 0.0, 1.0) of each matrix component

Useful when analyzing directionality of matrix effects

Source

pub fn lerp(&self, b: Mat2, t: f32) -> Mat2

Linearly interpolates between self and matrix b by amount t

Equivalent to self * (1.0 - t) + b * t

§Parameters
  • b: The target matrix
  • t: Interpolation factor
§Example
use omelet::matrices::Mat2;
use omelet::vec::Vec2;

let a = Mat2::IDENTITY;
let b = Mat2::from_scale(Vec2::new(2.0, 2.0));
let halfway = a.lerp(b, 0.5);
Source

pub fn lerp_between(a: Mat2, b: Mat2, t: f32) -> Mat2

Linearly interpolates between matrix a and matrix b by amount t

§Parameters
  • a: Starting matrix
  • b: Target matrix
  • t: Interpolation factor

Equivalent to a * (1.0 - t) + b * t

Source

pub fn approx_eq(&self, other: Mat2) -> bool

Checks if all components in self are approximately equal to all components in other with a default epsilon

§Parameters
  • other: The other matrix in the comparison
Source

pub fn approx_eq_eps(&self, other: Mat2, epsilon: f32) -> bool

Checks if all components in self are approximately equal to all components in other with a custom epsilon

§Parameters
  • other: The other matrix in the comparison
  • epsilon: Epsilon to use in the check
Source

pub fn is_nan(&self) -> bool

Returns true if any of the components are NaN

Source

pub fn is_finite(&self) -> bool

Returns true if all of the components are finite

Source

pub fn adjugate(self) -> Mat2

Computes the adjugate (classical adjoint) of the matrix.

For matrix A =

[a, b]
[c, d]

the adjugate is:

[ d, -b]
[-c,  a]
Source

pub fn extract_scale(&self) -> Vec2

Extracts the scale component from the matrix by computing the length (magnitude) of each column vector

This assumes that the matrix encodes a rotation+scale transform

Source

pub fn extract_scale_raw(&self) -> Vec2

Returns the diagonal scale components without considering rotation

This assumes the matrix is purely scaling or has scale aligned with axes It’s a “raw” lookup: returns (col0.x, col1.y)

Source

pub fn extract_rotation(&self) -> f32

Extracts the rotation angle in radians from the matrix

Assumes the matrix is a pure rotation or a rotation+scale transform Uses the direction of the first column vector (X-axis) to determine angle

Source

pub fn orthonormalize(&self) -> Mat2

Returns an orthonormalized version of the matrix

This ensures the columns are orthogonal unit vectors. This is useful if the matrix has accumulated numerical error or is approximately orthonormal

  • First column is normalized as the X-axis.
  • Second column is computed as the perpendicular vector (rotated 90 degrees CCW)
Source

pub fn is_orthogonal(&self, epsilon: f32) -> bool

Checks if the matrix is orthogonal within a tolerance.

Orthogonal matrices satisfy M^T * M = I.

§Parameters
  • epsilon: Maximum allowed deviation from orthogonality.
§Returns

true if orthogonal within tolerance.

Source

pub fn trace(self) -> f32

Returns the trace of the matrix.

The trace is the sum of the diagonal elements (a + d).

§Returns

Scalar trace value.

Source

pub fn decompose(&self) -> (Vec2, f32)

Source

pub fn is_lower_triangular(&self, epsilon: f32) -> bool

Checks if the matrix is lower-triangular within a tolerance.

This means elements above the main diagonal are approx zero.

§Parameters
  • epsilon: Maximum allowed magnitude of upper-triangular elements.
§Returns

true if matrix is lower-triangular within tolerance.

Source

pub fn is_upper_triangular(&self, epsilon: f32) -> bool

Checks if the matrix is upper-triangular within a tolerance.

This means elements below the main diagonal are approx zero.

§Parameters
  • epsilon: Maximum allowed magnitude of lower-triangular elements.
§Returns

true if matrix is upper-triangular within tolerance.

Trait Implementations§

Source§

impl AbsDiffEq for Mat2

Implements absolute difference equality comparison for Mat2.

Source§

type Epsilon = f32

Used for specifying relative comparisons.
Source§

fn default_epsilon() -> f32

The default tolerance to use when testing values that are close together. Read more
Source§

fn abs_diff_eq(&self, other: &Self, epsilon: f32) -> bool

A test for equality that uses the absolute difference to compute the approximate equality of two numbers.
Source§

fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool

The inverse of AbsDiffEq::abs_diff_eq.
Source§

impl Add for Mat2

Adds two matrices together component-wise.

Source§

type Output = Mat2

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for Mat2

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Mat2

Source§

fn clone(&self) -> Mat2

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Mat2

Source§

impl Debug for Mat2

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Mat2

Source§

fn default() -> Self

Returns the identity matrix.

Source§

impl Display for Mat2

Implements the Display trait for pretty-printing.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<f32> for Mat2

Divides each component of the matrix by a scalar.

Source§

type Output = Mat2

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign<f32> for Mat2

Source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
Source§

impl Index<usize> for Mat2

Enables m[column] access. Panics if col_index is out of bounds.

Source§

type Output = Vec2

The returned type after indexing.
Source§

fn index(&self, col_index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Mat2

Enables mutable m[column] access. Panics if col_index is out of bounds.

Source§

fn index_mut(&mut self, col_index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Mul for Mat2

Multiplies two matrices using standard matrix multiplication.

Source§

type Output = Mat2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Mat2> for f32

Multiplies a scalar by each component of the matrix.

Source§

type Output = Mat2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Mat2) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vec2> for Mat2

Multiplies the matrix by a Vec2 (matrix-vector multiplication).

Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vec2) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f32> for Mat2

Multiplies each component of the matrix by a scalar.

Source§

type Output = Mat2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign for Mat2

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl MulAssign<f32> for Mat2

Source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. Read more
Source§

impl Neg for Mat2

Negates each component of the matrix.

Source§

type Output = Mat2

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for Mat2

Checks whether two matrices are exactly equal.

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl RelativeEq for Mat2

Implements relative equality comparison for Mat2.

Source§

fn default_max_relative() -> f32

The default relative tolerance for testing values that are far-apart. Read more
Source§

fn relative_eq(&self, other: &Self, epsilon: f32, max_relative: f32) -> bool

A test for equality that uses a relative comparison if the values are far apart.
Source§

fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool

The inverse of RelativeEq::relative_eq.
Source§

impl Sub for Mat2

Subtracts rhs from self component-wise.

Source§

type Output = Mat2

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign for Mat2

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl Freeze for Mat2

§

impl RefUnwindSafe for Mat2

§

impl Send for Mat2

§

impl Sync for Mat2

§

impl Unpin for Mat2

§

impl UnsafeUnpin for Mat2

§

impl UnwindSafe for Mat2

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V