start implementing Rectangle

This commit is contained in:
expectocode 2019-12-31 00:21:43 +00:00
parent f071cafe51
commit 12a50af544
1 changed files with 61 additions and 0 deletions

View File

@ -76,6 +76,67 @@ impl PartialEq for Point {
impl Eq for Point {} impl Eq for Point {}
impl Rectangle {
fn from_corner_width_height(corner: Point, width: u32, height: u32) -> Rectangle {
Rectangle {
x: corner.x(),
y: corner.y(),
width,
height,
}
}
fn from_corners(corner_a: Point, corner_b: Point) -> Rectangle {
let left = corner_a.x().min(corner_b.x());
let top = corner_a.y().min(corner_b.y());
let width = (corner_a.x() as i64 - corner_b.x() as i64).abs() as u32;
let height = (corner_a.y() as i64 - corner_b.y() as i64).abs() as u32;
Rectangle {
x: left,
y: top,
width,
height,
}
}
fn at_origin_with_size(width: u32, height: u32) -> Rectangle {
Rectangle {
x: 0,
y: 0,
width,
height,
}
}
fn width(&self) -> u32 {
self.width
}
fn height(&self) -> u32 {
self.height
}
fn with_width(&self, width: u32) -> Rectangle {
Rectangle {
x: self.x,
y: self.y,
width,
height: self.height,
}
}
fn with_height(&self, height: u32) -> Rectangle {
Rectangle {
x: self.x,
y: self.y,
width: self.width,
height,
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;