Struct agb_fixnum::Rect
source · 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>
impl<T: Number> Rect<T>
sourcepub fn new(position: Vector2D<T>, size: Vector2D<T>) -> Self
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));
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 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)));
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(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));
sourcepub fn overlapping_rect(&self, other: Rect<T>) -> Option<Self>
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>
impl<T: FixedWidthUnsignedInteger> Rect<T>
sourcepub fn iter(self) -> impl Iterator<Item = (T, T)>
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);