use std::ops::{Mul, MulAssign};
use super::Vec2;
#[derive(PartialEq, Copy, Clone, Debug)]
pub struct Mat2 {
pub a00: f32,
pub a01: f32,
pub a10: f32,
pub a11: f32,
}
impl Mat2 {
pub fn new(a00: f32, a01: f32,
a10: f32, a11: f32) -> Mat2 {
Mat2 {
a00,
a01,
a10,
a11
}
}
pub fn rotation(angle: f32) -> Mat2 {
let (sin, cos) = angle.sin_cos();
Mat2::new(cos, -sin,
sin, cos)
}
pub fn transpose(&self) -> Mat2 {
Mat2::new(self.a00, self.a10,
self.a01, self.a11)
}
pub const I: Mat2 = Mat2 {
a00: 1.0,
a01: 0.0,
a10: 0.0,
a11: 1.0,
};
}
impl Mul for Mat2 {
type Output = Mat2;
fn mul(self, other: Mat2) -> Mat2 {
&self * &other
}
}
impl<'a> Mul<Mat2> for &'a Mat2 {
type Output = Mat2;
fn mul(self, other: Mat2) -> Mat2 {
self * &other
}
}
impl<'b> Mul<&'b Mat2> for Mat2 {
type Output = Mat2;
fn mul(self, other: &'b Mat2) -> Mat2 {
&self * other
}
}
impl<'a, 'b> Mul<&'b Mat2> for &'a Mat2 {
type Output = Mat2;
fn mul(self, other: &'b Mat2) -> Mat2 {
Mat2::new(
self.a00 * other.a00 + self.a01 * other.a10,
self.a00 * other.a01 + self.a01 * other.a11,
self.a10 * other.a00 + self.a11 * other.a10,
self.a10 * other.a01 + self.a11 * other.a11
)
}
}
impl Mul<Vec2> for Mat2 {
type Output = Vec2;
fn mul(self, other: Vec2) -> Vec2 {
&self * &other
}
}
impl<'a> Mul<Vec2> for &'a Mat2 {
type Output = Vec2;
fn mul(self, other: Vec2) -> Vec2 {
self * &other
}
}
impl<'b> Mul<&'b Vec2> for Mat2 {
type Output = Vec2;
fn mul(self, other: &'b Vec2) -> Vec2 {
&self * other
}
}
impl<'a, 'b> Mul<&'b Vec2> for &'a Mat2 {
type Output = Vec2;
fn mul(self, other: &'b Vec2) -> Vec2 {
Vec2::new(self.a00 * other.x + self.a01 * other.y,
self.a10 * other.x + self.a11 * other.y)
}
}
impl MulAssign for Mat2 {
fn mul_assign(&mut self, other: Mat2) {
*self = *self * other;
}
}
impl<'b> MulAssign<&'b Mat2> for Mat2 {
fn mul_assign(&mut self, other: &'b Mat2) {
*self = *self * other;
}
}