51 lines
1.1 KiB
Rust
51 lines
1.1 KiB
Rust
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
pub struct Point {
|
|
pub x: f32,
|
|
pub y: f32,
|
|
}
|
|
|
|
impl Point {
|
|
pub const fn new(x: f32, y: f32) -> Self {
|
|
Self { x, y }
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
pub struct Rect {
|
|
pub x: f32,
|
|
pub y: f32,
|
|
pub width: f32,
|
|
pub height: f32,
|
|
}
|
|
|
|
impl Rect {
|
|
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
|
|
Self {
|
|
x,
|
|
y,
|
|
width,
|
|
height,
|
|
}
|
|
}
|
|
|
|
pub fn contains(self, point: Point) -> bool {
|
|
point.x >= self.x
|
|
&& point.x <= self.x + self.width
|
|
&& point.y >= self.y
|
|
&& point.y <= self.y + self.height
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn rectangle_contains_edges_but_not_outside_points() {
|
|
let rect = Rect::new(10.0, 20.0, 30.0, 40.0);
|
|
assert!(rect.contains(Point::new(10.0, 20.0)));
|
|
assert!(rect.contains(Point::new(40.0, 60.0)));
|
|
assert!(!rect.contains(Point::new(9.9, 20.0)));
|
|
}
|
|
}
|