pub struct Rect {
pub position: Vec2,
pub size: UVec2,
}
Expand description
A 2D rectangle with integer coordinates for position and unsigned integer dimensions.
This struct represents a rectangle in a 2D space where the position is specified
using Vec2
for integer coordinates, and the size (width and height) is represented
using UVec2
for unsigned integer dimensions.
§Fields
position
: The bottom-left corner of the rectangle as aVec2
.size
: The width and height of the rectangle as aUVec2
.
§Examples
use int_math::prelude::*;
// Create a rectangle with bottom-left corner at (10, 20) and size (30, 40)
let rect = Rect {
position: Vec2::new(10, 20),
size: UVec2::new(30, 40),
};
assert_eq!(rect.position, Vec2::new(10, 20));
assert_eq!(rect.size, UVec2::new(30, 40));
Fields§
§position: Vec2
§size: UVec2
Implementations§
Source§impl Rect
impl Rect
Sourcepub const fn new(x: i16, y: i16, width: u16, height: u16) -> Rect
pub const fn new(x: i16, y: i16, width: u16, height: u16) -> Rect
Creates a new Rect
instance with the specified position and size.
§Arguments
x
: The x-coordinate of the bottom-left corner of the rectangle.y
: The y-coordinate of the bottom-left corner of the rectangle.width
: The width of the rectangle.height
: The height of the rectangle.
§Examples
use int_math::prelude::*;
let rect = Rect::new(10, 20, 30, 40);
assert_eq!(rect.position, Vec2::new(10, 20));
assert_eq!(rect.size, UVec2::new(30, 40));
Sourcepub const fn with_position_and_size(position: Vec2, size: UVec2) -> Rect
pub const fn with_position_and_size(position: Vec2, size: UVec2) -> Rect
Creates a new Rect
instance with the specified position and size.
§Arguments
position
: The bottom-left corner of the rectangle as aVec2
.size
: The width and height of the rectangle as aUVec2
.
§Examples
use int_math::prelude::*;
let position = Vec2::new(10, 20);
let size = UVec2::new(30, 40);
let rect = Rect::with_position_and_size(position, size);
assert_eq!(rect.position, position);
assert_eq!(rect.size, size);
Sourcepub const fn center(self) -> Vec2
pub const fn center(self) -> Vec2
Computes and returns the center of the rectangle.
The center is calculated as the midpoint of the rectangle’s width and height from its bottom-left corner.
§Examples
use int_math::prelude::*;
let rect = Rect::new(10, 20, 30, 40);
let center = rect.center();
assert_eq!(center, Vec2::new(25, 40));
§Note
This method performs integer division, which means that if the size of the rectangle is odd, the center coordinates may not be exact integers.
Sourcepub fn with_offset(self, offset: Vec2) -> Rect
pub fn with_offset(self, offset: Vec2) -> Rect
Returns a new Rect
instance with the specified offset applied to its position.
The new rectangle will have the same size but its position will be adjusted by the given offset
.
§Arguments
offset
: TheVec2
to add to the rectangle’s position.
§Examples
use int_math::prelude::*;
let rect = Rect::new(10, 20, 30, 40);
let offset = Vec2::new(5, -5);
let new_rect = rect.with_offset(offset);
assert_eq!(new_rect.position, Vec2::new(15, 15));
Trait Implementations§
impl Copy 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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more