Skip to content

Commit fb59e47

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 fb59e47

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/graphics/transform.rs

Lines changed: 62 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,63 @@ impl Default for Transform {
195197
Self::IDENTITY
196198
}
197199
}
200+
201+
impl Mul for Transform {
202+
type Output = Self;
203+
204+
/// Combines two transformations
205+
///
206+
/// Equivelant to calling `left.clone().combine(right)`
207+
///
208+
/// Returns new combined Transform
209+
/// ```
210+
/// # use sfml::graphics::Transform;
211+
/// let test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
212+
/// let result = Transform::new(30., 36., 42., 66., 81., 96., 102., 126., 150.);
213+
/// assert_eq!(test_transform * test_transform, result);
214+
/// ```
215+
fn mul(self, rhs: Self) -> Self::Output {
216+
let mut clone_self = self;
217+
clone_self.combine(&rhs);
218+
clone_self
219+
}
220+
}
221+
222+
impl MulAssign for Transform {
223+
/// Combines two transformations
224+
///
225+
/// Equivelant to calling `left.combine(right)`
226+
///
227+
/// Returns new combined Transform
228+
/// ```
229+
/// # use sfml::graphics::Transform;
230+
/// let mut test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
231+
/// let result = Transform::new(30., 36., 42., 66., 81., 96., 102., 126., 150.);
232+
/// test_transform *= test_transform;
233+
/// assert_eq!(test_transform, result);
234+
/// ```
235+
fn mul_assign(&mut self, rhs: Self) {
236+
self.combine(&rhs);
237+
}
238+
}
239+
240+
impl Mul<Vector2f> for Transform {
241+
type Output = Vector2f;
242+
243+
/// Transforms a point
244+
///
245+
/// Equivalent to calling `left.transform_point(right)`
246+
///
247+
/// Returns a new transformed points
248+
/// ```
249+
/// # use sfml::graphics::Transform;
250+
/// # use sfml::system::Vector2f;
251+
/// let test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
252+
/// let test_point = Vector2f::new(2., 3.);
253+
/// let result = Vector2f::new(11., 29.);
254+
/// assert_eq!(test_transform * test_point, result);
255+
/// ```
256+
fn mul(self, rhs: Vector2f) -> Self::Output {
257+
self.transform_point(rhs)
258+
}
259+
}

0 commit comments

Comments
 (0)