pub struct Rect<T> {
pub lower_left: Point<T>,
pub upper_right: Point<T>,
}
Expand description
A rectangle which is oriented along the x an y axis and represented by its lower left and upper right corner.
Fields§
§lower_left: Point<T>
Lower left corner of the rectangle.
upper_right: Point<T>
Upper right corner of the rectangle.
Implementations§
Source§impl<T> Rect<T>where
T: PartialOrd + Copy,
impl<T> Rect<T>where
T: PartialOrd + Copy,
Sourcepub fn new<C>(c1: C, c2: C) -> Rect<T>
pub fn new<C>(c1: C, c2: C) -> Rect<T>
Construct the bounding box of the two points. Order does not matter.
§Examples
use iron_shapes::prelude::*;
// Create a rectangle based on two corner points.
let rect1 = Rect::new(Point::new(0, 0), Point::new(1, 2));
// Any type that implements `Into<Point<T>>` can be used for the corner points.
let rect2 = Rect::new((1, 2), (0, 0));
// Ordering of the corner points does not matter.
assert_eq!(rect1, rect2);
// Even though `(0, 0)` was passed as second argument it is recognized as lower left corner.
assert_eq!(rect2.lower_left(), Point::new(0, 0));
Source§impl<T> Rect<T>where
T: Copy,
impl<T> Rect<T>where
T: Copy,
Sourcepub fn lower_left(&self) -> Point<T>
pub fn lower_left(&self) -> Point<T>
Get the lower left corner.
Sourcepub fn upper_left(&self) -> Point<T>
pub fn upper_left(&self) -> Point<T>
Get the upper left corner.
Sourcepub fn upper_right(&self) -> Point<T>
pub fn upper_right(&self) -> Point<T>
Get the upper right corner.
Sourcepub fn lower_right(&self) -> Point<T>
pub fn lower_right(&self) -> Point<T>
Get the lower right corner.
Source§impl<T> Rect<T>where
T: PartialOrd + Copy,
impl<T> Rect<T>where
T: PartialOrd + Copy,
Sourcepub fn contains_point(&self, p: Point<T>) -> bool
pub fn contains_point(&self, p: Point<T>) -> bool
Check if rectangle contains the point. Inclusive boundaries.
§Example
use iron_shapes::prelude::*;
let rect = Rect::new((0, 0), (10, 20));
// Contains point somewhere in the center.
assert!(rect.contains_point(Point::new(5, 5)));
// Also contains point on the boundaries.
assert!(rect.contains_point(Point::new(0, 0)));
// Does not contain point outside of the rectangle.
assert!(!rect.contains_point(Point::new(10, 21)));
Sourcepub fn contains_point_exclusive(&self, p: Point<T>) -> bool
pub fn contains_point_exclusive(&self, p: Point<T>) -> bool
Check if rectangle contains the point. Exclusive boundaries.
§Example
use iron_shapes::prelude::*;
let rect = Rect::new((0, 0), (10, 20));
// Contains point somewhere in the center.
assert!(rect.contains_point_exclusive(Point::new(5, 5)));
// Does not contain points on boundaries.
assert!(!rect.contains_point_exclusive(Point::new(0, 0)));
// Does not contain point outside of the rectangle.
assert!(!rect.contains_point_exclusive(Point::new(10, 21)));
Sourcepub fn contains_rectangle(&self, other: &Rect<T>) -> bool
pub fn contains_rectangle(&self, other: &Rect<T>) -> bool
Check if rectangle contains other rectangle. Inclusive boundaries.
§Example
use iron_shapes::prelude::*;
let outer = Rect::new((0, 0), (2, 2));
let inner = Rect::new((0, 0), (1, 1));
assert!(outer.contains_rectangle(&inner));
assert!(!inner.contains_rectangle(&outer));
Sourcepub fn contains_rectangle_exclusive(&self, other: &Rect<T>) -> bool
pub fn contains_rectangle_exclusive(&self, other: &Rect<T>) -> bool
Check if rectangle contains other rectangle. Exclusive boundaries.
§Example
use iron_shapes::prelude::*;
let outer = Rect::new((0, 0), (3, 3));
let inner = Rect::new((1, 1), (2, 2));
assert!(outer.contains_rectangle_exclusive(&inner));
assert!(!inner.contains_rectangle_exclusive(&outer));
let not_inner = Rect::new((0, 0), (1, 1)); // This shares the boundary with `outer`.
assert!(!outer.contains_rectangle_exclusive(¬_inner));
Sourcepub fn touches(&self, other: &Rect<T>) -> bool
pub fn touches(&self, other: &Rect<T>) -> bool
Test if the both rectangles touch each other, i.e. if they either share a boundary or are overlapping.
Sourcepub fn intersection(&self, other: &Rect<T>) -> Option<Rect<T>>
pub fn intersection(&self, other: &Rect<T>) -> Option<Rect<T>>
Compute the boolean intersection of two rectangles.
This function excludes the boundaries, hence a zero-area intersection is considered None
.
See intersection_inclusive_bounds()
zero-area intersections should be returned as Some(rectangle)
.
§Example
use iron_shapes::prelude::*;
// Create two overlapping rectangles.
let a = Rect::new((0, 0), (2, 2));
let b = Rect::new((1, 1), (3, 3));
// Compute the intersection.
assert_eq!(a.intersection(&b), Some(Rect::new((1, 1), (2, 2))));
// Create a non-overlapping rectangle.
let c = Rect::new((100, 100), (200, 200));
// The intersection with a non-overlapping rectangle is `None`.
assert_eq!(a.intersection(&c), None);
Sourcepub fn intersection_inclusive_bounds(&self, other: &Rect<T>) -> Option<Rect<T>>
pub fn intersection_inclusive_bounds(&self, other: &Rect<T>) -> Option<Rect<T>>
Compute the boolean intersection of two rectangles and include the boundaries. This allows to get zero-area intersection results for example if the two rectangles touch on a boundary or one of the rectangle is already zero-area.
§Example
use iron_shapes::prelude::*;
// Create two rectangles which intersect in a single point.
let a = Rect::new((0, 0), (2, 2));
let b = Rect::new((2, 2), (3, 3));
// Compute the intersection.
assert_eq!(a.intersection_inclusive_bounds(&b), Some(Rect::new((2, 2), (2, 2))));
Source§impl<T> Rect<T>
impl<T> Rect<T>
Sourcepub fn distance_to_point(&self, p: Point<T>) -> Vector<T>
pub fn distance_to_point(&self, p: Point<T>) -> Vector<T>
Compute the shortest from the rectangle to the point p
.
The distance is zero if the point is inside the rectangle.
§Example
use iron_shapes::prelude::*;
let r = Rect::new((0,0), (10, 10));
assert_eq!(r.distance_to_point((5, 15).into()), Vector::new(0, 5));
// Distance to point inside the rectangle is zero.
assert_eq!(r.distance_to_point((5, 5).into()), Vector::new(0, 0));
Source§impl<T> Rect<T>
impl<T> Rect<T>
Sourcepub fn sized(&self, add_x: T, add_y: T) -> Rect<T>
pub fn sized(&self, add_x: T, add_y: T) -> Rect<T>
Create an enlarged copy of this rectangle.
The vertical boundaries will be shifted towards the outside by add_x
.
The horizontal boundaries will be shifted towards the outside by add_y
.
Sourcepub fn sized_isotropic(&self, add: T) -> Rect<T>
pub fn sized_isotropic(&self, add: T) -> Rect<T>
Create an enlarged copy of this rectangle.
Trait Implementations§
Source§impl<T> BoundingBox<T> for Rect<T>where
T: Copy,
impl<T> BoundingBox<T> for Rect<T>where
T: Copy,
Source§fn bounding_box(&self) -> Rect<T>
fn bounding_box(&self) -> Rect<T>
Get bounding box of rectangle (which is equal to the rectangle itself).
Source§impl<'de, T> Deserialize<'de> for Rect<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Rect<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Rect<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Rect<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl<T> DoubledOrientedArea<T> for Rect<T>
impl<T> DoubledOrientedArea<T> for Rect<T>
Source§fn area_doubled_oriented(&self) -> T
fn area_doubled_oriented(&self) -> T
Calculate doubled oriented area of rectangle.
Source§impl<T> From<Rect<T>> for SimpleRPolygon<T>where
T: CoordinateType,
impl<T> From<Rect<T>> for SimpleRPolygon<T>where
T: CoordinateType,
Source§fn from(r: Rect<T>) -> SimpleRPolygon<T>
fn from(r: Rect<T>) -> SimpleRPolygon<T>
Source§impl<T> IntoEdges<T> for &Rect<T>where
T: CoordinateType,
impl<T> IntoEdges<T> for &Rect<T>where
T: CoordinateType,
Source§impl<'a, T> IntoIterator for &'a Rect<T>where
T: Copy,
Iterate over all points of the rectangle.
Starts with the lower left corner and iterates counter clock-wise.
impl<'a, T> IntoIterator for &'a Rect<T>where
T: Copy,
Iterate over all points of the rectangle. Starts with the lower left corner and iterates counter clock-wise.
Source§impl<T> IntoIterator for Rect<T>where
T: Copy,
Iterate over all points of the rectangle.
Starts with the lower left corner and iterates counter clock-wise.
impl<T> IntoIterator for Rect<T>where
T: Copy,
Iterate over all points of the rectangle. Starts with the lower left corner and iterates counter clock-wise.
Source§impl<C> IntoPoints<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<C> IntoPoints<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
Source§type PointIter = <Rect<<C as CoordinateBase>::Coord> as IntoIterator>::IntoIter
type PointIter = <Rect<<C as CoordinateBase>::Coord> as IntoIterator>::IntoIter
Source§fn into_points(
self,
) -> <Rect<<C as CoordinateBase>::Coord> as IntoPoints<C>>::PointIter
fn into_points( self, ) -> <Rect<<C as CoordinateBase>::Coord> as IntoPoints<C>>::PointIter
Source§impl<C> IntoSegments<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<C> IntoSegments<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
Source§type SegmentIter = RectEdgeIterator<<C as CoordinateBase>::Coord>
type SegmentIter = RectEdgeIterator<<C as CoordinateBase>::Coord>
Source§fn into_segments(
self,
) -> <Rect<<C as CoordinateBase>::Coord> as IntoSegments<C>>::SegmentIter
fn into_segments( self, ) -> <Rect<<C as CoordinateBase>::Coord> as IntoSegments<C>>::SegmentIter
Source§impl<T> MapPointwise<T> for Rect<T>where
T: Copy + PartialOrd,
Point wise transformation of the two corner points.
impl<T> MapPointwise<T> for Rect<T>where
T: Copy + PartialOrd,
Point wise transformation of the two corner points.
Source§impl<C> Polygon<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<C> Polygon<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
Source§fn set(
&mut self,
_iter: impl Iterator<Item = <Rect<<C as CoordinateBase>::Coord> as PolygonSet<C>>::Point>,
)
fn set( &mut self, _iter: impl Iterator<Item = <Rect<<C as CoordinateBase>::Coord> as PolygonSet<C>>::Point>, )
Source§impl<C> Polygon90<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<C> Polygon90<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
Source§type CompactIterator = IntoIter<<C as CoordinateBase>::Coord>
type CompactIterator = IntoIter<<C as CoordinateBase>::Coord>
Source§fn compact_iter(
&self,
) -> <Rect<<C as CoordinateBase>::Coord> as Polygon90<C>>::CompactIterator
fn compact_iter( &self, ) -> <Rect<<C as CoordinateBase>::Coord> as Polygon90<C>>::CompactIterator
Source§impl<C> PolygonSet<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<C> PolygonSet<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
Source§type AllPoints = <Rect<<C as CoordinateBase>::Coord> as IntoIterator>::IntoIter
type AllPoints = <Rect<<C as CoordinateBase>::Coord> as IntoIterator>::IntoIter
Source§fn num_polygons(&self) -> usize
fn num_polygons(&self) -> usize
Source§fn convolved(
self,
p: &<Rect<<C as CoordinateBase>::Coord> as PolygonSet<C>>::Point,
) -> Rect<<C as CoordinateBase>::Coord>
fn convolved( self, p: &<Rect<<C as CoordinateBase>::Coord> as PolygonSet<C>>::Point, ) -> Rect<<C as CoordinateBase>::Coord>
p
to all vertices.Source§fn convolve(
&mut self,
p: &<Rect<<C as CoordinateBase>::Coord> as PolygonSet<C>>::Point,
)
fn convolve( &mut self, p: &<Rect<<C as CoordinateBase>::Coord> as PolygonSet<C>>::Point, )
p
to all vertices.Source§fn scaled(
self,
scale: <C as CoordinateBase>::Coord,
) -> Rect<<C as CoordinateBase>::Coord>
fn scaled( self, scale: <C as CoordinateBase>::Coord, ) -> Rect<<C as CoordinateBase>::Coord>
Source§fn scale(&mut self, scale: <C as CoordinateBase>::Coord)
fn scale(&mut self, scale: <C as CoordinateBase>::Coord)
Source§fn all_points(
&self,
) -> <Rect<<C as CoordinateBase>::Coord> as PolygonSet<C>>::AllPoints
fn all_points( &self, ) -> <Rect<<C as CoordinateBase>::Coord> as PolygonSet<C>>::AllPoints
Source§impl<C> PolygonWithHoles<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<C> PolygonWithHoles<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
Source§impl<C> Rectangle<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<C> Rectangle<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
Source§type Interval = Interval<<C as CoordinateBase>::Coord>
type Interval = Interval<<C as CoordinateBase>::Coord>
Source§fn get(
&self,
orientation: Orientation2D,
) -> <Rect<<C as CoordinateBase>::Coord> as Rectangle<C>>::Interval
fn get( &self, orientation: Orientation2D, ) -> <Rect<<C as CoordinateBase>::Coord> as Rectangle<C>>::Interval
Source§impl<T> Serialize for Rect<T>where
T: Serialize,
impl<T> Serialize for Rect<T>where
T: Serialize,
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Source§impl<T> ToPolygon<T> for Rect<T>where
T: Copy,
impl<T> ToPolygon<T> for Rect<T>where
T: Copy,
Source§fn to_polygon(&self) -> Polygon<T>
fn to_polygon(&self) -> Polygon<T>
Source§impl<T> TryBoundingBox<T> for Rect<T>where
T: Copy,
impl<T> TryBoundingBox<T> for Rect<T>where
T: Copy,
Source§fn try_bounding_box(&self) -> Option<Rect<T>>
fn try_bounding_box(&self) -> Option<Rect<T>>
Get bounding box of rectangle (always exists).
Source§impl<T, Dst> TryCastCoord<T, Dst> for Rect<T>
impl<T, Dst> TryCastCoord<T, Dst> for Rect<T>
impl<T> Copy for Rect<T>where
T: Copy,
impl<T> Eq for Rect<T>where
T: Eq,
impl<C> Polygon90Set<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<C> Polygon90WithHoles<C> for Rect<<C as CoordinateBase>::Coord>where
C: CoordinateConcept,
impl<T> StructuralPartialEq for Rect<T>
Auto Trait Implementations§
impl<T> Freeze for Rect<T>where
T: Freeze,
impl<T> RefUnwindSafe for Rect<T>where
T: RefUnwindSafe,
impl<T> Send for Rect<T>where
T: Send,
impl<T> Sync for Rect<T>where
T: Sync,
impl<T> Unpin for Rect<T>where
T: Unpin,
impl<T> UnwindSafe for Rect<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more