makepad_vector/geometry/
linear_transformation.rs1use crate::geometry::{Point, Transformation, Vector};
2
3#[derive(Clone, Copy, Debug, PartialEq)]
4#[repr(C)]
5pub struct LinearTransformation {
6 pub x: Vector,
7 pub y: Vector,
8}
9
10impl LinearTransformation {
11 pub fn new(x: Vector, y: Vector) -> LinearTransformation {
12 LinearTransformation { x, y }
13 }
14
15 pub fn identity() -> LinearTransformation {
16 LinearTransformation::new(Vector::new(1.0, 0.0), Vector::new(0.0, 1.0))
17 }
18
19 pub fn scaling(v: Vector) -> LinearTransformation {
20 LinearTransformation::new(Vector::new(v.x, 0.0), Vector::new(0.0, v.y))
21 }
22
23 pub fn uniform_scaling(k: f64) -> LinearTransformation {
24 LinearTransformation::scaling(Vector::new(k, k))
25 }
26
27 pub fn scale(self, v: Vector) -> LinearTransformation {
28 LinearTransformation::new(self.x * v.x, self.y * v.y)
29 }
30
31 pub fn uniform_scale(self, k: f64) -> LinearTransformation {
32 LinearTransformation::new(self.x * k, self.y * k)
33 }
34
35 pub fn compose(self, other: LinearTransformation) -> LinearTransformation {
36 LinearTransformation::new(
37 self.transform_vector(other.x),
38 self.transform_vector(other.y),
39 )
40 }
41}
42
43impl Transformation for LinearTransformation {
44 fn transform_point(&self, p: Point) -> Point {
45 (self.x * p.x + self.y * p.y).to_point()
46 }
47
48 fn transform_vector(&self, v: Vector) -> Vector {
49 self.x * v.x + self.y * v.y
50 }
51}