Skip to main content

repose_core/
geometry.rs

1#[derive(Clone, Copy, Debug, Default, PartialEq)]
2pub struct Vec2 {
3    pub x: f32,
4    pub y: f32,
5}
6
7impl Vec2 {
8    pub const ZERO: Vec2 = Vec2 { x: 0.0, y: 0.0 };
9}
10
11impl std::ops::Add for Vec2 {
12    type Output = Vec2;
13    fn add(self, other: Vec2) -> Vec2 {
14        Vec2 {
15            x: self.x + other.x,
16            y: self.y + other.y,
17        }
18    }
19}
20
21impl std::ops::Sub for Vec2 {
22    type Output = Vec2;
23    fn sub(self, other: Vec2) -> Vec2 {
24        Vec2 {
25            x: self.x - other.x,
26            y: self.y - other.y,
27        }
28    }
29}
30
31impl std::ops::Neg for Vec2 {
32    type Output = Vec2;
33    fn neg(self) -> Vec2 {
34        Vec2 {
35            x: -self.x,
36            y: -self.y,
37        }
38    }
39}
40
41#[derive(Clone, Copy, Debug, Default, PartialEq)]
42pub struct Size {
43    pub width: f32,
44    pub height: f32,
45}
46
47#[derive(Clone, Copy, Debug, Default, PartialEq)]
48pub struct Rect {
49    pub x: f32,
50    pub y: f32,
51    pub w: f32,
52    pub h: f32,
53}
54
55impl Rect {
56    pub fn contains(&self, p: Vec2) -> bool {
57        p.x >= self.x && p.x <= self.x + self.w && p.y >= self.y && p.y <= self.y + self.h
58    }
59}
60
61#[derive(Clone, Copy, Debug, Default, PartialEq)]
62pub struct Transform {
63    pub translate_x: f32,
64    pub translate_y: f32,
65    pub scale_x: f32,
66    pub scale_y: f32,
67    pub rotate: f32, // radians
68}
69
70impl Transform {
71    pub fn identity() -> Self {
72        Self {
73            translate_x: 0.0,
74            translate_y: 0.0,
75            scale_x: 1.0,
76            scale_y: 1.0,
77            rotate: 0.0,
78        }
79    }
80
81    pub fn translate(x: f32, y: f32) -> Self {
82        Self {
83            translate_x: x,
84            translate_y: y,
85            scale_x: 1.0,
86            scale_y: 1.0,
87            rotate: 0.0,
88        }
89    }
90
91    pub fn apply_to_point(&self, p: Vec2) -> Vec2 {
92        // Apply in order: scale, rotate, translate
93        let mut x = p.x * self.scale_x;
94        let mut y = p.y * self.scale_y;
95
96        if self.rotate != 0.0 {
97            let cos = self.rotate.cos();
98            let sin = self.rotate.sin();
99            let nx = x * cos - y * sin;
100            let ny = x * sin + y * cos;
101            x = nx;
102            y = ny;
103        }
104
105        Vec2 {
106            x: x + self.translate_x,
107            y: y + self.translate_y,
108        }
109    }
110
111    pub fn apply_to_rect(&self, r: Rect) -> Rect {
112        let corners = [
113            Vec2 { x: r.x, y: r.y },
114            Vec2 {
115                x: r.x + r.w,
116                y: r.y,
117            },
118            Vec2 {
119                x: r.x,
120                y: r.y + r.h,
121            },
122            Vec2 {
123                x: r.x + r.w,
124                y: r.y + r.h,
125            },
126        ];
127        let mut min_x = f32::MAX;
128        let mut min_y = f32::MAX;
129        let mut max_x = f32::MIN;
130        let mut max_y = f32::MIN;
131        for c in corners {
132            let p = self.apply_to_point(c);
133            min_x = min_x.min(p.x);
134            min_y = min_y.min(p.y);
135            max_x = max_x.max(p.x);
136            max_y = max_y.max(p.y);
137        }
138        Rect {
139            x: min_x,
140            y: min_y,
141            w: max_x - min_x,
142            h: max_y - min_y,
143        }
144    }
145
146    /// Compose two transforms: `self` then `other` (post-multiply).
147    ///
148    /// Returns a transform such that `combined.apply_to_point(p) ==
149    /// other.apply_to_point(self.apply_to_point(p))`.
150    ///
151    /// **Limitation:** the T*R*S representation is not closed under matrix
152    /// multiplication when both transforms have rotation *and* non-uniform
153    /// scale. In that case the result is an approximation (off-diagonal
154    /// rotation/scale coupling is dropped). For UI use (uniform scale or no
155    /// rotation) this is exact.
156    pub fn combine(&self, other: &Transform) -> Transform {
157        let c = self.rotate.cos();
158        let s = self.rotate.sin();
159
160        Transform {
161            translate_x: other.translate_x * self.scale_x * c
162                - other.translate_y * self.scale_y * s
163                + self.translate_x,
164            translate_y: other.translate_x * self.scale_x * s
165                + other.translate_y * self.scale_y * c
166                + self.translate_y,
167            scale_x: self.scale_x * other.scale_x,
168            scale_y: self.scale_y * other.scale_y,
169            rotate: self.rotate + other.rotate,
170        }
171    }
172}