Skip to main content

Mat2

Struct Mat2 

Source
pub struct Mat2<T>(pub Vec2<T>, pub Vec2<T>);

Tuple Fields§

§0: Vec2<T>§1: Vec2<T>

Implementations§

Source§

impl<T> Mat2<T>
where T: NumAssign + Copy,

Source

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);
Source

pub fn zero() -> Self

Creates a 2x2 zero matrix.

This method returns a matrix with all elements set to zero.

§Returns

Returns a Mat2 instance with all elements as zero.

§Examples
let matrix = Mat2::zero();
Source

pub fn identity() -> Self

Creates a 2x2 identity matrix.

The identity matrix has ones on the diagonal and zeros elsewhere. It is the multiplicative identity for matrix multiplication.

§Returns

Returns a Mat2 instance with ones on the diagonal and zeros elsewhere.

§Examples
let matrix = Mat2::identity();
Source

pub fn transpose(&self) -> Self

Transposes the matrix.

The transpose of a matrix is obtained by swapping its rows and columns.

§Returns

Returns a new Mat2 instance that is the transpose of the original matrix.

§Examples
let matrix = Mat2::identity();
let transposed = matrix.transpose();
Source

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);
Source

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);
Source

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));
Source

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>
where T: NumAssign + Float,

Source

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)

Trait Implementations§

Source§

impl<T> Add for Mat2<T>
where T: NumAssign + Copy,

Source§

type Output = Mat2<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<T: Clone> Clone for Mat2<T>

Source§

fn clone(&self) -> Mat2<T>

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<T: Copy> Copy for Mat2<T>

Source§

impl<T: Debug> Debug for Mat2<T>

Source§

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

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

impl<T: Default> Default for Mat2<T>

Source§

fn default() -> Mat2<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Display for Mat2<T>
where T: Display,

Source§

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

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

impl<T> Div<T> for Mat2<T>
where T: NumAssign + Copy,

Source§

type Output = Mat2<T>

The resulting type after applying the / operator.
Source§

fn div(self, scalar: T) -> Self

Performs the / operation. Read more
Source§

impl<T> Index<usize> for Mat2<T>

Source§

type Output = Vec2<T>

The returned type after indexing.
Source§

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

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

impl<T> IndexMut<usize> for Mat2<T>

Source§

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

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

impl<'a, T> IntoIterator for &'a Mat2<T>
where T: NumAssign + Copy,

Source§

type Item = &'a Vec2<T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Vec2<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Mat2<T>
where T: NumAssign + Copy,

Source§

type Item = &'a mut Vec2<T>

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, Vec2<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> Mul for Mat2<T>
where T: NumAssign + Copy,

Source§

type Output = Mat2<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Mat2<T>) -> Mat2<T>

Performs the * operation. Read more
Source§

impl<T> Mul<T> for Mat2<T>
where T: NumAssign + Copy,

Source§

type Output = Mat2<T>

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: T) -> Self

Performs the * operation. Read more
Source§

impl<T> Neg for Mat2<T>
where T: NumAssign + Copy + Neg<Output = T>,

Source§

type Output = Mat2<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: PartialEq> PartialEq for Mat2<T>

Source§

fn eq(&self, other: &Mat2<T>) -> 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<T> StructuralPartialEq for Mat2<T>

Source§

impl<T> Sub for Mat2<T>
where T: NumAssign + Copy,

Source§

type Output = Mat2<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Mat2<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Mat2<T>
where T: RefUnwindSafe,

§

impl<T> Send for Mat2<T>
where T: Send,

§

impl<T> Sync for Mat2<T>
where T: Sync,

§

impl<T> Unpin for Mat2<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Mat2<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Mat2<T>
where T: UnwindSafe,

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.