Rect

Struct Rect 

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

Source

pub fn new(origin: impl Into<Point2D<T>>, size: impl Into<Size<T>>) -> Self

Create a new rectangle.

Source

pub fn points(min: impl Into<Point2D<T>>, max: impl Into<Point2D<T>>) -> Self
where T: Sub<Output = T>,

Create a rectangle from a bottom-left and top-right point.

Source

pub fn map<S, F>(self, f: F) -> Rect<S>
where F: Fn(T) -> S,

Map the origin and size of a rectangle.

Source

pub fn expand(&self, x: T, y: T) -> Self
where T: Add<Output = T> + Sub<Output = T>,

Expand a rectangle’s area.

Source§

impl<T: Copy> Rect<T>

Source

pub fn origin(size: impl Into<Size<T>>) -> Self
where T: Zero,

Create a rectangle with a zero origin.

Source

pub fn scale(&self, s: T) -> Self
where T: Mul<Output = T> + Copy,

Scale a rectangle’s size.

Source

pub fn with_origin(&self, origin: impl Into<Point2D<T>>) -> Self
where T: Add<Output = T> + Sub<Output = T> + Copy,

Return the rectangle with a different origin.

§Examples
use ombre::math::Rect;

let r = Rect::new([1, 1], [4, 4]);
assert_eq!(r.with_origin([0, 0]), Rect::new([0, 0], [4, 4]));
Source

pub fn with_size(&self, size: impl Into<Size<T>>) -> Self
where T: Add<Output = T> + Sub<Output = T> + Copy,

Return the rectangle with a different size.

§Examples
use ombre::math::Rect;

let r = Rect::new([1, 1], [4, 4]);
assert_eq!(r.with_size([9, 9]), Rect::new([1, 1], [9, 9]));
Source

pub fn area(&self) -> T
where T: Copy + Sub<Output = T> + PartialOrd + Mul<Output = T>,

Return the area of a rectangle.

Source

pub fn is_empty(&self) -> bool
where T: Zero,

Check whether the rectangle has a zero size.

Source

pub fn is_zero(&self) -> bool
where T: Zero,

Check whether the rectangle has a zero size and its origin is zero.

Source

pub fn width(&self) -> T

Return the width of the rectangle.

§Examples
use ombre::math::Rect;

let r = Rect::new([0, 0], [3, 3]);
assert_eq!(r.width(), 3);
Source

pub fn height(&self) -> T

Return the height of the rectangle.

§Examples
use ombre::math::Rect;

let r: Rect<u64> = Rect::origin([6, 6]);
assert_eq!(r.height(), 6);
Source

pub fn center(&self) -> Point2D<T>
where T: Two + Div<Output = T> + Add<Output = T>,

Return the center of the rectangle.

§Examples
use ombre::math::Rect;
use ombre::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));
Source

pub fn radius(&self) -> T
where T: Two + Div<Output = T> + PartialOrd + Sub<Output = T>,

Return the radius of the rectangle.

Source

pub fn contains(&self, point: impl Into<Point2D<T>>) -> bool
where T: PartialOrd + Add<Output = T>,

Check whether the given point is contained in the rectangle.

use ombre::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]));
Source

pub fn min(&self) -> Point2D<T>

Get the minimum point.

Source

pub fn max(&self) -> Point2D<T>
where T: Add<Output = T>,

Get the maximum point.

Source

pub fn intersects(&self, other: Rect<T>) -> bool
where T: PartialOrd + Add<Output = T>,

Check whether this rectangle intersects with another.

Source

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 ombre::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> Add<Vector2D<T>> for Rect<T>
where T: Add<Output = T> + Copy,

Source§

type Output = Rect<T>

The resulting type after applying the + operator.
Source§

fn add(self, vec: Vector2D<T>) -> Self

Performs the + operation. Read more
Source§

impl<T> AddAssign<Vector2D<T>> for Rect<T>
where T: Add<Output = T> + Copy,

Source§

fn add_assign(&mut self, vec: Vector2D<T>)

Performs the += operation. Read more
Source§

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

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
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() -> Rect<T>

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

impl<T> Display for Rect<T>
where T: Display,

Source§

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

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

impl<T: Copy + Add<Output = T>> From<&Rect<T>> for Box2D<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T: Copy + Add<Output = T>> From<Rect<T>> for Box2D<T>

Source§

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

Converts to this type from the input type.
Source§

impl Geometry for Rect<f32>

Source§

fn transform(self, t: impl Into<Transform>) -> Self

Source§

fn untransform(self, t: impl Into<Transform>) -> Self

Source§

impl<T> Mul<T> for Rect<T>
where T: Mul<Output = T> + Copy,

Source§

type Output = Rect<T>

The resulting type after applying the * operator.
Source§

fn mul(self, s: T) -> Self

Performs the * operation. Read more
Source§

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

Source§

fn eq(&self, other: &Rect<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Sub<Vector2D<T>> for Rect<T>
where T: Sub<Output = T> + Copy,

Source§

type Output = Rect<T>

The resulting type after applying the - operator.
Source§

fn sub(self, vec: Vector2D<T>) -> Self

Performs the - operation. Read more
Source§

impl<T> SubAssign<Vector2D<T>> for Rect<T>
where T: Sub<Output = T> + Copy,

Source§

fn sub_assign(&mut self, vec: Vector2D<T>)

Performs the -= operation. Read more
Source§

impl<T: Zero> Zero for Rect<T>

Source§

const ZERO: Self

Source§

fn is_zero(&self) -> bool

Source§

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

Source§

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

Source§

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> 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> 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<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.