use core::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Point<T = f32> {
pub x: T,
pub y: T,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vector<T = f32> {
pub x: T,
pub y: T,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Size<T = f32> {
pub width: T,
pub height: T,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rect<T = f32> {
pub x: T,
pub y: T,
pub width: T,
pub height: T,
}
impl<T> Point<T> {
pub const fn new(x: T, y: T) -> Self {
Self { x, y }
}
}
impl Point {
pub const ORIGIN: Self = Self::new(0.0, 0.0);
#[must_use]
pub fn distance(self, other: Self) -> f32 {
(self - other).length()
}
}
impl<T> Vector<T> {
pub const fn new(x: T, y: T) -> Self {
Self { x, y }
}
}
impl Vector {
pub const ZERO: Self = Self::new(0.0, 0.0);
#[must_use]
pub fn length(self) -> f32 {
self.x.hypot(self.y)
}
}
impl<T> Size<T> {
pub const fn new(width: T, height: T) -> Self {
Self { width, height }
}
}
impl Size {
pub const ZERO: Self = Self::new(0.0, 0.0);
#[must_use]
pub fn area(self) -> f32 {
self.width * self.height
}
}
impl<T: Copy> Rect<T> {
pub const fn new(position: Point<T>, size: Size<T>) -> Self {
Self {
x: position.x,
y: position.y,
width: size.width,
height: size.height,
}
}
pub const fn position(self) -> Point<T> {
Point::new(self.x, self.y)
}
pub const fn size(self) -> Size<T> {
Size::new(self.width, self.height)
}
}
impl Rect {
pub const ZERO: Self = Self::new(Point::ORIGIN, Size::ZERO);
#[must_use]
pub fn center(self) -> Point {
Point::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
}
#[must_use]
pub fn area(self) -> f32 {
self.size().area()
}
#[must_use]
pub fn contains(self, point: Point) -> bool {
point.x >= self.x
&& point.y >= self.y
&& point.x < self.x + self.width
&& point.y < self.y + self.height
}
#[must_use]
pub fn intersection(self, other: Self) -> Option<Self> {
let left = self.x.max(other.x);
let top = self.y.max(other.y);
let right = (self.x + self.width).min(other.x + other.width);
let bottom = (self.y + self.height).min(other.y + other.height);
if right <= left || bottom <= top {
return None;
}
Some(Self {
x: left,
y: top,
width: right - left,
height: bottom - top,
})
}
#[must_use]
pub fn is_within(self, other: Self) -> bool {
self.x >= other.x
&& self.y >= other.y
&& self.x + self.width <= other.x + other.width
&& self.y + self.height <= other.y + other.height
}
}
impl<T> From<(T, T)> for Point<T> {
fn from((x, y): (T, T)) -> Self {
Self::new(x, y)
}
}
impl<T> From<(T, T)> for Vector<T> {
fn from((x, y): (T, T)) -> Self {
Self::new(x, y)
}
}
impl<T> From<(T, T)> for Size<T> {
fn from((width, height): (T, T)) -> Self {
Self::new(width, height)
}
}
impl<T: Add<Output = T>> Add<Vector<T>> for Point<T> {
type Output = Self;
fn add(self, v: Vector<T>) -> Self {
Self::new(self.x + v.x, self.y + v.y)
}
}
impl<T: Sub<Output = T>> Sub<Vector<T>> for Point<T> {
type Output = Self;
fn sub(self, v: Vector<T>) -> Self {
Self::new(self.x - v.x, self.y - v.y)
}
}
impl<T: Sub<Output = T>> Sub for Point<T> {
type Output = Vector<T>;
fn sub(self, other: Self) -> Vector<T> {
Vector::new(self.x - other.x, self.y - other.y)
}
}
impl<T: AddAssign> AddAssign<Vector<T>> for Point<T> {
fn add_assign(&mut self, v: Vector<T>) {
self.x += v.x;
self.y += v.y;
}
}
impl<T: SubAssign> SubAssign<Vector<T>> for Point<T> {
fn sub_assign(&mut self, v: Vector<T>) {
self.x -= v.x;
self.y -= v.y;
}
}
impl<T: Add<Output = T>> Add for Vector<T> {
type Output = Self;
fn add(self, other: Self) -> Self {
Self::new(self.x + other.x, self.y + other.y)
}
}
impl<T: Sub<Output = T>> Sub for Vector<T> {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self::new(self.x - other.x, self.y - other.y)
}
}
impl<T: Neg<Output = T>> Neg for Vector<T> {
type Output = Self;
fn neg(self) -> Self {
Self::new(-self.x, -self.y)
}
}
impl<T: Mul<Output = T> + Copy> Mul<T> for Vector<T> {
type Output = Self;
fn mul(self, factor: T) -> Self {
Self::new(self.x * factor, self.y * factor)
}
}
impl<T: Mul<Output = T> + Copy> Mul<T> for Size<T> {
type Output = Self;
fn mul(self, factor: T) -> Self {
Self::new(self.width * factor, self.height * factor)
}
}
impl<T: Add<Output = T>> Add<Vector<T>> for Rect<T> {
type Output = Self;
fn add(self, v: Vector<T>) -> Self {
Self {
x: self.x + v.x,
y: self.y + v.y,
width: self.width,
height: self.height,
}
}
}
impl<T: Sub<Output = T>> Sub<Vector<T>> for Rect<T> {
type Output = Self;
fn sub(self, v: Vector<T>) -> Self {
Self {
x: self.x - v.x,
y: self.y - v.y,
width: self.width,
height: self.height,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn point_and_vector_arithmetic() {
let p = Point::new(1.0, 2.0);
let v = Vector::new(3.0, 4.0);
assert_eq!(p + v, Point::new(4.0, 6.0));
assert_eq!(p + v - v, p);
assert_eq!(Point::new(4.0, 6.0) - p, v);
assert_eq!(v + v, Vector::new(6.0, 8.0));
assert_eq!(v - v, Vector::ZERO);
assert_eq!(-v, Vector::new(-3.0, -4.0));
assert_eq!(v * 2.0, Vector::new(6.0, 8.0));
assert_eq!(v.length(), 5.0);
assert_eq!(Point::ORIGIN.distance(Point::new(3.0, 4.0)), 5.0);
let mut moved = p;
moved += v;
moved -= Vector::new(1.0, 1.0);
assert_eq!(moved, Point::new(3.0, 5.0));
}
#[test]
fn integer_geometry_works_too() {
let p: Point<i32> = Point::new(1, 2) + Vector::new(1, 1);
assert_eq!(p, Point::new(2, 3));
assert_eq!(Size::new(2_u32, 3) * 2, Size::new(4, 6));
}
#[test]
fn tuples_convert() {
assert_eq!(Point::from((1.0, 2.0)), Point::new(1.0, 2.0));
assert_eq!(Vector::from((1.0, 2.0)), Vector::new(1.0, 2.0));
assert_eq!(Size::from((1.0, 2.0)), Size::new(1.0, 2.0));
}
#[test]
fn rect_parts_and_centre() {
let r = Rect::new(Point::new(10.0, 20.0), Size::new(30.0, 40.0));
assert_eq!(r.position(), Point::new(10.0, 20.0));
assert_eq!(r.size(), Size::new(30.0, 40.0));
assert_eq!(r.center(), Point::new(25.0, 40.0));
assert_eq!(r.area(), 1200.0);
assert_eq!(
r + Vector::new(1.0, 1.0),
Rect::new(Point::new(11.0, 21.0), r.size())
);
assert_eq!(r + Vector::new(1.0, 1.0) - Vector::new(1.0, 1.0), r);
assert_eq!(Rect::ZERO.area(), 0.0);
}
#[test]
fn contains_is_half_open() {
let r = Rect::new(Point::ORIGIN, Size::new(10.0, 10.0));
assert!(r.contains(Point::new(0.0, 0.0)));
assert!(r.contains(Point::new(9.9, 9.9)));
assert!(!r.contains(Point::new(10.0, 5.0)));
assert!(!r.contains(Point::new(5.0, 10.0)));
assert!(!r.contains(Point::new(-0.1, 5.0)));
}
#[test]
fn intersection_of_overlapping_touching_and_apart() {
let a = Rect::new(Point::ORIGIN, Size::new(10.0, 10.0));
let b = Rect::new(Point::new(5.0, 5.0), Size::new(10.0, 10.0));
assert_eq!(
a.intersection(b),
Some(Rect::new(Point::new(5.0, 5.0), Size::new(5.0, 5.0)))
);
let touching = Rect::new(Point::new(10.0, 0.0), Size::new(5.0, 5.0));
assert_eq!(a.intersection(touching), None);
let apart = Rect::new(Point::new(50.0, 50.0), Size::new(5.0, 5.0));
assert_eq!(a.intersection(apart), None);
}
#[test]
fn is_within_includes_equal_edges() {
let outer = Rect::new(Point::ORIGIN, Size::new(10.0, 10.0));
assert!(outer.is_within(outer));
assert!(Rect::new(Point::new(1.0, 1.0), Size::new(2.0, 2.0)).is_within(outer));
assert!(!Rect::new(Point::new(9.0, 9.0), Size::new(2.0, 2.0)).is_within(outer));
}
}