Skip to main content

path_kit/
rect.rs

1//! 矩形,由 (left, top, right, bottom) 定义。
2//! Rectangle defined by (left, top, right, bottom) coordinates.
3
4use crate::bridge::ffi;
5
6/// 矩形,由 (left, top, right, bottom) 定义。
7/// Rectangle defined by (left, top, right, bottom) coordinates.
8#[derive(Debug, Clone, Copy, PartialEq)]
9pub struct Rect {
10    /// 左边界 / Left edge
11    pub left: f32,
12    /// 上边界 / Top edge
13    pub top: f32,
14    /// 右边界 / Right edge
15    pub right: f32,
16    /// 下边界 / Bottom edge
17    pub bottom: f32,
18}
19
20impl Rect {
21    /// 创建矩形。Creates a rectangle from bounds.
22    pub const fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
23        Self {
24            left,
25            top,
26            right,
27            bottom,
28        }
29    }
30
31    /// 宽度。Returns the width.
32    pub fn width(&self) -> f32 {
33        self.right - self.left
34    }
35
36    /// 高度。Returns the height.
37    pub fn height(&self) -> f32 {
38        self.bottom - self.top
39    }
40
41    /// 是否为空矩形。Returns true if the rect has zero or negative width/height.
42    pub fn is_empty(&self) -> bool {
43        self.left >= self.right || self.top >= self.bottom
44    }
45}
46
47impl From<Rect> for ffi::Rect {
48    fn from(r: Rect) -> Self {
49        ffi::Rect {
50            fLeft: r.left,
51            fTop: r.top,
52            fRight: r.right,
53            fBottom: r.bottom,
54        }
55    }
56}
57
58impl From<ffi::Rect> for Rect {
59    fn from(r: ffi::Rect) -> Self {
60        Self {
61            left: r.fLeft,
62            top: r.fTop,
63            right: r.fRight,
64            bottom: r.fBottom,
65        }
66    }
67}