Point 结构体源码解析这是iced_core中定义的2D点类型是整个图形系统的基础构建块之一。️ 结构体定义/// 一个2D点#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]pubstructPointTf32{/// X坐标pubx:T,/// Y坐标puby:T,}关键特性属性说明T f32默认使用f32类型Debug支持格式化调试输出Clone, Copy支持复制栈上拷贝PartialEq, Eq支持相等性比较Default支持默认值 常量定义implPoint{/// 原点 (0, 0)pubconstORIGIN:SelfSelf::new(0.0,0.0);}注意ORIGIN只能在T f32时使用因为它硬编码为0.0。️ 核心方法构造函数implT:NumPointT{/// 创建新的Pointpubconstfnnew(x:T,y:T)-Self{Self{x,y}}}使用Numtrait 约束确保T是数值类型const fn允许在编译时创建常量距离计算/// 计算到另一个点的距离pubfndistance(self,to:Self)-TwhereT:Float,{letaself.x-to.x;letbself.y-to.y;a.hypot(b)// 计算平方根(a² b²)的平方根}数学公式distance √[(x₁ - x₂)² (y₁ - y₂)²] 类型转换从数组转换implTFrom[T;2]forPointTwhereT:Num,{fnfrom([x,y]:[T;2])-Self{Point{x,y}}}使用示例letpointPoint::from([10,20]);// [x, y]letpointPoint::from((10,20));// (x, y)转换为数组implTFromPointTfor[T;2]{fnfrom(point:PointT)-[T;2]{[point.x,point.y]}}使用示例letpointPoint::new(10,20);letarray:[i32;2]point.into();// [10, 20]➕ 向量运算点 向量 新点implTstd::ops::AddVectorTforPointTwhereT:std::ops::AddOutputT,{typeOutputSelf;fnadd(self,vector:VectorT)-Self{Self{x:self.xvector.x,y:self.yvector.y,}}}使用示例letpointPoint::new(5,5);letvectorVector::new(2,3);letnew_pointpointvector;// Point { x: 7, y: 8 }点 - 向量 新点letpointPoint::new(5,5);letvectorVector::new(2,3);letnew_pointpoint-vector;// Point { x: 3, y: 2 }点 - 点 向量implTstd::ops::SubPointTforPointTwhereT:std::ops::SubOutputT,{typeOutputVectorT;fnsub(self,point:Self)-VectorT{Vector::new(self.x-point.x,self.y-point.y)}}使用示例letp1Point::new(10,10);letp2Point::new(3,4);letvectorp1-p2;// Vector { x: 7, y: 6 }复合赋值运算letmutpointPoint::new(5,5);pointVector::new(2,3);// point 变为 (7, 8)point-Vector::new(1,2);// point 变为 (6, 6) 格式化输出implTfmt::DisplayforPointTwhereT:fmt::Display,{fnfmt(self,f:mutfmt::Formatter_)-fmt::Result{write!(f,Point {{ x: {}, y: {} }},self.x,self.y)}}输出示例letpointPoint::new(10.5,20.3);println!({},point);// 输出: Point { x: 10.5, y: 20.3 } f32 特定方法implPointf32{/// 四舍五入坐标pubfnround(self)-Self{Point{x:self.x.round(),y:self.y.round(),}}/// 转换为无符号整数坐标吸附到整数网格pubfnsnap(self)-Pointu32{Point{x:self.x.round()asu32,y:self.y.round()asu32,}}}使用示例letpointPoint::new(10.6,20.4);letroundedpoint.round();// Point { x: 11.0, y: 20.0 }letsnappedpoint.snap();// Pointu32 { x: 11, y: 20 }用途round()- 用于像素对齐避免模糊渲染snap()- 用于整数坐标系统如纹理映射 完整功能总结功能类别操作示例构造new(),ORIGINPoint::new(10, 20)距离distance()p1.distance(p2)转换From数组/元组Point::from([10, 20])运算 Vector,- Vectorpoint Vector::new(2, 3)差值Point - Pointp1 - p2→Vector复合,-point vector格式化Display,Debugprintln!({}, point)对齐round(),snap()point.snap()→Pointu32 设计亮点泛型设计- 支持任意数值类型默认f32运算符重载- 与Vector的自然交互类型安全- 点与向量的区别在类型层面体现零成本抽象- 所有操作都是简单的数学计算实用方法- 提供像素对齐等UI常用操作这个Point类型是整个 Iced 图形系统的基石简洁而强大
【Iced】core库几何数学结构体Point(point.rs)
Point 结构体源码解析这是iced_core中定义的2D点类型是整个图形系统的基础构建块之一。️ 结构体定义/// 一个2D点#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]pubstructPointTf32{/// X坐标pubx:T,/// Y坐标puby:T,}关键特性属性说明T f32默认使用f32类型Debug支持格式化调试输出Clone, Copy支持复制栈上拷贝PartialEq, Eq支持相等性比较Default支持默认值 常量定义implPoint{/// 原点 (0, 0)pubconstORIGIN:SelfSelf::new(0.0,0.0);}注意ORIGIN只能在T f32时使用因为它硬编码为0.0。️ 核心方法构造函数implT:NumPointT{/// 创建新的Pointpubconstfnnew(x:T,y:T)-Self{Self{x,y}}}使用Numtrait 约束确保T是数值类型const fn允许在编译时创建常量距离计算/// 计算到另一个点的距离pubfndistance(self,to:Self)-TwhereT:Float,{letaself.x-to.x;letbself.y-to.y;a.hypot(b)// 计算平方根(a² b²)的平方根}数学公式distance √[(x₁ - x₂)² (y₁ - y₂)²] 类型转换从数组转换implTFrom[T;2]forPointTwhereT:Num,{fnfrom([x,y]:[T;2])-Self{Point{x,y}}}使用示例letpointPoint::from([10,20]);// [x, y]letpointPoint::from((10,20));// (x, y)转换为数组implTFromPointTfor[T;2]{fnfrom(point:PointT)-[T;2]{[point.x,point.y]}}使用示例letpointPoint::new(10,20);letarray:[i32;2]point.into();// [10, 20]➕ 向量运算点 向量 新点implTstd::ops::AddVectorTforPointTwhereT:std::ops::AddOutputT,{typeOutputSelf;fnadd(self,vector:VectorT)-Self{Self{x:self.xvector.x,y:self.yvector.y,}}}使用示例letpointPoint::new(5,5);letvectorVector::new(2,3);letnew_pointpointvector;// Point { x: 7, y: 8 }点 - 向量 新点letpointPoint::new(5,5);letvectorVector::new(2,3);letnew_pointpoint-vector;// Point { x: 3, y: 2 }点 - 点 向量implTstd::ops::SubPointTforPointTwhereT:std::ops::SubOutputT,{typeOutputVectorT;fnsub(self,point:Self)-VectorT{Vector::new(self.x-point.x,self.y-point.y)}}使用示例letp1Point::new(10,10);letp2Point::new(3,4);letvectorp1-p2;// Vector { x: 7, y: 6 }复合赋值运算letmutpointPoint::new(5,5);pointVector::new(2,3);// point 变为 (7, 8)point-Vector::new(1,2);// point 变为 (6, 6) 格式化输出implTfmt::DisplayforPointTwhereT:fmt::Display,{fnfmt(self,f:mutfmt::Formatter_)-fmt::Result{write!(f,Point {{ x: {}, y: {} }},self.x,self.y)}}输出示例letpointPoint::new(10.5,20.3);println!({},point);// 输出: Point { x: 10.5, y: 20.3 } f32 特定方法implPointf32{/// 四舍五入坐标pubfnround(self)-Self{Point{x:self.x.round(),y:self.y.round(),}}/// 转换为无符号整数坐标吸附到整数网格pubfnsnap(self)-Pointu32{Point{x:self.x.round()asu32,y:self.y.round()asu32,}}}使用示例letpointPoint::new(10.6,20.4);letroundedpoint.round();// Point { x: 11.0, y: 20.0 }letsnappedpoint.snap();// Pointu32 { x: 11, y: 20 }用途round()- 用于像素对齐避免模糊渲染snap()- 用于整数坐标系统如纹理映射 完整功能总结功能类别操作示例构造new(),ORIGINPoint::new(10, 20)距离distance()p1.distance(p2)转换From数组/元组Point::from([10, 20])运算 Vector,- Vectorpoint Vector::new(2, 3)差值Point - Pointp1 - p2→Vector复合,-point vector格式化Display,Debugprintln!({}, point)对齐round(),snap()point.snap()→Pointu32 设计亮点泛型设计- 支持任意数值类型默认f32运算符重载- 与Vector的自然交互类型安全- 点与向量的区别在类型层面体现零成本抽象- 所有操作都是简单的数学计算实用方法- 提供像素对齐等UI常用操作这个Point类型是整个 Iced 图形系统的基石简洁而强大