pico_detect/traits/
region.rs

1use nalgebra::Point2;
2
3/// A trait defining a rectangular region with methods to access its properties.
4pub trait Region {
5    /// Returns the left coordinate of the region.
6    fn left(&self) -> i32;
7    /// Returns the top coordinate of the region.
8    fn top(&self) -> i32;
9    /// Returns the width of the region.
10    fn width(&self) -> u32;
11    /// Returns the height of the region.
12    fn height(&self) -> u32;
13
14    /// Returns the right coordinate of the region.
15    #[inline]
16    fn right(&self) -> i32 {
17        self.left() + (self.width() - 1) as i32
18    }
19
20    /// Returns the bottom coordinate of the region.
21    #[inline]
22    fn bottom(&self) -> i32 {
23        self.top() + (self.height() - 1) as i32
24    }
25
26    /// Checks if the region is square.
27    #[inline]
28    fn is_square(&self) -> bool {
29        self.width() == self.height()
30    }
31
32    /// Checks if the region contains a point with coordinates `(x, y)`.
33    #[inline]
34    fn contains(&self, x: i32, y: i32) -> bool {
35        self.left() <= x && self.top() <= y && self.right() >= x && self.bottom() >= y
36    }
37
38    /// Returns the center point of the region.
39    #[inline]
40    fn center(&self) -> Point2<i32> {
41        Point2::new(
42            self.left() + (self.width() / 2 + 1) as i32,
43            self.top() + (self.height() / 2 + 1) as i32,
44        )
45    }
46
47    /// Returns the top-left corner of the region as a point.
48    #[inline]
49    fn top_left(&self) -> Point2<i32> {
50        Point2::new(self.left(), self.top())
51    }
52
53    /// Returns the square area of the region.
54    #[inline]
55    fn square(&self) -> u32 {
56        self.width() * self.height()
57    }
58}