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>
impl<T> Rect<T>
Sourcepub const fn new(position: Vector2D<T>, size: Vector2D<T>) -> Rect<T>
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));Sourcepub fn from_corners(top_left: Vector2D<T>, bottom_right: Vector2D<T>) -> Rect<T>where
T: Sub<Output = T> + Copy,
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>
impl<T> Rect<T>
Sourcepub fn scale<Rhs, O>(self, rhs: Rhs) -> Rect<O>where
T: Mul<Rhs, Output = O>,
Rhs: Copy,
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)));Sourcepub fn scale_down<Rhs, O>(self, rhs: Rhs) -> Rect<O>where
T: Div<Rhs, Output = O>,
Rhs: Copy,
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§impl<T> Rect<T>where
T: Add<Output = T> + Ord + Copy,
impl<T> Rect<T>where
T: Add<Output = T> + Ord + Copy,
Sourcepub fn contains_point(&self, point: Vector2D<T>) -> bool
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)));Sourcepub fn touches(&self, other: Rect<T>) -> bool
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>,
impl<T> Rect<T>where
T: Ord + Copy + AddAssign + Sub<Output = T> + Add<Output = T>,
Sourcepub fn overlapping_rect(&self, other: Rect<T>) -> Option<Rect<T>>
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);Sourcepub fn clamp_point(self, point: impl Into<Vector2D<T>>) -> Vector2D<T>
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>
impl<T> Rect<T>
Sourcepub fn width(self) -> T
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);Sourcepub fn height(self) -> T
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);Sourcepub fn top_left(self) -> Vector2D<T>
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§impl<T> Rect<T>where
T: AddAssign + Add<Output = T>,
impl<T> Rect<T>where
T: AddAssign + Add<Output = T>,
Sourcepub fn right(self) -> T
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);Sourcepub fn bottom(self) -> T
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);Sourcepub fn top_right(self) -> Vector2D<T>
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));Sourcepub fn bottom_left(self) -> Vector2D<T>
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));Sourcepub fn bottom_right(self) -> Vector2D<T>
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,
impl<T> Rect<T>where
T: Div<Output = T> + Add<Output = T> + One + Copy,
Sourcepub fn centre(self) -> Vector2D<T>
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));Sourcepub fn left_centre(self) -> Vector2D<T>
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));Sourcepub fn right_centre(self) -> Vector2D<T>
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));Sourcepub fn top_centre(self) -> Vector2D<T>
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));Sourcepub fn bottom_centre(self) -> Vector2D<T>
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>where
T: FixedWidthUnsignedInteger,
impl<T> Rect<T>where
T: FixedWidthUnsignedInteger,
Sourcepub fn iter(self) -> impl Iterator<Item = Vector2D<T>>
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>
impl<T> Rect<T>
Sourcepub fn abs(self) -> Rect<T>
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);