Rectangle

Struct Rectangle 

Source
pub struct Rectangle {
    pub x1: f64,
    pub y1: f64,
    pub x2: f64,
    pub y2: f64,
}
Expand description

表示二维平面上的矩形的结构体。

矩形由左上角顶点 (x1, y1) 和右下角顶点 (x2, y2) 定义。

Fields§

§x1: f64

左上角顶点的 x 坐标。

§y1: f64

左上角顶点的 y 坐标。

§x2: f64

右下角顶点的 x 坐标。

§y2: f64

右下角顶点的 y 坐标。

Implementations§

Source§

impl Rectangle

Source

pub fn new_from_corner(x: f64, y: f64, width: f64, height: f64) -> Self

通过给定的左上角顶点坐标、宽度和高度构建矩形。

§参数
  • x: 左上角顶点的 x 坐标。
  • y: 左上角顶点的 y 坐标。
  • width: 矩形的宽度。
  • height: 矩形的高度。
§返回

返回通过左上角顶点坐标、宽度和高度定义的矩形。

§注意

如果提供的参数不合法,将会触发 panic。

Source

pub fn scale(&mut self, sx: f64, sy: f64)

缩放矩形。

§参数
  • sx: x 方向的缩放比例。
  • sy: y 方向的缩放比例。
§注意

该方法会修改矩形的右下角顶点坐标,使矩形按照指定的比例进行缩放。

Source

pub fn rotate(&self, angle: f64) -> Rectangle

旋转一个矩形(绕原点)。

§参数
  • angle: 旋转角度(弧度)。
§返回

返回一个新的矩形,表示原矩形绕原点逆时针旋转一定角度后的状态。

Source

pub fn area(&self) -> f64

计算矩形的面积。

§返回

返回矩形的面积。

Source

pub fn perimeter(&self) -> f64

计算矩形的周长。

§返回

返回矩形的周长。

Source

pub fn point_inside(&self, x: f64, y: f64) -> bool

判断点是否在矩形内。

§参数
  • x: 点的 x 坐标。
  • y: 点的 y 坐标。
§返回

如果点在矩形内,返回 true,否则返回 false

Source

pub fn rotate_around_point( &self, angle: f64, x_rot: f64, y_rot: f64, ) -> Rectangle

根据点旋转矩形。

§参数
  • angle: 旋转角度(弧度)。
  • x_rot: 旋转点的 x 坐标。
  • y_rot: 旋转点的 y 坐标。
§返回

旋转后的矩形。

Source

pub fn intersect(&self, other: &Rectangle) -> bool

判断两个矩形是否相交。

§参数
  • other: 另一个矩形。
§返回

如果两个矩形相交,则返回 true,否则返回 false

Source

pub fn contains(&self, other: &Rectangle) -> bool

判断当前矩形是否包含另一个矩形。

§参数
  • other: 要检查的矩形。
§返回

如果当前矩形包含另一个矩形,则返回 true,否则返回 false

Source

pub fn inscribed_circle(&self) -> Circle

计算矩形的内切圆。

§返回

返回一个包含内切圆信息的 Circle 结构体。

Trait Implementations§

Source§

impl Debug for Rectangle

Source§

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

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

impl PartialEq for Rectangle

Source§

fn eq(&self, other: &Rectangle) -> 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 StructuralPartialEq for Rectangle

Auto Trait Implementations§

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