Skip to main content

Rect

Struct Rect 

Source
pub struct Rect {
    pub min: Vec2,
    pub max: Vec2,
}
Expand description

2D 矩形,用于边界检测和 UI 布局

矩形使用最小点和最大点表示,确保 min.x <= max.xmin.y <= max.y

Fields§

§min: Vec2

最小点(左下角)

§max: Vec2

最大点(右上角)

Implementations§

Source§

impl Rect

Source

pub const ZERO: Rect

零大小的矩形

Source

pub fn new(min: Vec2, max: Vec2) -> Rect

创建新的矩形

§参数
  • min: 最小点
  • max: 最大点
§注意

如果 min 的某个分量大于 max 的对应分量,会自动交换以确保有效性。

§示例
use anvilkit_core::math::geometry::Rect;
use glam::Vec2;
 
let rect = Rect::new(Vec2::ZERO, Vec2::new(10.0, 20.0));
assert_eq!(rect.width(), 10.0);
assert_eq!(rect.height(), 20.0);
Source

pub fn from_center_size(center: Vec2, size: Vec2) -> Rect

从中心点和大小创建矩形

§示例
use anvilkit_core::math::geometry::Rect;
use glam::Vec2;
 
let rect = Rect::from_center_size(Vec2::new(5.0, 5.0), Vec2::new(10.0, 20.0));
assert_eq!(rect.center(), Vec2::new(5.0, 5.0));
Source

pub fn from_position_size(position: Vec2, size: Vec2) -> Rect

从位置和大小创建矩形(位置为左下角)

Source

pub fn width(&self) -> f32

获取矩形宽度

Source

pub fn height(&self) -> f32

获取矩形高度

Source

pub fn size(&self) -> Vec2

获取矩形大小

Source

pub fn center(&self) -> Vec2

获取矩形中心点

Source

pub fn area(&self) -> f32

获取矩形面积

Source

pub fn perimeter(&self) -> f32

获取矩形周长

Source

pub fn contains(&self, point: Vec2) -> bool

检查点是否在矩形内

§示例
use anvilkit_core::math::geometry::Rect;
use glam::Vec2;
 
let rect = Rect::new(Vec2::ZERO, Vec2::new(10.0, 10.0));
assert!(rect.contains(Vec2::new(5.0, 5.0)));
assert!(!rect.contains(Vec2::new(15.0, 5.0)));
Source

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

检查是否与另一个矩形相交

§示例
use anvilkit_core::math::geometry::Rect;
use glam::Vec2;
 
let rect1 = Rect::new(Vec2::ZERO, Vec2::new(10.0, 10.0));
let rect2 = Rect::new(Vec2::new(5.0, 5.0), Vec2::new(15.0, 15.0));
assert!(rect1.intersects(&rect2));
Source

pub fn intersects_circle(&self, circle: &Circle) -> bool

检查是否与圆形相交

Source

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

计算与另一个矩形的交集

§返回

如果两个矩形相交,返回交集矩形;否则返回 None

Source

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

计算包含两个矩形的最小矩形

Source

pub fn expand_to_include(&mut self, point: Vec2)

扩展矩形以包含指定点

Source

pub fn expand(&self, amount: f32) -> Rect

按指定量扩展矩形

§参数
  • amount: 扩展量,正值扩展,负值收缩
Source

pub fn is_valid(&self) -> bool

检查矩形是否有效(非负大小)

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Rect

Returns a duplicate 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<(), Error>

Formats the value using the given formatter. Read more
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 UnsafeUnpin 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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,