Struct rgx::math::rect::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>>) -> Selfwhere 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) -> Selfwhere 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>>) -> Selfwhere T: Zero,

Create a rectangle with a zero origin.

source

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

Scale a rectangle’s size.

source

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]));
source

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]));
source

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

Return the area of a rectangle.

source

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

Check whether the rectangle has a zero size.

source

pub fn is_zero(&self) -> boolwhere 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 rgx::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 rgx::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 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));
source

pub fn radius(&self) -> Twhere 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>>) -> 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]));
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>) -> boolwhere 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 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> Add<Vector2D<T, ()>> for Rect<T>where T: Add<Output = T> + Copy,

§

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 copy 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 From<Rect<f32>> for Paint

source§

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

Converts to this type from the input type.
source§

impl From<Rect<f32>> for Rectangle

source§

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

Converts to this type from the input type.
source§

impl Geometry for Rect<f32>

source§

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

Return a transformed object.
source§

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

Undos a transform.
source§

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

§

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<Rect<T>> for Rect<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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,

§

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> StructuralEq for Rect<T>

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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> IntoPaint for Twhere T: Into<Paint>,

source§

fn into_paint(self, _canvas: &Canvas<'_>) -> Paint

Turn into paint.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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.