image_renderer/
point.rs

1//! 点和坐标类型定义
2
3/// 整数坐标点
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5pub struct Point {
6    pub x: i32,
7    pub y: i32,
8}
9
10impl Point {
11    /// 创建新的点
12    pub fn new(x: i32, y: i32) -> Self {
13        Self { x, y }
14    }
15
16    /// 原点 (0, 0)
17    pub const ZERO: Point = Point { x: 0, y: 0 };
18}
19
20impl From<(i32, i32)> for Point {
21    fn from((x, y): (i32, i32)) -> Self {
22        Self::new(x, y)
23    }
24}
25
26/// 浮点坐标点
27#[derive(Debug, Clone, Copy, PartialEq, Default)]
28pub struct PointF {
29    pub x: f32,
30    pub y: f32,
31}
32
33impl PointF {
34    /// 创建新的浮点点
35    pub fn new(x: f32, y: f32) -> Self {
36        Self { x, y }
37    }
38
39    /// 原点 (0.0, 0.0)
40    pub const ZERO: PointF = PointF { x: 0.0, y: 0.0 };
41
42    /// 转换为整数点(四舍五入)
43    pub fn to_point(&self) -> Point {
44        Point::new(self.x.round() as i32, self.y.round() as i32)
45    }
46}
47
48impl From<(f32, f32)> for PointF {
49    fn from((x, y): (f32, f32)) -> Self {
50        Self::new(x, y)
51    }
52}
53
54impl From<Point> for PointF {
55    fn from(p: Point) -> Self {
56        Self::new(p.x as f32, p.y as f32)
57    }
58}
59
60/// IPoint 类型别名(整数坐标点)
61///
62/// 与 Point 相同,提供与 skia-safe 的兼容性
63pub type IPoint = Point;