Struct fixed32_math::Rect
source · 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 fn new(pos: Vector, size: Vector) -> Self
pub 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);
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.
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.
Trait Implementations§
impl Copy for Rect
impl StructuralPartialEq for Rect
Auto Trait Implementations§
impl Freeze for Rect
impl RefUnwindSafe for Rect
impl Send for Rect
impl Sync for Rect
impl Unpin for Rect
impl UnwindSafe for Rect
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)