Browse Source

textium::geometry::Rect

master
Erin 8 years ago
parent
commit
2da16d8a38
1 changed files with 64 additions and 0 deletions
  1. +64
    -0
      textium/src/geometry.rs

+ 64
- 0
textium/src/geometry.rs View File

@ -0,0 +1,64 @@
use std::ops::{Add, Sub, Mul};
/// A rectangle.
#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)]
pub struct Rect<T> {
x: T, y: T,
w: T, h: T,
}
#[allow(dead_code)]
impl<T> Rect<T>
where T: Copy + Add<Output=T> + Sub<Output=T> + Mul<Output=T>
+ 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<T> {
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<T> {
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<T>) -> 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<T>) -> bool {
self.left() <= other.left() &&
self.right() >= other.right() &&
self.top() <= other.top() &&
self.bottom() >= other.bottom()
}
}

Loading…
Cancel
Save