use num_traits::{NumCast, One, Zero};
use std::ops::{Add, Div, Sub};
use crate::base::{cmp, Vec2};
use crate::shape::Point;
use crate::traits::{BoundingBox, Cast, MapPoint, ToPolygon, TryBoundingBox};
use super::Polygon;
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, Default)]
pub struct Rect<T> {
pub lower_left: Point<T>,
pub upper_right: Point<T>,
}
impl<T: PartialOrd + Copy> Rect<T> {
pub fn new(c1: impl Into<Point<T>>, c2: impl Into<Point<T>>) -> Self {
let p1 = c1.into();
let p2 = c2.into();
let (x1, x2) = if p1.x < p2.x {
(p1.x, p2.x)
} else {
(p2.x, p1.x)
};
let (y1, y2) = if p1.y < p2.y {
(p1.y, p2.y)
} else {
(p2.y, p1.y)
};
Rect {
lower_left: Point::new(x1, y1),
upper_right: Point::new(x2, y2),
}
}
}
impl<T: Copy> Rect<T> {
#[inline]
pub fn lower_left(&self) -> Point<T> {
self.lower_left
}
#[inline]
pub fn upper_left(&self) -> Point<T> {
Point::new(self.lower_left.x, self.upper_right.y)
}
#[inline]
pub fn upper_right(&self) -> Point<T> {
self.upper_right
}
#[inline]
pub fn lower_right(&self) -> Point<T> {
Point::new(self.upper_right.x, self.lower_left.y)
}
}
impl<T: PartialOrd + Copy> Rect<T> {
pub fn contains_point(&self, p: Point<T>) -> bool {
self.lower_left.x <= p.x
&& p.x <= self.upper_right.x
&& self.lower_left.y <= p.y
&& p.y <= self.upper_right.y
}
pub fn contains_point_exclusive(&self, p: Point<T>) -> bool {
self.lower_left.x < p.x
&& p.x < self.upper_right.x
&& self.lower_left.y < p.y
&& p.y < self.upper_right.y
}
pub fn contains_rectangle(&self, other: &Self) -> bool {
self.contains_point(other.lower_left) && self.contains_point(other.upper_right)
}
pub fn contains_rectangle_exclusive(&self, other: &Self) -> bool {
self.contains_point_exclusive(other.lower_left)
&& self.contains_point_exclusive(other.upper_right)
}
pub fn touches(&self, other: &Self) -> bool {
!(self.lower_left.x > other.upper_right.x
|| self.lower_left.y > other.upper_right.y
|| self.upper_right.x < other.lower_left.x
|| self.upper_right.y < other.lower_left.y)
}
pub fn intersection(&self, other: &Self) -> Option<Self> {
let llx = cmp::max(self.lower_left.x, other.lower_left.x);
let lly = cmp::max(self.lower_left.y, other.lower_left.y);
let urx = cmp::min(self.upper_right.x, other.upper_right.x);
let ury = cmp::min(self.upper_right.y, other.upper_right.y);
if llx < urx && lly < ury {
Some(Rect::new((llx, lly), (urx, ury)))
} else {
None
}
}
pub fn intersection_inclusive_bounds(&self, other: &Self) -> Option<Self> {
let llx = cmp::max(self.lower_left.x, other.lower_left.x);
let lly = cmp::max(self.lower_left.y, other.lower_left.y);
let urx = cmp::min(self.upper_right.x, other.upper_right.x);
let ury = cmp::min(self.upper_right.y, other.upper_right.y);
if llx <= urx && lly <= ury {
Some(Rect::new((llx, lly), (urx, ury)))
} else {
None
}
}
pub fn add_point(&self, point: Point<T>) -> Self {
Rect::new(
Point::new(
cmp::min(self.lower_left.x, point.x),
cmp::min(self.lower_left.y, point.y),
),
Point::new(
cmp::max(self.upper_right.x, point.x),
cmp::max(self.upper_right.y, point.y),
),
)
}
pub fn add_rect(&self, rect: &Self) -> Self {
self.add_point(rect.lower_left).add_point(rect.upper_right)
}
}
impl<T: Sub<Output = T> + Copy + Ord + Zero> Rect<T> {
pub fn distance_to_point(&self, p: Point<T>) -> Vec2<T> {
let ll = self.lower_left();
let ul = self.upper_right();
let dx_neg = (p.x - ll.x).min(Zero::zero());
let dx_pos = (p.x - ul.x).max(Zero::zero());
let dx = dx_neg + dx_pos;
let dy_neg = (p.y - ll.y).min(Zero::zero());
let dy_pos = (p.y - ul.y).max(Zero::zero());
let dy = dy_neg + dy_pos;
Vec2::new(dx, dy)
}
}
impl<T: Copy + Sub<Output = T>> Rect<T> {
#[inline]
pub fn width(&self) -> T {
self.upper_right.x - self.lower_left.x
}
#[inline]
pub fn height(&self) -> T {
self.upper_right.y - self.lower_left.y
}
}
impl<T: Copy + Add<Output = T> + Div<Output = T> + One> Rect<T> {
pub fn center(&self) -> Point<T> {
let two = T::one() + T::one();
(self.lower_left() + self.upper_right()) / two
}
}
impl<T: Copy + Add<Output = T> + Sub<Output = T> + PartialOrd> Rect<T> {
pub fn sized(&self, add_x: T, add_y: T) -> Self {
Rect::new(
(self.lower_left.x - add_x, self.lower_left.y - add_y),
(self.upper_right.x + add_x, self.upper_right.y + add_y),
)
}
pub fn sized_isotropic(&self, add: T) -> Self {
self.sized(add, add)
}
}
impl<'a, T> IntoIterator for &'a Rect<T>
where
T: Copy,
{
type Item = Point<T>;
type IntoIter = std::array::IntoIter<Self::Item, 4>;
fn into_iter(self) -> Self::IntoIter {
[
self.lower_left(),
self.lower_right(),
self.upper_right(),
self.upper_left(),
]
.into_iter()
}
}
impl<T> IntoIterator for Rect<T>
where
T: Copy,
{
type Item = Point<T>;
type IntoIter = std::array::IntoIter<Self::Item, 4>;
fn into_iter(self) -> Self::IntoIter {
(&self).into_iter()
}
}
impl<T: Copy> MapPoint<T> for Rect<T> {
fn transform<F>(&self, transformate: F) -> Self
where
F: Fn(Point<T>) -> Point<T>
{
Self {
lower_left: transformate(self.lower_left),
upper_right: transformate(self.upper_right)
}
}
}
impl<T: Copy> BoundingBox<T> for Rect<T> {
fn bounding_box(&self) -> Rect<T> {
*self
}
}
impl<T: Copy> TryBoundingBox<T> for Rect<T> {
fn try_bounding_box(&self) -> Option<Rect<T>> {
Some(self.bounding_box())
}
}
impl<Src: Copy + NumCast, Dst: Copy + NumCast + PartialOrd> Cast<Src, Dst> for Rect<Src> {
type Output = Rect<Dst>;
fn try_cast(&self) -> Option<Self::Output> {
match (self.lower_left.try_cast(), self.upper_right.try_cast()) {
(Some(ll), Some(ur)) => Some(Rect::new(ll, ur)),
_ => None,
}
}
}
impl<T: Copy> ToPolygon<T> for Rect<T> {
fn to_polygon(&self) -> Polygon<T> {
Polygon::from(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_distance_to_point() {
let rect = Rect::new((10, 10), (20, 20));
assert_eq!(rect.distance_to_point((15, 15).into()).norm1(), 0);
assert_eq!(rect.distance_to_point((10, 10).into()).norm1(), 0);
assert_eq!(rect.distance_to_point((10, 20).into()).norm1(), 0);
assert_eq!(rect.distance_to_point((0, 0).into()).norm1(), 20);
assert_eq!(rect.distance_to_point((0, 5).into()).norm1(), 15);
assert_eq!(rect.distance_to_point((0, 10).into()).norm1(), 10);
assert_eq!(rect.distance_to_point((0, 15).into()).norm1(), 10);
assert_eq!(rect.distance_to_point((0, 20).into()).norm1(), 10);
assert_eq!(rect.distance_to_point((0, 25).into()).norm1(), 15);
}
#[test]
fn test_rect_intersection() {
let a = Rect::new((0, 0), (2, 4));
let b = Rect::new((1, 2), (3, 5));
assert_eq!(a.intersection(&b), Some(Rect::new((1, 2), (2, 4))));
let a = Rect::new((0, 0), (2, 2));
let b = Rect::new((1, 1), (3, 3));
assert_eq!(a.intersection(&b), Some(Rect::new((1, 1), (2, 2))));
let a = Rect::new((0, 0), (1, 1));
let b = Rect::new((2, 2), (3, 3));
assert_eq!(a.intersection(&b), None);
let a = Rect::new((0, 0), (2, 2));
let b = Rect::new((1, 2), (5, 5));
assert_eq!(a.intersection(&b), None);
}
}