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§

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

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

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

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

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

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.