Skip to main content

Rect

Struct Rect 

Source
pub struct Rect<T> {
    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> Rect<T>

Source

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

Creates a rectangle from it’s position and size

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

pub fn from_corners(top_left: Vector2D<T>, bottom_right: Vector2D<T>) -> Rect<T>
where T: Sub<Output = T> + Copy,

Creates a rectangle from two corner points.

let r = Rect::from_corners(vec2(1, 2), vec2(4, 6));
assert_eq!(r, Rect::new(vec2(1, 2), vec2(3, 4)));
Source§

impl<T> Rect<T>

Source

pub fn scale<Rhs, O>(self, rhs: Rhs) -> Rect<O>
where T: Mul<Rhs, Output = O>, Rhs: Copy,

Scales up the rectangle by the given amount about the origin.

let r = Rect::new(vec2(5, 10), vec2(15, 20));
assert_eq!(r.scale(2), Rect::new(vec2(10, 20), vec2(30, 40)));
Source

pub fn scale_down<Rhs, O>(self, rhs: Rhs) -> Rect<O>
where T: Div<Rhs, Output = O>, Rhs: Copy,

Scales down the rectangle by the given amount about the origin.

let r = Rect::new(vec2(10, 20), vec2(30, 40));
assert_eq!(r.scale_down(2), Rect::new(vec2(5, 10), vec2(15, 20)));
Source

pub fn translate(self, amount: Vector2D<T>) -> Rect<T>
where T: Add<Output = T> + Copy,

Translate the rectangle by the given amount

let r = Rect::new(vec2(10, 20), vec2(30, 40));
assert_eq!(r.translate(vec2(3, 5)), Rect::new(vec2(13, 25), vec2(30, 40)));
Source§

impl<T> Rect<T>
where T: Add<Output = T> + Ord + Copy,

Source

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

Returns true if the rectangle contains the point given, note that the boundary counts part of the rectangle.

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

assert!(!r.contains_point(vec2(0, 2)));
assert!(!r.contains_point(vec2(5, 2)));
assert!(!r.contains_point(vec2(2, 0)));
assert!(!r.contains_point(vec2(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(vec2(1, 1), vec2(3, 3));

assert!(r.touches(r));

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

let r2 = Rect::new(vec2(-10, -10), vec2(3, 3));
assert!(!r.touches(r2));
Source§

impl<T> Rect<T>
where T: Ord + Copy + AddAssign + Sub<Output = T> + Add<Output = T>,

Source

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

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(vec2(1, 1), vec2(3, 3));
let r2 = Rect::new(vec2(2, 2), vec2(3, 3));

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

assert_eq!(r.overlapping_rect(r2), None);
Source

pub fn clamp_point(self, point: impl Into<Vector2D<T>>) -> Vector2D<T>

Clamps the given point to be within the rectangle.

let bounding_rect = Rect::new(vec2(10, 10), vec2(10, 10));

assert_eq!(bounding_rect.clamp_point(vec2(15, 15)), vec2(15, 15));
assert_eq!(bounding_rect.clamp_point(vec2(0, 15)), vec2(10, 15));
assert_eq!(bounding_rect.clamp_point(vec2(100, 30)), vec2(20, 20));
Source§

impl<T> Rect<T>

Source

pub fn width(self) -> T

Returns the width of the rectangle.

let r = Rect::new(vec2(5, 10), vec2(20, 30));
assert_eq!(r.width(), 20);
Source

pub fn height(self) -> T

Returns the height of the rectangle.

let r = Rect::new(vec2(5, 10), vec2(20, 30));
assert_eq!(r.height(), 30);
Source

pub fn top_left(self) -> Vector2D<T>

Returns the top left point of the rectangle.

Is the same as .position.

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.top_left(), vec2(10, 10));
assert_eq!(r.top_left(), r.position);
Source

pub fn left(self) -> T

Returns the x coordinate of the left hand side of the rectangle

let r = Rect::new(vec2(10, 20), vec2(20, 25));
assert_eq!(r.left(), 10);
Source

pub fn top(self) -> T

Returns the y coordinate of the top of the rectangle

let r = Rect::new(vec2(10, 20), vec2(20, 25));
assert_eq!(r.top(), 20);
Source§

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

Source

pub fn right(self) -> T

Returns the x coordinate of the right hand side of the rectangle

let r = Rect::new(vec2(10, 20), vec2(20, 25));
assert_eq!(r.right(), 30);
Source

pub fn bottom(self) -> T

Returns the y coordinate of the top of the rectangle

let r = Rect::new(vec2(10, 20), vec2(20, 25));
assert_eq!(r.bottom(), 45);
Source

pub fn top_right(self) -> Vector2D<T>

Returns the top right point of the rectangle.

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.top_right(), vec2(20, 10));
Source

pub fn bottom_left(self) -> Vector2D<T>

Returns the bottom left point of the rectangle.

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.bottom_left(), vec2(10, 20));
Source

pub fn bottom_right(self) -> Vector2D<T>

Returns the bottom right point of the rectangle.

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.bottom_right(), vec2(20, 20));
Source§

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

Source

pub fn centre(self) -> Vector2D<T>

Returns the centre point of the rectangle

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.centre(), vec2(15, 15));
Source

pub fn left_centre(self) -> Vector2D<T>

Returns the centre point of the left edge of the rectangle

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.left_centre(), vec2(10, 15));
Source

pub fn right_centre(self) -> Vector2D<T>

Returns the centre point of the right edge of the rectangle

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.right_centre(), vec2(20, 15));
Source

pub fn top_centre(self) -> Vector2D<T>

Returns the centre point of the top edge of the rectangle

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.top_centre(), vec2(15, 10));
Source

pub fn bottom_centre(self) -> Vector2D<T>

Returns the centre point of the bottom edge of the rectangle

let r = Rect::new(vec2(10, 10), vec2(10, 10));
assert_eq!(r.bottom_centre(), vec2(15, 20));
Source§

impl<T> Rect<T>

Source

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

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

use agb_fixnum::{Rect, vec2};
let r = Rect::new(vec2(1, 1), vec2(1, 2));

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

assert_eq!(rect_points, expected_points);
Source§

impl<T> Rect<T>
where T: Ord + Zero + Signed + Copy,

Source

pub fn abs(self) -> Rect<T>

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

use agb_fixnum::{Rect, vec2};

let r: Rect<i32> = Rect::new(vec2(5, 5), vec2(-3, -2));

let normalized_rect = Rect::new(vec2(2, 3), vec2(3, 2));

// even though they represent the same area, they are not consider equivalent
assert_ne!(r, normalized_rect);
// unless you normalize the one with negative area
assert_eq!(r.abs(), normalized_rect);

Trait Implementations§

Source§

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

Source§

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

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

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

Performs copy-assignment from source. Read more
Source§

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

Source§

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

Source§

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

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

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

Source§

fn default() -> Rect<T>

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

impl<'de, T> Deserialize<'de> for Rect<T>
where T: Deserialize<'de>,

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Rect<T>, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,