Skip to main content

Rect

Struct Rect 

Source
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>§upper_right: Point<T>

Implementations§

Source§

impl<T: PartialOrd + Copy> Rect<T>

Source

pub fn new(c1: impl Into<Point<T>>, c2: impl Into<Point<T>>) -> Self

Construct the bounding box of the two points. Order does not matter.

§Examples
use reda_geometry::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: Copy> Rect<T>

Source

pub fn lower_left(&self) -> Point<T>

Get the lower left corner.

Source

pub fn upper_left(&self) -> Point<T>

Get the upper left corner.

Source

pub fn upper_right(&self) -> Point<T>

Get the upper right corner.

Source

pub fn lower_right(&self) -> Point<T>

Get the lower right corner.

Source§

impl<T: PartialOrd + Copy> Rect<T>

Source

pub fn contains_point(&self, p: Point<T>) -> bool

Check if rectangle contains the point. Inclusive boundaries.

§Example
use reda_geometry::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)));
Source

pub fn contains_point_exclusive(&self, p: Point<T>) -> bool

Check if rectangle contains the point. Exclusive boundaries.

§Example
use reda_geometry::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)));
Source

pub fn contains_rectangle(&self, other: &Self) -> bool

Check if rectangle contains other rectangle. Inclusive boundaries.

§Example
use reda_geometry::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));
Source

pub fn contains_rectangle_exclusive(&self, other: &Self) -> bool

Check if rectangle contains other rectangle. Exclusive boundaries.

§Example
use reda_geometry::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(&not_inner));
Source

pub fn touches(&self, other: &Self) -> bool

Test if the both rectangles touch each other, i.e. if they either share a boundary or are overlapping.

Source

pub fn intersection(&self, other: &Self) -> Option<Self>

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 reda_geometry::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);
Source

pub fn intersection_inclusive_bounds(&self, other: &Self) -> Option<Self>

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 reda_geometry::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

pub fn add_point(&self, point: Point<T>) -> Self

Create the smallest Rect that contains the original Rect and the point.

§Example
use reda_geometry::prelude::*;

let r1 = Rect::new((0,0), (1,2));

let r2 = r1.add_point(Point::new(10, 11));

assert_eq!(r2, Rect::new((0,0), (10,11)));
Source

pub fn add_rect(&self, rect: &Self) -> Self

Get the smallest Rect that contains both rectangles self and rect.

§Example
use reda_geometry::prelude::*;

let r1 = Rect::new((0,0), (1,2));
let r2 = Rect::new((4,5), (6,7));

let r3 = r1.add_rect(&r2);

assert_eq!(r3, Rect::new((0,0), (6,7)));
Source§

impl<T: Sub<Output = T> + Copy + Ord + Zero> Rect<T>

Source

pub fn distance_to_point(&self, p: Point<T>) -> Vec2<T>

Compute the shortest from the rectangle to the point p. The distance is zero if the point is inside the rectangle.

§Example
use reda_geometry::prelude::*;

let r = Rect::new((0,0), (10, 10));

assert_eq!(r.distance_to_point((5, 15).into()), Vec2::new(0, 5));

// Distance to point inside the rectangle is zero.
assert_eq!(r.distance_to_point((5, 5).into()), Vec2::new(0, 0));
Source§

impl<T: Copy + Sub<Output = T>> Rect<T>

Source

pub fn width(&self) -> T

Compute the width of the rectangle.

Source

pub fn height(&self) -> T

Compute the height of the rectangle.

Source§

impl<T: Copy + Add<Output = T> + Div<Output = T> + One> Rect<T>

Source

pub fn center(&self) -> Point<T>

Get the center point of the rectangle. When using integer coordinates the resulting coordinates will be truncated to the next integers.

Source§

impl<T: Copy + Add<Output = T> + Sub<Output = T> + PartialOrd> Rect<T>

Source

pub fn sized(&self, add_x: T, add_y: T) -> Self

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.

Source

pub fn sized_isotropic(&self, add: T) -> Self

Create an enlarged copy of this rectangle.

Trait Implementations§

Source§

impl<T: Copy> BoundingBox<T> for Rect<T>

Source§

fn bounding_box(&self) -> Rect<T>

Source§

impl<Src: Copy + NumCast, Dst: Copy + NumCast + PartialOrd> Cast<Src, Dst> for Rect<Src>

Source§

type Output = Rect<Dst>

Source§

fn try_cast(&self) -> Option<Self::Output>

Source§

fn cast(&self) -> Self::Output

Source§

impl<T: Clone> Clone for Rect<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Copy> Copy for Rect<T>

Source§

impl<T: Debug> Debug for Rect<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for Rect<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Eq> Eq for Rect<T>

Source§

impl<T> From<&Rect<T>> for Polygon<T>
where T: Copy,

Source§

fn from(rect: &Rect<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Rect<T>> for Polygon<T>
where T: Copy,

Source§

fn from(rect: Rect<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Rect<T>> for Geometry<T>

Source§

fn from(x: Rect<T>) -> Geometry<T>

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for Rect<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
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.

Source§

type Item = Point<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<&'a Rect<T> as IntoIterator>::Item, 4>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
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.

Source§

type Item = Point<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<Rect<T> as IntoIterator>::Item, 4>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Copy> MapPoint<T> for Rect<T>

Source§

fn transform<F>(&self, transformate: F) -> Self
where F: Fn(Point<T>) -> Point<T>,

Source§

impl<T: PartialEq> PartialEq for Rect<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialEq> StructuralPartialEq for Rect<T>

Source§

impl<T: Copy> ToPolygon<T> for Rect<T>

Source§

fn to_polygon(&self) -> Polygon<T>

Source§

impl<T: Copy> TryBoundingBox<T> for Rect<T>

Auto Trait Implementations§

§

impl<T> Freeze for Rect<T>
where Point<T>: Freeze,

§

impl<T> RefUnwindSafe for Rect<T>
where Point<T>: RefUnwindSafe,

§

impl<T> Send for Rect<T>
where Point<T>: Send,

§

impl<T> Sync for Rect<T>
where Point<T>: Sync,

§

impl<T> Unpin for Rect<T>
where Point<T>: Unpin,

§

impl<T> UnsafeUnpin for Rect<T>
where Point<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for Rect<T>
where Point<T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<S, T> Mirror<T> for S
where T: Copy + Zero + Sub<Output = T>, S: MapPoint<T>,

Source§

fn mirror_x(&self) -> S

Source§

fn mirror_y(&self) -> S

Source§

impl<S, T> Rotate<T> for S
where T: Copy + Zero + Sub<Output = T>, S: MapPoint<T>,

Source§

fn rotate(&self, a: Angle) -> S

Source§

impl<S, T> Scale<T> for S
where T: Copy + Mul<Output = T>, S: MapPoint<T>,

Source§

fn scale(&self, factor: T) -> S

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<S, T> Translate<T> for S
where T: Copy + Add<Output = T>, S: MapPoint<T>,

Source§

fn translate(&self, v: Vec2<T>) -> S

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.