pub struct Rect {
pub pos: Vector,
pub size: Vector,
}
Expand description
Represents a rectangle in a 2D space.
The Rect
struct is defined by its position (pos
) and size (size
), both of which are
represented as Vector
instances. The position indicates the coordinates of the rectangle’s
bottom-left corner, and the size indicates the width and height of the rectangle.
§Examples
Creating a new rectangle:
use fixed32::Fp;
use fixed32_math::{Vector, Rect};
let pos = Vector::new(Fp::from(1), Fp::from(2));
let size = Vector::new(Fp::from(3), Fp::from(4));
let rect = Rect::new(pos, size);
Accessing the position and size:
use fixed32::Fp;
use fixed32_math::{Vector, Rect};
let pos = Vector::new(Fp::from(1), Fp::from(2));
let size = Vector::new(Fp::from(3), Fp::from(4));
let rect = Rect::new(pos, size);
assert_eq!(rect.pos.x, Fp::from(1));
assert_eq!(rect.size.y, Fp::from(4));
Fields§
§pos: Vector
§size: Vector
Implementations§
Source§impl Rect
impl Rect
Sourcepub const fn new(pos: Vector, size: Vector) -> Self
pub const fn new(pos: Vector, size: Vector) -> Self
Creates a new Rect
with the specified position and size.
§Parameters
pos
: The position of the rectangle’s top-left corner as aVector
.size
: The size of the rectangle, including its width and height, as aVector
.
§Returns
A Rect
instance with the given position and size.
§Examples
use fixed32::Fp;
use fixed32_math::{Vector, Rect};
let pos = Vector::new(Fp::from(1), Fp::from(2));
let size = Vector::new(Fp::from(3), Fp::from(4));
let rect = Rect::new(pos, size);
assert_eq!(rect.pos, pos);
assert_eq!(rect.size, size);
pub fn top(self) -> Fp
pub const fn bottom(self) -> Fp
pub const fn left(self) -> Fp
pub fn right(self) -> Fp
Sourcepub fn move_by(self, offset: Vector) -> Self
pub fn move_by(self, offset: Vector) -> Self
Returns a new Rect
with its position translated by the given vector.
This method is useful for moving the rectangle while keeping its size unchanged.
§Parameters
offset
: A vector indicating how much to translate the rectangle’s position.
§Returns
A new Rect
with the position translated by the given vector. The size remains unchanged.
§Examples
use fixed32::Fp;
use fixed32_math::{Rect, Vector};
let pos = Vector::new(Fp::from(1), Fp::from(2));
let size = Vector::new(Fp::from(3), Fp::from(4));
let rect = Rect::new(pos, size);
let offset = Vector::new(Fp::from(1), Fp::from(-1));
let translated_rect = rect.move_by(offset);
assert_eq!(translated_rect.pos.x, Fp::from(2));
assert_eq!(translated_rect.pos.y, Fp::from(1));
assert_eq!(translated_rect.size, size);
Sourcepub fn intersection(&self, other: &Self) -> Option<Self>
pub fn intersection(&self, other: &Self) -> Option<Self>
Calculates the intersection of two rectangles.
Sourcepub fn contains_point(&self, point: &Vector) -> bool
pub fn contains_point(&self, point: &Vector) -> bool
Checks if a point is inside the rectangle.
Sourcepub fn contains_rect(&self, other: &Self) -> bool
pub fn contains_rect(&self, other: &Self) -> bool
Checks if another rectangle is completely inside this rectangle.
pub fn is_overlapping(self, other: Self) -> bool
Sourcepub fn contracted(&self, offset: Vector) -> Self
pub fn contracted(&self, offset: Vector) -> Self
Contracts the rectangle by a given offset.
Sourcepub fn aspect_ratio(&self) -> Fp
pub fn aspect_ratio(&self) -> Fp
Calculates the aspect ratio of the rectangle.