1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! A matrix with two rows and columns.

macro_rules! impl_op {
    ($op_trait:ident, $op_func:ident, $op:tt) => {
        impl<I> $op_trait<I> for Mat2<I> 
            where I: $op_trait<I> + Copy,
                  <I as $op_trait<I>>::Output: Into<I>
        {
            type Output = Mat2<I>;

            fn $op_func(mut self, other: I) -> Self::Output {
                self.data[0][0] = (self.data[0][0] $op other).into();
                self.data[1][0] = (self.data[1][0] $op other).into();
                self.data[0][1] = (self.data[0][1] $op other).into();
                self.data[1][1] = (self.data[1][1] $op other).into();

                self
            }
        }

        impl<I> $op_trait<Mat2<I>> for Mat2<I> 
            where I: $op_trait<I> + Copy,
                  <I as $op_trait<I>>::Output: Into<I>
        {
            type Output = Mat2<I>;

            fn $op_func(mut self, other: Mat2<I>) -> Self::Output {
                self.data[0][0] = (self.data[0][0] $op other.data[0][0]).into();
                self.data[1][0] = (self.data[1][0] $op other.data[1][0]).into();
                self.data[0][1] = (self.data[0][1] $op other.data[0][1]).into();
                self.data[1][1] = (self.data[1][1] $op other.data[1][1]).into();

                self
            }
        }
    };
}

macro_rules! impl_op_ass {
    ($op_trait:ident, $op_func:ident, $op:tt) => {
        impl<I> $op_trait<I> for Mat2<I> 
            where I: $op_trait<I> + Copy
        {
            fn $op_func(&mut self, other: I) {
                self.data[0][0] $op other;
                self.data[1][0] $op other;
                self.data[0][1] $op other;
                self.data[1][1] $op other;
            }
        }

        impl<I> $op_trait<Mat2<I>> for Mat2<I> 
            where I: $op_trait<I> + Copy
        {

            fn $op_func(&mut self, other: Mat2<I>) {
                self.data[0][0] $op other.data[0][0];
                self.data[1][0] $op other.data[1][0];
                self.data[0][1] $op other.data[0][1];
                self.data[1][1] $op other.data[1][1];
            }
        }
    };
}

use std::f32::consts::PI;
use super::Vec2;
use std::ops::{
    Add,
    Sub,
    Mul,
    Div,
    AddAssign,
    SubAssign,
    MulAssign,
    DivAssign,
};
use crate::serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Mat2<I> {
    data: [[I; 2]; 2],
}

impl<I> Mat2<I> {
    pub fn new(data: [[I; 2]; 2]) -> Mat2<I> {
        Mat2 {
            data
        }
    }

    pub fn from_radians(angle: f32) -> Mat2<f32> {
        Mat2 {
            data: [
                [angle.cos(), -angle.sin()],
                [angle.sin(), angle.cos()]
            ]
        }
    }

    pub fn from_degrees(mut angle: f32) -> Mat2<f32> {
        angle = angle/180.0 * PI;

        Self::from_radians(angle)
    }

    pub fn as_array(self) -> [[I; 2]; 2] {
        self.data
    }
}

impl<I> Mul<Vec2<I>> for Mat2<I> 
    where I: Add<I> + Mul<I> + Copy,
          <I as Mul<I>>::Output: Into<I>,
          <I as Add<I>>::Output: Into<I>
{
    type Output = Vec2<I>;

    fn mul(self, mut other: Vec2<I>) -> Self::Output {
        let x = other.x;
        other.x = ((other.x * self.data[0][0]).into() + (other.y * self.data[0][1]).into()).into();
        other.y = ((x * self.data[1][0]).into() + (other.y * self.data[1][1]).into()).into();

        other
    }
}

impl<I> Mul<Mat2<I>> for Vec2<I> 
    where I: Add<I> + Mul<I> + Copy,
          <I as Mul<I>>::Output: Into<I>,
          <I as Add<I>>::Output: Into<I>
{
    type Output = Vec2<I>;

    fn mul(mut self, other: Mat2<I>) -> Self::Output {
        let x = self.x;
        self.x = ((self.x * other.data[0][0]).into() + (self.y * other.data[0][1]).into()).into();
        self.y = ((x * other.data[1][0]).into() + (self.y * other.data[1][1]).into()).into();

        self
    }
}

impl<I> MulAssign<Mat2<I>> for Vec2<I>
    where Vec2<I>: Mul<Mat2<I>> + Clone,
          <Vec2<I> as Mul<Mat2<I>>>::Output: Into<Vec2<I>>
{
    fn mul_assign(&mut self, other: Mat2<I>) {
        *self = (self.clone() * other).into();
    }
}

impl_op!(Add, add, +);
impl_op!(Sub, sub, -);
impl_op!(Mul, mul, *);
impl_op!(Div, div, /);

impl_op_ass!(AddAssign, add_assign, +=);
impl_op_ass!(SubAssign, sub_assign, -=);
impl_op_ass!(MulAssign, mul_assign, *=);
impl_op_ass!(DivAssign, div_assign, /=);