Skip to content

Commit b36957b

Browse files
committed
Implement Mul for Transform
Seems like it is implemented in SFML like such, so I copied the behavior from SFML to here.
1 parent da4caec commit b36957b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/graphics/transform.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::{Mul, MulAssign};
2+
13
use crate::{ffi::graphics as ffi, graphics::FloatRect, system::Vector2f};
24

35
/// Define a 3x3 transform matrix.
@@ -195,3 +197,58 @@ impl Default for Transform {
195197
Self::IDENTITY
196198
}
197199
}
200+
201+
impl Mul for Transform {
202+
type Output = Self;
203+
204+
fn mul(self, rhs: Self) -> Self::Output {
205+
let mut clone_self = self;
206+
clone_self.combine(&rhs);
207+
clone_self
208+
}
209+
}
210+
211+
impl MulAssign for Transform {
212+
fn mul_assign(&mut self, rhs: Self) {
213+
self.combine(&rhs);
214+
}
215+
}
216+
217+
impl Mul<Vector2f> for Transform {
218+
type Output = Vector2f;
219+
220+
fn mul(self, rhs: Vector2f) -> Self::Output {
221+
self.transform_point(rhs)
222+
}
223+
}
224+
225+
#[cfg(test)]
226+
mod test {
227+
use super::*;
228+
229+
#[test]
230+
fn test_mul() {
231+
let test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
232+
let result = Transform::new(30., 36., 42., 66., 81., 96., 102., 126., 150.);
233+
234+
assert_eq!(test_transform * test_transform, result);
235+
}
236+
237+
#[test]
238+
fn test_mulassign() {
239+
let mut test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
240+
let result = Transform::new(30., 36., 42., 66., 81., 96., 102., 126., 150.);
241+
242+
test_transform *= test_transform;
243+
assert_eq!(test_transform, result);
244+
}
245+
246+
#[test]
247+
fn test_mul_vector() {
248+
let test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
249+
let test_point = Vector2f::new(2., 3.);
250+
let result = Vector2f::new(11., 29.);
251+
252+
assert_eq!(test_transform * test_point, result);
253+
}
254+
}

0 commit comments

Comments
 (0)