From 2da16d8a389b2a774e561847ceb8ebc7d981916b Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 15 Aug 2017 12:15:52 -0500 Subject: [PATCH] textium::geometry::Rect --- textium/src/geometry.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 textium/src/geometry.rs diff --git a/textium/src/geometry.rs b/textium/src/geometry.rs new file mode 100644 index 0000000..324289d --- /dev/null +++ b/textium/src/geometry.rs @@ -0,0 +1,64 @@ +use std::ops::{Add, Sub, Mul}; + + +/// A rectangle. +#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)] +pub struct Rect { + x: T, y: T, + w: T, h: T, +} + +#[allow(dead_code)] +impl Rect + where T: Copy + Add + Sub + Mul + + Ord + PartialOrd +{ + /// Returns a new rectangle given the upper-left corner and its width + height. + pub fn new(x: T, y: T, w: T, h: T) -> Rect { + Rect { + x: x, y: y, + w: w, h: h, + } + } + + /// Returns a new rectangle given a pair of points in opposing corners. + pub fn from_points(x1: T, y1: T, x2: T, y2: T) -> Rect { + Rect { + x: x1, y: y1, + w: x2 - x1, h: y2 - y1 + } + } + + /// Return the position of the top edge of the rectangle. + #[inline(always)] + pub fn top(&self) -> T {self.y} + /// Return the position of the left edge of the rectangle. + #[inline(always)] + pub fn left(&self) -> T {self.x} + /// Return the position of the bottom edge of the rectangle. + #[inline(always)] + pub fn bottom(&self) -> T {self.y + self.h} + /// Return the position of the right edge of the rectangle. + #[inline(always)] + pub fn right(&self) -> T {self.x + self.w} + + /// Return the area of the rectangle. + #[inline(always)] + pub fn area(&self) -> T {self.x * self.y} + + /// Given this rectangle and another rectangle, return true if they intersect. + pub fn intersects(&self, other: &Rect) -> bool { + self.left() < other.right() && + self.right() > other.left() && + self.top() < other.bottom() && + self.bottom() > other.top() + } + + /// Return true if this rectangle completely contains another rectangle. + pub fn contains(&self, other: &Rect) -> bool { + self.left() <= other.left() && + self.right() >= other.right() && + self.top() <= other.top() && + self.bottom() >= other.bottom() + } +}