1use std::ops::Mul;
26
27use crate::*;
28
29#[derive(Debug, PartialEq, PartialOrd, Clone)]
32pub struct Matrix3 {
34 pub data: [[f64; 3]; 3],
35}
36
37impl Matrix3 {
38 pub fn identity() -> Matrix3 {
40 Matrix3 {
41 data: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
42 }
43 }
44 pub fn zeroes() -> Matrix3 {
46 Matrix3 {
47 data: [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
48 }
49 }
50 pub fn translation(x: f64, y: f64) -> Matrix3 {
52 Matrix3 {
53 data: [[1.0, 0.0, x], [0.0, 1.0, y], [0.0, 0.0, 1.0]],
54 }
55 }
56 pub fn scale(x: f64, y: f64) -> Matrix3 {
58 Matrix3 {
59 data: [[x, 0.0, 0.0], [0.0, y, 0.0], [0.0, 0.0, 1.0]],
60 }
61 }
62 pub fn rotation(rad: Rad) -> Matrix3 {
64 Matrix3 {
65 data: [
66 [rad.val.cos(), -rad.val.sin(), 0.0],
67 [rad.val.sin(), rad.val.cos(), 0.0],
68 [0.0, 0.0, 1.0],
69 ],
70 }
71 }
72}
73
74impl Default for Matrix3 {
75 fn default() -> Self {
76 Self::identity()
77 }
78}
79
80impl Mul for Matrix3 {
81 type Output = Self;
82
83 fn mul(self, other: Self) -> Self {
84 let mut result = Matrix3::default();
85 for i in 0..3 {
86 for j in 0..3 {
87 result.data[i][j] = self.data[i][0] * other.data[0][j]
88 + self.data[i][1] * other.data[1][j]
89 + self.data[i][2] * other.data[2][j];
90 }
91 }
92 result
93 }
94}
95
96impl Mul<&Matrix3> for Matrix3 {
97 type Output = Self;
98
99 fn mul(self, other: &Self) -> Self {
100 let mut result = Matrix3::default();
101 for i in 0..3 {
102 for j in 0..3 {
103 result.data[i][j] = self.data[i][0] * other.data[0][j]
104 + self.data[i][1] * other.data[1][j]
105 + self.data[i][2] * other.data[2][j];
106 }
107 }
108 result
109 }
110}
111
112impl Mul for &Matrix3 {
113 type Output = Matrix3;
114
115 fn mul(self, other: Self) -> Matrix3 {
116 let mut result = Matrix3::default();
117 for i in 0..3 {
118 for j in 0..3 {
119 result.data[i][j] = self.data[i][0] * other.data[0][j]
120 + self.data[i][1] * other.data[1][j]
121 + self.data[i][2] * other.data[2][j];
122 }
123 }
124 result
125 }
126}
127
128impl Mul<f64> for Matrix3 {
129 type Output = Self;
130
131 fn mul(self, other: f64) -> Self {
132 let mut result = Matrix3::default();
133 for i in 0..3 {
134 for j in 0..3 {
135 result.data[i][j] = other * self.data[i][j];
136 }
137 }
138 result
139 }
140}
141
142impl Mul<f64> for &Matrix3 {
143 type Output = Matrix3;
144
145 fn mul(self, other: f64) -> Matrix3 {
146 let mut result = Matrix3::default();
147 for i in 0..3 {
148 for j in 0..3 {
149 result.data[i][j] = other * self.data[i][j];
150 }
151 }
152 result
153 }
154}