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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use core::convert;
use core::ops;

use crate::math::{Vec2, EPSILON};

/// A 2x2 matrix with `f32` elements.
#[derive(Clone, Copy, Debug, Default)]
pub struct Mat2 {
    // The elements of the matrix.
    // (a c)
    // (b d)
    data: [f32; 4],
}

impl ops::Add for Mat2 {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        Self::new(
            self.data[0] + other.data[0],
            self.data[2] + other.data[2],
            self.data[1] + other.data[1],
            self.data[3] + other.data[3],
        )
    }
}

impl ops::AddAssign for Mat2 {
    fn add_assign(&mut self, other: Self) {
        *self = *self + other
    }
}

impl ops::Sub for Mat2 {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        Self::new(
            self.data[0] - other.data[0],
            self.data[2] - other.data[2],
            self.data[1] - other.data[1],
            self.data[3] - other.data[3],
        )
    }
}

impl ops::SubAssign for Mat2 {
    fn sub_assign(&mut self, other: Self) {
        *self = *self - other
    }
}

impl ops::Neg for Mat2 {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self::new(-self.data[0], -self.data[2], -self.data[1], -self.data[3])
    }
}

impl ops::Mul<f32> for Mat2 {
    type Output = Self;

    fn mul(self, scale: f32) -> Self::Output {
        Self::new(
            self.data[0] * scale,
            self.data[2] * scale,
            self.data[1] * scale,
            self.data[3] * scale,
        )
    }
}

impl ops::Mul<Mat2> for f32 {
    type Output = Mat2;

    fn mul(self, rhs: Mat2) -> Self::Output {
        rhs * self
    }
}

impl ops::MulAssign<f32> for Mat2 {
    fn mul_assign(&mut self, scale: f32) {
        *self = *self * scale
    }
}

impl ops::Mul<Vec2> for Mat2 {
    type Output = Vec2;

    fn mul(self, other: Vec2) -> Self::Output {
        Vec2::new(
            self.data[0] * other.x() + self.data[2] * other.y(),
            self.data[1] * other.x() + self.data[3] * other.y(),
        )
    }
}

impl ops::Mul for Mat2 {
    type Output = Self;

    fn mul(self, other: Self) -> Self::Output {
        Self::new(
            self.data[0] * other.data[0] + self.data[2] * other.data[1],
            self.data[0] * other.data[2] + self.data[2] * other.data[3],
            self.data[1] * other.data[0] + self.data[3] * other.data[1],
            self.data[1] * other.data[2] + self.data[3] * other.data[3],
        )
    }
}

impl ops::MulAssign for Mat2 {
    fn mul_assign(&mut self, other: Self) {
        *self = *self * other;
    }
}

impl ops::Div<f32> for Mat2 {
    type Output = Self;

    fn div(self, scale: f32) -> Self::Output {
        Self::new(
            self.data[0] / scale,
            self.data[2] / scale,
            self.data[1] / scale,
            self.data[3] / scale,
        )
    }
}

impl ops::DivAssign<f32> for Mat2 {
    fn div_assign(&mut self, scale: f32) {
        *self = *self / scale
    }
}

impl PartialEq for Mat2 {
    fn eq(&self, other: &Self) -> bool {
        f32::abs(self.data[0] - other.data[0]) < EPSILON
            && f32::abs(self.data[2] - other.data[2]) < EPSILON
            && f32::abs(self.data[1] - other.data[1]) < EPSILON
            && f32::abs(self.data[3] - other.data[3]) < EPSILON
    }
}

impl Eq for Mat2 {}

impl Mat2 {
    /// Creates a new `Mat2` of the form:
    /// (a b)
    /// (c d)
    pub const fn new(a: f32, b: f32, c: f32, d: f32) -> Self {
        Self { data: [a, c, b, d] }
    }

    /// Creates a new `Mat2` from two column `Vec2`s.
    pub const fn from_vec2(v1: Vec2, v2: Vec2) -> Self {
        Self::new(v1.x(), v2.x(), v1.y(), v2.y())
    }

    /// Returns the zero matrix.
    pub const fn zero() -> Self {
        Self::new(0.0, 0.0, 0.0, 0.0)
    }

    /// Returns the identity matrix.
    pub const fn identity() -> Self {
        Self::new(1.0, 0.0, 0.0, 1.0)
    }

    /// Returns a matrix that rotates by `angle`.
    pub fn rotation(angle: f32) -> Self {
        let cos = angle.cos();
        let sin = angle.sin();
        Self::new(cos, -sin, sin, cos)
    }

    /// Returns a matrix that scales by `scale_x` and `scale_y`.
    pub const fn scaling(scale_x: f32, scale_y: f32) -> Self {
        Self::new(scale_x, 0.0, 0.0, scale_y)
    }

    /// Returns the transposed matrix.
    pub const fn transpose(self) -> Self {
        Self::new(self.data[0], self.data[1], self.data[2], self.data[3])
    }
}

impl convert::AsRef<[f32; 4]> for Mat2 {
    fn as_ref(&self) -> &[f32; 4] {
        &self.data
    }
}