pub struct Mat2<T>(pub Vec2<T>, pub Vec2<T>);Tuple Fields§
§0: Vec2<T>§1: Vec2<T>Implementations§
Source§impl<T> Mat2<T>
impl<T> Mat2<T>
Sourcepub fn new(col0: &Vec2<T>, col1: &Vec2<T>) -> Self
pub fn new(col0: &Vec2<T>, col1: &Vec2<T>) -> Self
Creates a new 2x2 matrix from two column vectors.
§Parameters
col0: The first column of the matrix.col1: The second column of the matrix.
§Returns
Returns a new Mat2 instance with the specified columns.
§Examples
let col0 = Vec2::new(1.0, 2.0);
let col1 = Vec2::new(3.0, 4.0);
let matrix = Mat2::new(&col0, &col1);Sourcepub fn determinant(&self) -> T
pub fn determinant(&self) -> T
Computes the determinant of the matrix.
The determinant is a scalar value that can be used to determine if the matrix is invertible. For a 2x2 matrix, the determinant is computed as:
det = (a00 * a11 - a01 * a10)
§Returns
Returns the determinant of the matrix.
§Examples
let matrix = Mat2::new(&Vec2::new(1.0, 2.0), &Vec2::new(3.0, 4.0));
let det = matrix.determinant();
assert_eq!(det, -2.0);Sourcepub fn trace(&self) -> T
pub fn trace(&self) -> T
Computes the trace of the matrix.
The trace of a matrix is the sum of its diagonal elements. For a 2x2 matrix, the trace is computed as:
trace = a00 + a11
§Returns
Returns the trace of the matrix.
§Examples
let matrix = Mat2::new(&Vec2::new(1.0, 2.0), &Vec2::new(3.0, 4.0));
let trace = matrix.trace();
assert_eq!(trace, 5.0);Sourcepub fn mul(&self, other: &Self) -> Self
pub fn mul(&self, other: &Self) -> Self
Multiplies this matrix with another 2x2 matrix.
Matrix multiplication is performed using the formula:
C[0][0] = A[0][0] * B[0][0] + A[1][0] * B[0][1]
C[0][1] = A[0][1] * B[0][0] + A[1][1] * B[0][1]
C[1][0] = A[0][0] * B[1][0] + A[1][0] * B[1][1]
C[1][1] = A[0][1] * B[1][0] + A[1][1] * B[1][1]§Parameters
other: The matrix to multiply with.
§Returns
Returns a new Mat2 instance that is the result of the matrix multiplication.
§Examples
let a = Mat2::new(&Vec2::new(1.0, 2.0), &Vec2::new(3.0, 4.0));
let b = Mat2::new(&Vec2::new(5.0, 6.0), &Vec2::new(7.0, 8.0));
let c = a.mul(&b);
assert_eq!(c.0, Vec2::new(19.0, 22.0));
assert_eq!(c.1, Vec2::new(43.0, 50.0));Sourcepub fn scale(&mut self, scale: Vec2<T>)
pub fn scale(&mut self, scale: Vec2<T>)
Scales the matrix by a given 2D vector.
This method scales the matrix by multiplying its elements according to the given scale vector. The scaling affects only the diagonal elements of the matrix.
§Parameters
scale: The 2D vector to scale the matrix by.
§Examples
let mut matrix = Mat2::identity();
matrix.scale(Vec2::new(2.0, 3.0));
assert_eq!(matrix.0, Vec2::new(2.0, 0.0));
assert_eq!(matrix.1, Vec2::new(0.0, 3.0));Source§impl<T> Mat2<T>
impl<T> Mat2<T>
Sourcepub fn rotation(&mut self, angle: T)
pub fn rotation(&mut self, angle: T)
Applies a rotation transformation to the matrix by the specified angle.
This method performs a rotation of the matrix elements by an angle, effectively rotating the coordinate system represented by the matrix. The rotation is applied using a counter-clockwise rotation matrix. The rotation is applied to the matrix as follows:
R = [ c -s ]
[ s c ]where c = cos(angle) and s = sin(angle).
§Parameters
angle: The angle of rotation in radians. Positive values rotate counter-clockwise, and negative values rotate clockwise.
§Examples
let mut matrix = Mat2::identity();
matrix.rotation(0.5); // Rotate by 0.5 radians
assert_eq!(matrix[0][0], 0.8775825618903728); // cos(0.5)
assert_eq!(matrix[0][1], -0.479425538604203); // -sin(0.5)
assert_eq!(matrix[1][0], 0.479425538604203); // sin(0.5)
assert_eq!(matrix[1][1], 0.8775825618903728); // cos(0.5)