diff --git a/src/vec.rs b/src/vec.rs index f08095c..0fecf45 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -2,8 +2,7 @@ use XY; use num::traits::Zero; -use std::cmp::{Ordering, max, min}; - +use std::cmp::{max, min, Ordering}; use std::ops::{Add, Div, Mul, Sub}; /// Simple 2D size, in cells. @@ -15,9 +14,9 @@ use std::ops::{Add, Div, Mul, Sub}; /// [`XY`]: ../struct.XY.html pub type Vec2 = XY; -impl PartialOrd for XY { +impl PartialOrd for XY { /// `a < b` <=> `a.x < b.x && a.y < b.y` - fn partial_cmp(&self, other: &Vec2) -> Option { + fn partial_cmp(&self, other: &XY) -> Option { if self == other { Some(Ordering::Equal) } else if self.x < other.x && self.y < other.y { @@ -36,8 +35,10 @@ impl XY { /// Never panics. pub fn saturating_sub>(&self, other: O) -> Self { let other = other.into(); - Self::new(self.x.saturating_sub(other.x), - self.y.saturating_sub(other.y)) + Self::new( + self.x.saturating_sub(other.x), + self.y.saturating_sub(other.y), + ) } /// Checked subtraction. Computes `self - other` if possible. @@ -109,14 +110,28 @@ impl XY { impl + Clone> XY { /// Returns (max(self.x,other.x), self.y+other.y) pub fn stack_vertical(&self, other: &Self) -> Self { - Self::new(max(self.x.clone(), other.x.clone()), - self.y.clone() + other.y.clone()) + Self::new( + max(self.x.clone(), other.x.clone()), + self.y.clone() + other.y.clone(), + ) } /// Returns (self.x+other.x, max(self.y,other.y)) pub fn stack_horizontal(&self, other: &Self) -> Self { - Self::new(self.x.clone() + other.x.clone(), - max(self.y.clone(), other.y.clone())) + Self::new( + self.x.clone() + other.x.clone(), + max(self.y.clone(), other.y.clone()), + ) + } + + /// Returns `true` if `self` fits in the given rectangle. + pub fn fits_in_rect(&self, top_left: O1, size: O2) -> bool + where + O1: Into, + O2: Into, + { + let top_left = top_left.into(); + self.fits(top_left.clone()) && self < &(top_left + size) } } @@ -201,7 +216,7 @@ impl Mul for XY { } /// Four values representing each direction. -#[derive(Clone,Copy)] +#[derive(Clone, Copy)] pub struct Vec4 { /// Left margin pub left: usize, @@ -268,8 +283,9 @@ impl From<((i32, i32), (i32, i32))> for Vec4 { } } impl From<((usize, usize), (usize, usize))> for Vec4 { - fn from(((left, right), (top, bottom)): ((usize, usize), (usize, usize))) - -> Vec4 { + fn from( + ((left, right), (top, bottom)): ((usize, usize), (usize, usize)) + ) -> Vec4 { (left, right, top, bottom).into() } }