Struct agb_fixnum::Rect

source ·
pub struct Rect<T: Number> {
    pub position: Vector2D<T>,
    pub size: Vector2D<T>,
}
Expand description

A rectangle with a position in 2d space and a 2d size

Fields§

§position: Vector2D<T>

The position of the rectangle

§size: Vector2D<T>

The size of the rectangle

Implementations§

source§

impl<T: Number> Rect<T>

source

pub fn new(position: Vector2D<T>, size: Vector2D<T>) -> Self

Creates a rectangle from it’s position and size

let r = Rect::new(Vector2D::new(1,1), Vector2D::new(2,3));
assert_eq!(r.position, Vector2D::new(1,1));
assert_eq!(r.size, Vector2D::new(2,3));
source

pub fn contains_point(&self, point: Vector2D<T>) -> bool

Returns true if the rectangle contains the point given, note that the boundary counts as containing the rectangle.

let r = Rect::new(Vector2D::new(1,1), Vector2D::new(3,3));
assert!(r.contains_point(Vector2D::new(1,1)));
assert!(r.contains_point(Vector2D::new(2,2)));
assert!(r.contains_point(Vector2D::new(3,3)));
assert!(r.contains_point(Vector2D::new(4,4)));

assert!(!r.contains_point(Vector2D::new(0,2)));
assert!(!r.contains_point(Vector2D::new(5,2)));
assert!(!r.contains_point(Vector2D::new(2,0)));
assert!(!r.contains_point(Vector2D::new(2,5)));
source

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

Returns true if the other rectangle touches or overlaps the first.

let r = Rect::new(Vector2D::new(1,1), Vector2D::new(3,3));

assert!(r.touches(r.clone()));

let r1 = Rect::new(Vector2D::new(2,2), Vector2D::new(3,3));
assert!(r.touches(r1));

let r2 = Rect::new(Vector2D::new(-10,-10), Vector2D::new(3,3));
assert!(!r.touches(r2));
source

pub fn overlapping_rect(&self, other: Rect<T>) -> Option<Self>

Returns the rectangle that is the region that the two rectangles have in common, or None if they don’t overlap

let r = Rect::new(Vector2D::new(1,1), Vector2D::new(3,3));
let r2 = Rect::new(Vector2D::new(2,2), Vector2D::new(3,3));

assert_eq!(r.overlapping_rect(r2), Some(Rect::new(Vector2D::new(2,2), Vector2D::new(2,2))));
let r = Rect::new(Vector2D::new(1,1), Vector2D::new(3,3));
let r2 = Rect::new(Vector2D::new(-10,-10), Vector2D::new(3,3));

assert_eq!(r.overlapping_rect(r2), None);
source§

impl<T: FixedWidthUnsignedInteger> Rect<T>

source

pub fn iter(self) -> impl Iterator<Item = (T, T)>

Iterate over the points in a rectangle in row major order.

let r = Rect::new(Vector2D::new(1,1), Vector2D::new(2,3));

let expected_points = vec![(1,1), (2,1), (1,2), (2,2), (1,3), (2,3)];
let rect_points: Vec<(i32, i32)> = r.iter().collect();

assert_eq!(rect_points, expected_points);
source§

impl<T: FixedWidthSignedInteger> Rect<T>

source

pub fn abs(self) -> Self

Makes a rectangle that represents the equivalent location in space but with a positive size

Trait Implementations§

source§

impl<T: Clone + Number> 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 + Number> Debug for Rect<T>

source§

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

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

impl<T: Hash + Number> 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<T: PartialEq + Number> PartialEq 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: Copy + Number> Copy for Rect<T>

source§

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

source§

impl<T: Number> 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 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> 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, U> TryFrom<U> for T
where 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 T
where 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.