smithay/src/utils/rectangle.rs

47 lines
1.5 KiB
Rust
Raw Normal View History

2017-09-22 08:46:00 +00:00
/// A rectangle defined by its top-left corner and dimensions
#[derive(Copy, Clone, Debug, Default, PartialEq)]
2017-09-22 08:46:00 +00:00
pub struct Rectangle {
/// horizontal position of the top-left corner of the rectangle, in surface coordinates
2017-09-22 08:46:00 +00:00
pub x: i32,
/// vertical position of the top-left corner of the rectangle, in surface coordinates
2017-09-22 08:46:00 +00:00
pub y: i32,
/// width of the rectangle
pub width: i32,
/// height of the rectangle
pub height: i32,
}
impl Rectangle {
/// Checks whether given point is inside a rectangle
pub fn contains(self, point: (i32, i32)) -> bool {
2017-09-22 08:46:00 +00:00
let (x, y) = point;
2017-09-22 12:53:39 +00:00
(x >= self.x) && (x < self.x + self.width) && (y >= self.y) && (y < self.y + self.height)
2017-09-22 08:46:00 +00:00
}
/// Checks whether a given rectangle overlaps with this one
pub fn overlaps(self, other: &Rectangle) -> bool {
// if the rectangle is not outside of the other
// they must overlap
!(
// self is left of other
self.x + self.width < other.x
// self is right of other
|| self.x > other.x + other.width
// self is above of other
|| self.y + self.height < other.y
// self is below of other
|| self.y > other.y + other.height
)
}
/// Scales the dimensions of this rectangle by given factor
pub fn scale(self, factor: i32) -> Rectangle {
Rectangle {
x: self.x * factor,
y: self.y * factor,
width: self.width * factor,
height: self.height * factor,
}
}
2017-09-22 12:53:39 +00:00
}