【Iced】core库Size 结构体源码解析(size.rs)
这是iced_core中定义的2D尺寸类型用于表示宽度和高度。️ 结构体定义/// 2维空间中的尺寸大小#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]pubstructSizeTf32{/// 宽度pubwidth:T,/// 高度pubheight:T,}关键特性属性说明T f32默认使用f32类型Debug支持格式化调试输出Clone, Copy支持复制栈上拷贝PartialEq, Eq支持相等性比较Hash支持作为哈希键Default支持默认值0, 0️ 泛型构造函数implTSizeT{/// 创建新的Sizepubconstfnnew(width:T,height:T)-Self{Size{width,height}}} 常量定义f32版本implSize{/// 零尺寸 (0, 0)pubconstZERO:SizeSize::new(0.,0.);/// 单位尺寸 (1, 1)pubconstUNIT:SizeSize::new(1.,1.);/// 无限尺寸 (∞, ∞)pubconstINFINITE:SizeSize::new(f32::INFINITY,f32::INFINITY);} 核心方法1.min/max - 分量比较/// 返回两个尺寸每个分量的最小值pubfnmin(self,other:Self)-Self{Size{width:self.width.min(other.width),height:self.height.min(other.height),}}/// 返回两个尺寸每个分量的最大值pubfnmax(self,other:Self)-Self{Size{width:self.width.max(other.width),height:self.height.max(other.height),}}示例letaSize::new(10,20);letbSize::new(15,5);letmina.min(b);// Size { width: 10, height: 5 }letmaxa.max(b);// Size { width: 15, height: 20 }2.expand - 扩展尺寸/// 扩展当前尺寸pubfnexpand(self,other:implIntoSize)-Self{letotherother.into();Size{width:self.widthother.width,height:self.heightother.height,}}示例letsizeSize::new(100,100);letexpandedsize.expand(Size::new(20,30));// (120, 130)3.rotate - 旋转变换/// 旋转尺寸返回包含旋转后尺寸的最小矩形pubfnrotate(self,rotation:Radians)-Size{letradiansf32::from(rotation);Size{width:(self.width*radians.cos()).abs()(self.height*radians.sin()).abs(),height:(self.width*radians.sin()).abs()(self.height*radians.cos()).abs(),}}数学原理旋转矩形后的外接矩形尺寸计算示例letsizeSize::new(100,50);letrotatedsize.rotate(Radians::from(45.0_f32.to_radians()));// 计算旋转45度后的外接矩形尺寸4.ratio - 宽高比约束/// 应用宽高比约束不超过原尺寸pubconstfnratio(self,aspect_ratio:f32)-Size{Size{width:(self.height*aspect_ratio).min(self.width),height:(self.width/aspect_ratio).min(self.height),}}用途在保持宽高比的同时确保尺寸不超过原始边界示例letcontainerSize::new(200,100);letimagecontainer.ratio(16.0/9.0);// 保持16:9宽高比// 结果尽可能大但不超过200x100 Length 特殊实现implSizeLength{/// 检查宽度或高度是否为0固定长度#[inline]pubfnis_void(self)-bool{matches!(self.width,Length::Fixed(0.0))||matches!(self.height,Length::Fixed(0.0))}}用途在布局系统中检查尺寸是否为空 类型转换从数组/元组创建implTFrom[T;2]forSizeT{fnfrom([width,height]:[T;2])-Self{Size{width,height}}}implTFrom(T,T)forSizeT{fnfrom((width,height):(T,T))-Self{Self{width,height}}}// 特殊从(u32, u32)到f32 SizeimplFrom(u32,u32)forSize{fnfrom((width,height):(u32,u32))-Self{Size::new(widthasf32,heightasf32)}}与Vector的转换implTFromVectorTforSizeT{fnfrom(vector:VectorT)-Self{Size{width:vector.x,height:vector.y,}}}implTFromSizeTforVectorT{fnfrom(size:SizeT)-Self{Vector::new(size.width,size.height)}}转换为数组implTFromSizeTfor[T;2]{fnfrom(size:SizeT)-Self{[size.width,size.height]}}➗ 数学运算1.加减运算implTstd::ops::AddforSizeTwhereT:std::ops::AddOutputT{typeOutputSizeT;fnadd(self,rhs:Self)-Self::Output{Size{width:self.widthrhs.width,height:self.heightrhs.height,}}}// 类似的Sub实现2.标量乘除implTstd::ops::MulTforSizeTwhereT:std::ops::MulOutputTCopy{typeOutputSizeT;fnmul(self,rhs:T)-Self::Output{Size{width:self.width*rhs,height:self.height*rhs,}}}// 类似的Div实现3.向量缩放implTstd::ops::MulVectorTforSizeTwhereT:std::ops::MulOutputTCopy{typeOutputSizeT;fnmul(self,scale:VectorT)-Self::Output{Size{width:self.width*scale.x,height:self.height*scale.y,}}}示例letsizeSize::new(100,200);letscaledsize*2.0;// (200, 400)letscaled_vecsize*Vector::new(2.0,0.5);// (200, 100) 功能总结功能类别操作示例构造new(),ZERO,UNIT,INFINITESize::new(100, 200)比较min(),max()size.min(other_size)变换expand(),rotate(),ratio()size.rotate(45°)检查is_void()size.is_void()转换From/Into 数组/元组/VectorSize::from([100, 200])运算,-,*,/size1 size2缩放* 标量,* Vectorsize * Vector::new(2, 3) 设计亮点泛型灵活- 支持任意数值类型默认f32常量完备- ZERO, UNIT, INFINITE 覆盖常见用例几何完备- 支持旋转、宽高比等高级操作类型互通- 与Vector无缝转换运算符重载- 直观的数学操作布局集成- 与Length类型配合用于UI布局这个Size类型是 Iced 布局系统和图形渲染的核心组件提供了丰富的尺寸操作功能
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2418882.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!