#[derive(Debug, Copy, Clone)]
pub struct Rect<N> {
top: N,
bottom: N,
left: N,
right: N,
}
impl<N> Rect<N> {
pub fn from_left_top_right_bottom(left: N, top: N, right: N, bottom: N) -> Self {
Rect {
top,
bottom,
left,
right,
}
}
pub fn from_corners(top_left: Point<N>, bottom_right: Point<N>) -> Self {
let (left, top) = top_left.into_xy();
let (bottom, right) = bottom_right.into_xy();
Rect {
top,
bottom,
left,
right,
}
}
pub fn into_left_top_bottom_right(self) -> (N, N, N, N) {
(self.left, self.top, self.bottom, self.right)
}
pub fn set_top(&mut self, top: N) {
self.top = top;
}
pub fn set_left(&mut self, left: N) {
self.left = left;
}
pub fn set_bottom(&mut self, bottom: N) {
self.bottom = bottom;
}
pub fn set_right(&mut self, right: N) {
self.right = right;
}
pub fn contains(&self, point: Point<N>) -> bool
where
N: PartialOrd,
{
let (x, y) = point.into_xy();
x >= self.left && x < self.right && y >= self.bottom && y < self.top
}
}
impl<N: Clone> Rect<N> {
pub fn top(&self) -> N {
self.top.clone()
}
pub fn bottom(&self) -> N {
self.bottom.clone()
}
pub fn left(&self) -> N {
self.left.clone()
}
pub fn right(&self) -> N {
self.right.clone()
}
}
#[derive(Debug, Copy, Clone)]
pub struct Point<N> {
x: N,
y: N,
}
impl<N> Point<N> {
pub fn from_xy(x: N, y: N) -> Self {
Point { x, y }
}
pub fn set_x(&mut self, x: N) {
self.x = x;
}
pub fn set_y(&mut self, y: N) {
self.y = y;
}
pub fn into_xy(self) -> (N, N) {
(self.x, self.y)
}
}
impl<N: Clone> Point<N> {
pub fn x(&self) -> N {
self.x.clone()
}
pub fn y(&self) -> N {
self.y.clone()
}
}
impl<N> From<(N, N)> for Point<N> {
fn from((x, y): (N, N)) -> Self {
Point::from_xy(x, y)
}
}