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: Vec2The first column of the matrix
col1: Vec2The second column of the matrix
Implementations§
Source§impl Mat2
impl Mat2
pub const ZERO: Self
pub const IDENTITY: Self
pub const NAN: Self
pub const INFINITY: Self
Sourcepub fn new(col0: Vec2, col1: Vec2) -> Mat2
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]Sourcepub fn from_rows(r0: Vec2, r1: Vec2) -> Mat2
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)));Sourcepub fn from_angle(radians: f32) -> Mat2
pub fn from_angle(radians: f32) -> Mat2
Sourcepub fn from_scale(scale: Vec2) -> Mat2
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)));Sourcepub fn transpose(&self) -> Mat2
pub fn transpose(&self) -> Mat2
Returns the transpose of the matrix
Swaps rows and columns:
[a, b] => [a, c]
[c, d] => [b, d]Sourcepub fn determinant(&self) -> f32
pub fn determinant(&self) -> f32
Returns the determinant of the matrix
Used for checking invertibility or computing area under transformations
det = a * d- b * cSourcepub fn inverse(&self) -> Option<Mat2>
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
Sourcepub fn is_invertible(&self) -> bool
pub fn is_invertible(&self) -> bool
Returns true if the matrix is invertible (non-zero determinant)
Sourcepub fn to_array_2d_row_major(&self) -> [[f32; 2]; 2]
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]]
Sourcepub fn to_array_row_major(&self) -> [f32; 4]
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]
Sourcepub fn to_array_2d_col_major(&self) -> [[f32; 2]; 2]
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]]
Sourcepub fn to_array_col_major(&self) -> [f32; 4]
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]
Sourcepub fn to_tuple_2d_row_major(&self) -> Mat2Tuple2D
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))
Sourcepub fn to_tuple_row_major(&self) -> Mat2Tuple
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)
Sourcepub fn to_tuple_2d_col_major(&self) -> Mat2Tuple2D
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))
Sourcepub fn to_tuple_col_major(&self) -> Mat2Tuple
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)
Sourcepub fn from_array(arr: [f32; 4]) -> Mat2
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]]}}
Sourcepub fn from_2d_tuple(t: Mat2Tuple2D) -> Mat2
pub fn from_2d_tuple(t: Mat2Tuple2D) -> Mat2
Sourcepub fn from_tuple(t: Mat2Tuple) -> Mat2
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}}
pub fn row(&self, index: usize) -> Vec2
Sourcepub fn abs(self) -> Mat2
pub fn abs(self) -> Mat2
Returns a matrix with the absolute value of each component
Useful for bounding box operations or eliminating directional signs
Sourcepub fn signum(self) -> Mat2
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
Sourcepub fn lerp(&self, b: Mat2, t: f32) -> Mat2
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 matrixt: 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);Sourcepub fn lerp_between(a: Mat2, b: Mat2, t: f32) -> Mat2
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 matrixb: Target matrixt: Interpolation factor
Equivalent to a * (1.0 - t) + b * t
Sourcepub fn approx_eq(&self, other: Mat2) -> bool
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
Sourcepub fn approx_eq_eps(&self, other: Mat2, epsilon: f32) -> bool
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 comparisonepsilon: Epsilon to use in the check
Sourcepub fn adjugate(self) -> Mat2
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]Sourcepub fn extract_scale(&self) -> Vec2
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
Sourcepub fn extract_scale_raw(&self) -> Vec2
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)
Sourcepub fn extract_rotation(&self) -> f32
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
Sourcepub fn orthonormalize(&self) -> Mat2
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)
Sourcepub fn is_orthogonal(&self, epsilon: f32) -> bool
pub fn is_orthogonal(&self, epsilon: f32) -> bool
Sourcepub fn trace(self) -> f32
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.
pub fn decompose(&self) -> (Vec2, f32)
Sourcepub fn is_lower_triangular(&self, epsilon: f32) -> bool
pub fn is_lower_triangular(&self, epsilon: f32) -> bool
Sourcepub fn is_upper_triangular(&self, epsilon: f32) -> bool
pub fn is_upper_triangular(&self, epsilon: f32) -> bool
Trait Implementations§
Source§impl AbsDiffEq for Mat2
Implements absolute difference equality comparison for Mat2.
impl AbsDiffEq for Mat2
Implements absolute difference equality comparison for Mat2.
Source§fn default_epsilon() -> f32
fn default_epsilon() -> f32
Source§fn abs_diff_eq(&self, other: &Self, epsilon: f32) -> bool
fn abs_diff_eq(&self, other: &Self, epsilon: f32) -> bool
Source§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq.Source§impl AddAssign for Mat2
impl AddAssign for Mat2
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreimpl Copy for Mat2
Source§impl DivAssign<f32> for Mat2
impl DivAssign<f32> for Mat2
Source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/= operation. Read moreSource§impl IndexMut<usize> for Mat2
Enables mutable m[column] access. Panics if col_index is out of bounds.
impl IndexMut<usize> for Mat2
Enables mutable m[column] access. Panics if col_index is out of bounds.
Source§impl MulAssign for Mat2
impl MulAssign for Mat2
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl MulAssign<f32> for Mat2
impl MulAssign<f32> for Mat2
Source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*= operation. Read moreSource§impl RelativeEq for Mat2
Implements relative equality comparison for Mat2.
impl RelativeEq for Mat2
Implements relative equality comparison for Mat2.
Source§fn default_max_relative() -> f32
fn default_max_relative() -> f32
Source§fn relative_eq(&self, other: &Self, epsilon: f32, max_relative: f32) -> bool
fn relative_eq(&self, other: &Self, epsilon: f32, max_relative: f32) -> bool
Source§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
RelativeEq::relative_eq.