Add Rect::ZERO and make stuff const
Also tweaks the comments because they're not worth a separate commit.
This commit is contained in:
parent
b31c2de425
commit
d6b9aea273
1 changed files with 20 additions and 17 deletions
|
|
@ -11,9 +11,12 @@ pub struct Rect {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rect {
|
impl Rect {
|
||||||
/// Whenever a `Rect` is constructed, this function must be used. This
|
/// A zero-sized rectangle located at the origin.
|
||||||
|
pub const ZERO: Self = Self::new(0, 0, -1, -1);
|
||||||
|
|
||||||
|
/// Whenever a rectangle is constructed, this function must be used. This
|
||||||
/// ensures invariants are always checked.
|
/// ensures invariants are always checked.
|
||||||
fn new(north: i32, south: i32, west: i32, east: i32) -> Self {
|
const fn new(north: i32, south: i32, west: i32, east: i32) -> Self {
|
||||||
let result = Self {
|
let result = Self {
|
||||||
north,
|
north,
|
||||||
south,
|
south,
|
||||||
|
|
@ -28,65 +31,65 @@ impl Rect {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `Rect` that is a bounding box around two points.
|
/// Construct a rectangle that is a bounding box around two points.
|
||||||
///
|
///
|
||||||
/// It is not possible to construct a `Rect` with a width or height of 0
|
/// It is not possible to construct a rectangle with a width or height of 0
|
||||||
/// through this method. Use one of the other constructor functions instead.
|
/// through this method. Use one of the other constructor functions instead.
|
||||||
pub fn from_points(a: Vec2, b: Vec2) -> Self {
|
pub fn from_points(a: Vec2, b: Vec2) -> Self {
|
||||||
Self::new(a.y.min(b.y), a.y.max(b.y), a.x.min(b.x), a.x.max(b.x))
|
Self::new(a.y.min(b.y), a.y.max(b.y), a.x.min(b.x), a.x.max(b.x))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `Rect` from its north-west and south-east corners.
|
/// Construct a rectangle from its north-west and south-east corners.
|
||||||
pub fn from_nw_se(nw: Vec2, se: Vec2) -> Self {
|
pub const fn from_nw_se(nw: Vec2, se: Vec2) -> Self {
|
||||||
Self::new(nw.y, se.y, nw.x, se.x)
|
Self::new(nw.y, se.y, nw.x, se.x)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `Rect` from its north-east and south-west corners.
|
/// Construct a rectangle from its north-east and south-west corners.
|
||||||
pub fn from_ne_sw(ne: Vec2, sw: Vec2) -> Self {
|
pub const fn from_ne_sw(ne: Vec2, sw: Vec2) -> Self {
|
||||||
Self::new(ne.y, sw.y, sw.x, ne.x)
|
Self::new(ne.y, sw.y, sw.x, ne.x)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `Rect` from its north-west corner and size.
|
/// Construct a rectangle from its north-west corner and size.
|
||||||
pub fn from_nw(nw: Vec2, size: Vec2) -> Self {
|
pub fn from_nw(nw: Vec2, size: Vec2) -> Self {
|
||||||
let se = nw + (size - 1);
|
let se = nw + (size - 1);
|
||||||
Self::from_nw_se(nw, se)
|
Self::from_nw_se(nw, se)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `Rect` from its north-east corner and size.
|
/// Construct a rectangle from its north-east corner and size.
|
||||||
pub fn from_corner_ne(ne: Vec2, size: Vec2) -> Self {
|
pub fn from_corner_ne(ne: Vec2, size: Vec2) -> Self {
|
||||||
let sw = ne + (size - 1).neg_x();
|
let sw = ne + (size - 1).neg_x();
|
||||||
Self::from_ne_sw(ne, sw)
|
Self::from_ne_sw(ne, sw)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `Rect` from its south-west corner and size.
|
/// Construct a rectangle from its south-west corner and size.
|
||||||
pub fn from_corner_sw(sw: Vec2, size: Vec2) -> Self {
|
pub fn from_corner_sw(sw: Vec2, size: Vec2) -> Self {
|
||||||
let ne = sw + (size - 1).neg_y();
|
let ne = sw + (size - 1).neg_y();
|
||||||
Self::from_ne_sw(ne, sw)
|
Self::from_ne_sw(ne, sw)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `Rect` from its south-east corner and size.
|
/// Construct a rectangle from its south-east corner and size.
|
||||||
pub fn from_corner_se(se: Vec2, size: Vec2) -> Self {
|
pub fn from_corner_se(se: Vec2, size: Vec2) -> Self {
|
||||||
let nw = se - (size - 1);
|
let nw = se - (size - 1);
|
||||||
Self::from_nw_se(nw, se)
|
Self::from_nw_se(nw, se)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn corner_nw(&self) -> Vec2 {
|
pub const fn corner_nw(&self) -> Vec2 {
|
||||||
Vec2::new(self.west, self.north)
|
Vec2::new(self.west, self.north)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn corner_ne(&self) -> Vec2 {
|
pub const fn corner_ne(&self) -> Vec2 {
|
||||||
Vec2::new(self.east, self.north)
|
Vec2::new(self.east, self.north)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn corner_sw(&self) -> Vec2 {
|
pub const fn corner_sw(&self) -> Vec2 {
|
||||||
Vec2::new(self.west, self.south)
|
Vec2::new(self.west, self.south)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn corner_se(&self) -> Vec2 {
|
pub const fn corner_se(&self) -> Vec2 {
|
||||||
Vec2::new(self.east, self.south)
|
Vec2::new(self.east, self.south)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn size(&self) -> Vec2 {
|
pub const fn size(&self) -> Vec2 {
|
||||||
Vec2::new(self.east - self.west + 1, self.south - self.north + 1)
|
Vec2::new(self.east - self.west + 1, self.south - self.north + 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue