pub trait Rectangle{
type Unit: Num + One + Copy + PartialEq + PartialOrd + Ord;
Show 15 methods
// Required methods
fn left(&self) -> Self::Unit;
fn right(&self) -> Self::Unit;
fn top(&self) -> Self::Unit;
fn bottom(&self) -> Self::Unit;
fn new_from_sides(
left: Self::Unit,
right: Self::Unit,
top: Self::Unit,
bottom: Self::Unit,
) -> Self;
// Provided methods
fn width(&self) -> Self::Unit { ... }
fn height(&self) -> Self::Unit { ... }
fn translate(&self, x: Self::Unit, y: Self::Unit) -> Self { ... }
fn perimeter(&self) -> Self::Unit { ... }
fn area(&self) -> Self::Unit { ... }
fn contains_point(&self, x: Self::Unit, y: Self::Unit) -> bool { ... }
fn contains_rectangle(
&self,
other: &impl Rectangle<Unit = Self::Unit>,
) -> bool { ... }
fn overlaps(&self, other: &impl Rectangle<Unit = Self::Unit>) -> bool { ... }
fn intersection(
&self,
other: &impl Rectangle<Unit = Self::Unit>,
) -> Option<Self> { ... }
fn unobstructed_subrectangles(
&self,
obstructions: &[&impl Rectangle<Unit = Self::Unit>],
) -> Vec<Self> { ... }
}
Expand description
A trait containing methods for rectangle like data structures which implement Sized
& Copy
.
This trait treats all edges (left, right, top, & bottom) as inclusive.
§Example
use rect_lib::Rectangle;
#[derive(Clone, Copy)]
pub struct BasicRectangle {
x: i32,
y: i32,
width: i32,
height: i32,
}
impl Rectangle for BasicRectangle {
type Unit = i32;
fn left(&self) -> i32 {
self.x
}
fn right(&self) -> i32 {
self.x + self.width - 1
}
fn top(&self) -> i32 {
self.y
}
fn bottom(&self) -> i32 {
self.y - self.height + 1
}
fn new_from_sides(left: i32, right: i32, top: i32, bottom: i32) -> Self {
Self {
x: left,
y: top,
width: right - left + 1,
height: top - bottom + 1,
}
}
}
Required Associated Types§
Required Methods§
Sourcefn left(&self) -> Self::Unit
fn left(&self) -> Self::Unit
The left most point of the rectangle.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 2, 3);
assert_eq!(rect.left(), 0);
Sourcefn right(&self) -> Self::Unit
fn right(&self) -> Self::Unit
The right most point of the rectangle.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 2, 3);
assert_eq!(rect.right(), 1);
Sourcefn top(&self) -> Self::Unit
fn top(&self) -> Self::Unit
The top most point of the rectangle.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 2, 3);
assert_eq!(rect.top(), 2);
Provided Methods§
Sourcefn width(&self) -> Self::Unit
fn width(&self) -> Self::Unit
The width of the rectangle.
This is calculated as right - left
.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 1, 0);
assert_eq!(rect.width(), 1);
Sourcefn height(&self) -> Self::Unit
fn height(&self) -> Self::Unit
The height of the rectangle.
This is calculated as top - bottom
.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 1, 0);
assert_eq!(rect.height(), 1);
Sourcefn translate(&self, x: Self::Unit, y: Self::Unit) -> Self
fn translate(&self, x: Self::Unit, y: Self::Unit) -> Self
Translates the rectangle by the given amount. This is done by adding the given amount to the x and y coordinates.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 1, 0);
let translated = rect.translate(1, 1);
assert_eq!(translated, BasicRectangle::new_from_sides(1, 2, 2, 1));
Sourcefn perimeter(&self) -> Self::Unit
fn perimeter(&self) -> Self::Unit
The perimeter of the rectangle.
This is calculated as (width + height) * 2
.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 1, 0);
assert_eq!(rect.perimeter(), 4);
Sourcefn area(&self) -> Self::Unit
fn area(&self) -> Self::Unit
The area of the rectangle.
This is calculated as width * height
.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 1, 0);
assert_eq!(rect.area(), 1);
Sourcefn contains_point(&self, x: Self::Unit, y: Self::Unit) -> bool
fn contains_point(&self, x: Self::Unit, y: Self::Unit) -> bool
Checks if the rectangle contains the given point.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 1, 1, 0);
assert!(rect.contains_point(0, 1));
assert!(!rect.contains_point(0, 2));
Sourcefn contains_rectangle(&self, other: &impl Rectangle<Unit = Self::Unit>) -> bool
fn contains_rectangle(&self, other: &impl Rectangle<Unit = Self::Unit>) -> bool
Checks if one rectangle contains another.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 2, 2, 0);
let other = BasicRectangle::new_from_sides(0, 1, 1, 0);
assert!(rect.contains_rectangle(&other));
assert!(!other.contains_rectangle(&rect));
Sourcefn overlaps(&self, other: &impl Rectangle<Unit = Self::Unit>) -> bool
fn overlaps(&self, other: &impl Rectangle<Unit = Self::Unit>) -> bool
Checks if one rectangle overlaps with another.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 2, 2, 0);
assert!(rect.overlaps(&BasicRectangle::new_from_sides(1, 3, 3, 1)));
assert!(!rect.overlaps(&BasicRectangle::new_from_sides(3, 4, 4, 3)));
Sourcefn intersection(
&self,
other: &impl Rectangle<Unit = Self::Unit>,
) -> Option<Self>
fn intersection( &self, other: &impl Rectangle<Unit = Self::Unit>, ) -> Option<Self>
Returns the intersection of two rectangles.
If the rectangles do not intersect, None
is returned.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 2, 2, 0);
let intersection = rect.intersection(&BasicRectangle::new_from_sides(1, 3, 3, 1));
assert_eq!(intersection, Some(BasicRectangle::new_from_sides(1, 2, 2, 1)));
let no_intersection = rect.intersection(&BasicRectangle::new_from_sides(3, 4, 4, 3));
assert_eq!(no_intersection, None);
Sourcefn unobstructed_subrectangles(
&self,
obstructions: &[&impl Rectangle<Unit = Self::Unit>],
) -> Vec<Self>
fn unobstructed_subrectangles( &self, obstructions: &[&impl Rectangle<Unit = Self::Unit>], ) -> Vec<Self>
This algorithm identifies all unique unobstructed sub-rectangles within a given rectangle by comparing it against a list of obstructions.
§Example
use rect_lib::{BasicRectangle, Rectangle};
let rect = BasicRectangle::new_from_sides(0, 5, 5, 0);
let obstruction = BasicRectangle::new_from_sides(0, 2, 5, 1);
let subrects = rect.unobstructed_subrectangles(&vec![&obstruction]);
assert_eq!(subrects.len(), 2);
assert!(subrects.iter().all(|r| [
BasicRectangle::new_from_sides(0, 5, 0, 0),
BasicRectangle::new_from_sides(3, 5, 5, 0)
].contains(r)));
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.