pub struct Rect<T> {
pub origin: Point2D<T>,
pub size: Size<T>,
}
Expand description
A generic rectangle with an origin and size.
Fields§
§origin: Point2D<T>
Rectangle origin.
size: Size<T>
Rectangle size.
Implementations§
source§impl<T: Copy> Rect<T>
impl<T: Copy> Rect<T>
sourcepub fn new(origin: impl Into<Point2D<T>>, size: impl Into<Size<T>>) -> Self
pub fn new(origin: impl Into<Point2D<T>>, size: impl Into<Size<T>>) -> Self
Create a new rectangle.
sourcepub fn points(min: impl Into<Point2D<T>>, max: impl Into<Point2D<T>>) -> Selfwhere
T: Sub<Output = T>,
pub fn points(min: impl Into<Point2D<T>>, max: impl Into<Point2D<T>>) -> Selfwhere T: Sub<Output = T>,
Create a rectangle from a bottom-left and top-right point.
source§impl<T: Copy> Rect<T>
impl<T: Copy> Rect<T>
sourcepub fn origin(size: impl Into<Size<T>>) -> Selfwhere
T: Zero,
pub fn origin(size: impl Into<Size<T>>) -> Selfwhere T: Zero,
Create a rectangle with a zero origin.
sourcepub fn with_origin(&self, origin: impl Into<Point2D<T>>) -> Selfwhere
T: Add<Output = T> + Sub<Output = T> + Copy,
pub fn with_origin(&self, origin: impl Into<Point2D<T>>) -> Selfwhere T: Add<Output = T> + Sub<Output = T> + Copy,
Return the rectangle with a different origin.
Examples
use rgx::math::Rect;
let r = Rect::new([1, 1], [4, 4]);
assert_eq!(r.with_origin([0, 0]), Rect::new([0, 0], [4, 4]));
sourcepub fn with_size(&self, size: impl Into<Size<T>>) -> Selfwhere
T: Add<Output = T> + Sub<Output = T> + Copy,
pub fn with_size(&self, size: impl Into<Size<T>>) -> Selfwhere T: Add<Output = T> + Sub<Output = T> + Copy,
Return the rectangle with a different size.
Examples
use rgx::math::Rect;
let r = Rect::new([1, 1], [4, 4]);
assert_eq!(r.with_size([9, 9]), Rect::new([1, 1], [9, 9]));
sourcepub fn area(&self) -> Twhere
T: Copy + Sub<Output = T> + PartialOrd + Mul<Output = T>,
pub fn area(&self) -> Twhere T: Copy + Sub<Output = T> + PartialOrd + Mul<Output = T>,
Return the area of a rectangle.
sourcepub fn is_zero(&self) -> boolwhere
T: Zero,
pub fn is_zero(&self) -> boolwhere T: Zero,
Check whether the rectangle has a zero size and its origin is zero.
sourcepub fn width(&self) -> T
pub fn width(&self) -> T
Return the width of the rectangle.
Examples
use rgx::math::Rect;
let r = Rect::new([0, 0], [3, 3]);
assert_eq!(r.width(), 3);
sourcepub fn height(&self) -> T
pub fn height(&self) -> T
Return the height of the rectangle.
Examples
use rgx::math::Rect;
let r: Rect<u64> = Rect::origin([6, 6]);
assert_eq!(r.height(), 6);
sourcepub fn center(&self) -> Point2D<T>where
T: Two + Div<Output = T> + Add<Output = T>,
pub fn center(&self) -> Point2D<T>where T: Two + Div<Output = T> + Add<Output = T>,
Return the center of the rectangle.
Examples
use rgx::math::Rect;
use rgx::math::Point2D;
let r = Rect::origin([8, 8]);
assert_eq!(r.center(), Point2D::new(4, 4));
let r = Rect::new([0, 0], [-8, -8]);
assert_eq!(r.center(), Point2D::new(-4, -4));
sourcepub fn radius(&self) -> Twhere
T: Two + Div<Output = T> + PartialOrd + Sub<Output = T>,
pub fn radius(&self) -> Twhere T: Two + Div<Output = T> + PartialOrd + Sub<Output = T>,
Return the radius of the rectangle.
sourcepub fn contains(&self, point: impl Into<Point2D<T>>) -> boolwhere
T: PartialOrd + Add<Output = T>,
pub fn contains(&self, point: impl Into<Point2D<T>>) -> boolwhere T: PartialOrd + Add<Output = T>,
Check whether the given point is contained in the rectangle.
use rgx::math::{Point2D, Rect};
let r = Rect::origin([6, 6]);
assert!(r.contains([0, 0]));
assert!(r.contains([3, 3]));
assert!(!r.contains([6, 6]));
let r = Rect::new([-6, -6], [6, 6]);
assert!(r.contains([-3, -3]));
sourcepub fn intersects(&self, other: Rect<T>) -> boolwhere
T: PartialOrd + Add<Output = T>,
pub fn intersects(&self, other: Rect<T>) -> boolwhere T: PartialOrd + Add<Output = T>,
Check whether this rectangle intersects with another.
sourcepub fn intersection(&self, other: Rect<T>) -> Option<Self>where
T: Ord + Add<Output = T> + Sub<Output = T>,
pub fn intersection(&self, other: Rect<T>) -> Option<Self>where T: Ord + Add<Output = T> + Sub<Output = T>,
Return the intersection between two rectangles.
Examples
use rgx::math::Rect;
let other = Rect::points([0, 0], [3, 3]);
let r = Rect::points([1, 1], [6, 6]);
assert_eq!(r.intersection(other), Some(Rect::points([1, 1], [3, 3])));
let r = Rect::points([1, 1], [2, 2]);
assert_eq!(r.intersection(other), Some(Rect::points([1, 1], [2, 2])));
let r = Rect::points([-1, -1], [3, 3]);
assert_eq!(r.intersection(other), Some(Rect::points([0, 0], [3, 3])));
let r = Rect::points([-1, -1], [4, 4]);
assert_eq!(r.intersection(other), Some(other));
let r = Rect::points([4, 4], [5, 5]);
assert_eq!(r.intersection(other), None);
assert_eq!(other.intersection(r), None);
Trait Implementations§
source§impl<T> AddAssign<Vector2D<T, ()>> for Rect<T>where
T: Add<Output = T> + Copy,
impl<T> AddAssign<Vector2D<T, ()>> for Rect<T>where T: Add<Output = T> + Copy,
source§fn add_assign(&mut self, vec: Vector2D<T>)
fn add_assign(&mut self, vec: Vector2D<T>)
Performs the
+=
operation. Read moresource§impl<T: PartialEq> PartialEq<Rect<T>> for Rect<T>
impl<T: PartialEq> PartialEq<Rect<T>> for Rect<T>
source§impl<T> SubAssign<Vector2D<T, ()>> for Rect<T>where
T: Sub<Output = T> + Copy,
impl<T> SubAssign<Vector2D<T, ()>> for Rect<T>where T: Sub<Output = T> + Copy,
source§fn sub_assign(&mut self, vec: Vector2D<T>)
fn sub_assign(&mut self, vec: Vector2D<T>)
Performs the
-=
operation. Read moreimpl<T: Copy> Copy for Rect<T>
impl<T: Eq> Eq for Rect<T>
impl<T> StructuralEq for Rect<T>
impl<T> StructuralPartialEq for Rect<T>
Auto Trait Implementations§
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
Mutably borrows from an owned value. Read more
source§impl<T> IntoPaint for Twhere
T: Into<Paint>,
impl<T> IntoPaint for Twhere T: Into<Paint>,
source§fn into_paint(self, _canvas: &Canvas<'_>) -> Paint
fn into_paint(self, _canvas: &Canvas<'_>) -> Paint
Turn into paint.