Struct 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

Source

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 a Vector.
  • size: The size of the rectangle, including its width and height, as a Vector.
§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);
Source

pub fn top(self) -> Fp

Source

pub const fn bottom(self) -> Fp

Source

pub const fn left(self) -> Fp

Source

pub fn right(self) -> Fp

Source

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

pub fn area(&self) -> Fp

Calculates the area of the rectangle.

Source

pub fn perimeter(&self) -> Fp

Calculates the perimeter of the rectangle.

Source

pub fn intersection(&self, other: &Self) -> Option<Self>

Calculates the intersection of two rectangles.

Source

pub fn union(&self, other: &Self) -> Self

Calculates the union of two rectangles.

Source

pub fn contains_point(&self, point: &Vector) -> bool

Checks if a point is inside the rectangle.

Source

pub fn contains_rect(&self, other: &Self) -> bool

Checks if another rectangle is completely inside this rectangle.

Source

pub fn is_overlapping(self, other: Self) -> bool

Source

pub fn expanded(&self, offset: Vector) -> Self

Expands the rectangle by a given offset.

Source

pub fn contracted(&self, offset: Vector) -> Self

Contracts the rectangle by a given offset.

Source

pub fn aspect_ratio(&self) -> Fp

Calculates the aspect ratio of the rectangle.

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Rect

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Rect

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Rect

Source§

fn default() -> Rect

Returns the “default value” for a type. Read more
Source§

impl Display for Rect

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<(f32, f32, f32, f32)> for Rect

Source§

fn from(values: (f32, f32, f32, f32)) -> Self

Converts to this type from the input type.
Source§

impl From<(i16, i16, i16, i16)> for Rect

Source§

fn from(values: (i16, i16, i16, i16)) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Rect

Source§

fn eq(&self, other: &Rect) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Rect

Source§

impl Eq for Rect

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.