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

source

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 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 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 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 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 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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.