mkgraphic 0.4.0

A Rust port of the cycfi/elements GUI framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Rectangle type for 2D regions.

use super::point::{Axis, Extent, Point};

/// A rectangle defined by its left, top, right, and bottom edges.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Rect {
    pub left: f32,
    pub top: f32,
    pub right: f32,
    pub bottom: f32,
}

impl Rect {
    /// Creates a new rectangle from edge coordinates.
    #[inline]
    pub const fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
        Self {
            left,
            top,
            right,
            bottom,
        }
    }

    /// Creates a rectangle from origin point and size.
    #[inline]
    pub fn from_origin_size(origin: Point, size: Extent) -> Self {
        Self {
            left: origin.x,
            top: origin.y,
            right: origin.x + size.x,
            bottom: origin.y + size.y,
        }
    }

    /// Creates a rectangle from two corner points.
    #[inline]
    pub fn from_points(p1: Point, p2: Point) -> Self {
        Self {
            left: p1.x.min(p2.x),
            top: p1.y.min(p2.y),
            right: p1.x.max(p2.x),
            bottom: p1.y.max(p2.y),
        }
    }

    /// Creates an empty rectangle at the origin.
    #[inline]
    pub const fn zero() -> Self {
        Self {
            left: 0.0,
            top: 0.0,
            right: 0.0,
            bottom: 0.0,
        }
    }

    /// Returns the width of the rectangle.
    #[inline]
    pub fn width(&self) -> f32 {
        self.right - self.left
    }

    /// Sets the width, keeping the left edge fixed.
    #[inline]
    pub fn set_width(&mut self, width: f32) {
        self.right = self.left + width;
    }

    /// Returns the height of the rectangle.
    #[inline]
    pub fn height(&self) -> f32 {
        self.bottom - self.top
    }

    /// Sets the height, keeping the top edge fixed.
    #[inline]
    pub fn set_height(&mut self, height: f32) {
        self.bottom = self.top + height;
    }

    /// Returns the size as an Extent.
    #[inline]
    pub fn size(&self) -> Extent {
        Extent::new(self.width(), self.height())
    }

    /// Sets the size, keeping the top-left corner fixed.
    #[inline]
    pub fn set_size(&mut self, size: Extent) {
        self.right = self.left + size.x;
        self.bottom = self.top + size.y;
    }

    /// Returns the top-left corner.
    #[inline]
    pub fn top_left(&self) -> Point {
        Point::new(self.left, self.top)
    }

    /// Returns the top-right corner.
    #[inline]
    pub fn top_right(&self) -> Point {
        Point::new(self.right, self.top)
    }

    /// Returns the bottom-left corner.
    #[inline]
    pub fn bottom_left(&self) -> Point {
        Point::new(self.left, self.bottom)
    }

    /// Returns the bottom-right corner.
    #[inline]
    pub fn bottom_right(&self) -> Point {
        Point::new(self.right, self.bottom)
    }

    /// Returns the center point.
    #[inline]
    pub fn center(&self) -> Point {
        Point::new(
            self.left + self.width() / 2.0,
            self.top + self.height() / 2.0,
        )
    }

    /// Returns true if the rectangle is empty (zero width or height).
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.left == self.right || self.top == self.bottom
    }

    /// Returns true if the rectangle is valid (right >= left and bottom >= top).
    #[inline]
    pub fn is_valid(&self) -> bool {
        self.left <= self.right && self.top <= self.bottom
    }

    /// Returns true if the point is inside the rectangle.
    #[inline]
    pub fn contains(&self, p: Point) -> bool {
        p.x >= self.left && p.x <= self.right && p.y >= self.top && p.y <= self.bottom
    }

    /// Returns true if this rectangle fully contains the other rectangle.
    #[inline]
    pub fn contains_rect(&self, other: &Rect) -> bool {
        other.left >= self.left
            && other.right <= self.right
            && other.top >= self.top
            && other.bottom <= self.bottom
    }

    /// Moves the rectangle by the given delta.
    #[inline]
    pub fn translate(self, dx: f32, dy: f32) -> Self {
        Self {
            left: self.left + dx,
            top: self.top + dy,
            right: self.right + dx,
            bottom: self.bottom + dy,
        }
    }

    /// Moves the rectangle to the given position.
    #[inline]
    pub fn move_to(self, x: f32, y: f32) -> Self {
        self.translate(x - self.left, y - self.top)
    }

    /// Insets the rectangle by the given amounts (shrinks it).
    #[inline]
    pub fn inset(self, x_inset: f32, y_inset: f32) -> Self {
        let mut r = Self {
            left: self.left + x_inset,
            top: self.top + y_inset,
            right: self.right - x_inset,
            bottom: self.bottom - y_inset,
        };

        if !r.is_valid() {
            r = Self::zero();
        }
        r
    }

    /// Expands the rectangle by the given amounts.
    #[inline]
    pub fn expand(self, x_expand: f32, y_expand: f32) -> Self {
        self.inset(-x_expand, -y_expand)
    }

    /// Returns the area of the rectangle.
    #[inline]
    pub fn area(&self) -> f32 {
        self.width() * self.height()
    }

    /// Returns the extent along the given axis.
    #[inline]
    pub fn extent(&self, axis: Axis) -> f32 {
        match axis {
            Axis::X => self.width(),
            Axis::Y => self.height(),
        }
    }

    /// Returns the minimum value along the given axis.
    #[inline]
    pub fn min(&self, axis: Axis) -> f32 {
        match axis {
            Axis::X => self.left,
            Axis::Y => self.top,
        }
    }

    /// Returns the maximum value along the given axis.
    #[inline]
    pub fn max(&self, axis: Axis) -> f32 {
        match axis {
            Axis::X => self.right,
            Axis::Y => self.bottom,
        }
    }

    /// Returns a mutable reference to the minimum value along the given axis.
    #[inline]
    pub fn min_mut(&mut self, axis: Axis) -> &mut f32 {
        match axis {
            Axis::X => &mut self.left,
            Axis::Y => &mut self.top,
        }
    }

    /// Returns a mutable reference to the maximum value along the given axis.
    #[inline]
    pub fn max_mut(&mut self, axis: Axis) -> &mut f32 {
        match axis {
            Axis::X => &mut self.right,
            Axis::Y => &mut self.bottom,
        }
    }

    /// Returns the intersection with another rectangle.
    #[inline]
    pub fn intersection(&self, other: Rect) -> Option<Rect> {
        let r = Rect {
            left: self.left.max(other.left),
            top: self.top.max(other.top),
            right: self.right.min(other.right),
            bottom: self.bottom.min(other.bottom),
        };

        if r.is_valid() && !r.is_empty() {
            Some(r)
        } else {
            None
        }
    }
}

/// Returns true if two rectangles intersect.
pub fn intersects(a: &Rect, b: &Rect) -> bool {
    a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom
}

/// Returns the intersection of two rectangles.
pub fn intersection(a: &Rect, b: &Rect) -> Option<Rect> {
    let r = Rect {
        left: a.left.max(b.left),
        top: a.top.max(b.top),
        right: a.right.min(b.right),
        bottom: a.bottom.min(b.bottom),
    };

    if r.is_valid() && !r.is_empty() {
        Some(r)
    } else {
        None
    }
}

/// Returns the union (bounding box) of two rectangles.
pub fn union(a: &Rect, b: &Rect) -> Rect {
    Rect {
        left: a.left.min(b.left),
        top: a.top.min(b.top),
        right: a.right.max(b.right),
        bottom: a.bottom.max(b.bottom),
    }
}

/// Centers a rectangle within an enclosing rectangle.
pub fn center(r: Rect, encl: &Rect) -> Rect {
    let dx = (encl.width() - r.width()) / 2.0;
    let dy = (encl.height() - r.height()) / 2.0;
    Rect {
        left: encl.left + dx,
        top: encl.top + dy,
        right: encl.left + dx + r.width(),
        bottom: encl.top + dy + r.height(),
    }
}

/// Horizontally centers a rectangle within an enclosing rectangle.
pub fn center_h(r: Rect, encl: &Rect) -> Rect {
    let dx = (encl.width() - r.width()) / 2.0;
    Rect {
        left: encl.left + dx,
        top: r.top,
        right: encl.left + dx + r.width(),
        bottom: r.bottom,
    }
}

/// Vertically centers a rectangle within an enclosing rectangle.
pub fn center_v(r: Rect, encl: &Rect) -> Rect {
    let dy = (encl.height() - r.height()) / 2.0;
    Rect {
        left: r.left,
        top: encl.top + dy,
        right: r.right,
        bottom: encl.top + dy + r.height(),
    }
}

/// Aligns a rectangle within an enclosing rectangle.
///
/// `x_align` and `y_align` should be between 0.0 and 1.0.
pub fn align(r: Rect, encl: &Rect, x_align: f32, y_align: f32) -> Rect {
    let dx = (encl.width() - r.width()) * x_align;
    let dy = (encl.height() - r.height()) * y_align;
    Rect {
        left: encl.left + dx,
        top: encl.top + dy,
        right: encl.left + dx + r.width(),
        bottom: encl.top + dy + r.height(),
    }
}

/// Clips a rectangle to fit within an enclosing rectangle.
pub fn clip(r: Rect, encl: &Rect) -> Rect {
    Rect {
        left: r.left.max(encl.left).min(encl.right),
        top: r.top.max(encl.top).min(encl.bottom),
        right: r.right.max(encl.left).min(encl.right),
        bottom: r.bottom.max(encl.top).min(encl.bottom),
    }
}

/// Creates a rectangle given an axis and coordinates.
pub fn make_rect(
    axis: Axis,
    this_axis_min: f32,
    other_axis_min: f32,
    this_axis_max: f32,
    other_axis_max: f32,
) -> Rect {
    match axis {
        Axis::X => Rect::new(this_axis_min, other_axis_min, this_axis_max, other_axis_max),
        Axis::Y => Rect::new(other_axis_min, this_axis_min, other_axis_max, this_axis_max),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rect_basic() {
        let r = Rect::new(10.0, 20.0, 110.0, 70.0);
        assert_eq!(r.width(), 100.0);
        assert_eq!(r.height(), 50.0);
        assert_eq!(r.area(), 5000.0);
    }

    #[test]
    fn test_rect_contains() {
        let r = Rect::new(0.0, 0.0, 100.0, 100.0);
        assert!(r.contains(Point::new(50.0, 50.0)));
        assert!(r.contains(Point::new(0.0, 0.0)));
        assert!(r.contains(Point::new(100.0, 100.0)));
        assert!(!r.contains(Point::new(-1.0, 50.0)));
        assert!(!r.contains(Point::new(101.0, 50.0)));
    }

    #[test]
    fn test_intersection() {
        let a = Rect::new(0.0, 0.0, 100.0, 100.0);
        let b = Rect::new(50.0, 50.0, 150.0, 150.0);
        let c = intersection(&a, &b).unwrap();
        assert_eq!(c, Rect::new(50.0, 50.0, 100.0, 100.0));
    }

    #[test]
    fn test_no_intersection() {
        let a = Rect::new(0.0, 0.0, 100.0, 100.0);
        let b = Rect::new(200.0, 200.0, 300.0, 300.0);
        assert!(intersection(&a, &b).is_none());
    }
}