#[repr(C)]pub struct Bounds<T: Clone + Debug + Default + PartialEq> {
pub origin: Point<T>,
pub size: Size<T>,
}Expand description
重导出几何类型(Point、Size、Bounds 等) 表示二维空间中的矩形区域,包含原点和尺寸。
Bounds 结构体对类型 T 是泛型的,T 表示坐标系统的类型。
原点表示为 Point<T>,定义矩形的左上角;
尺寸表示为 Size<T>,定义矩形的宽度和高度。
§示例
let origin = Point { x: 0, y: 0 };
let size = Size { width: 10, height: 20 };
let bounds = Bounds::new(origin, size);
assert_eq!(bounds.origin, origin);
assert_eq!(bounds.size, size);Fields§
§origin: Point<T>该区域的原点。
size: Size<T>矩形的尺寸。
Implementations§
Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn from_corners(top_left: Point<T>, bottom_right: Point<T>) -> Self
pub fn from_corners(top_left: Point<T>, bottom_right: Point<T>) -> Self
从两个角点构造 Bounds:左上角和右下角。
此函数根据提供的角点计算 Bounds 的原点和尺寸。
原点设为左上角,尺寸由右下角和左上角的 x、y 坐标差值确定。
§参数
top_left- 表示矩形左上角的Point<T>。bottom_right- 表示矩形右下角的Point<T>。
§返回值
返回包含两个角点定义区域的 Bounds<T>。
§示例
let top_left = Point { x: 0, y: 0 };
let bottom_right = Point { x: 10, y: 10 };
let bounds = Bounds::from_corners(top_left, bottom_right);
assert_eq!(bounds.origin, top_left);
assert_eq!(bounds.size.width, 10);
assert_eq!(bounds.size.height, 10);Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn centered_at(center: Point<T>, size: Size<T>) -> Self
pub fn centered_at(center: Point<T>, size: Size<T>) -> Self
创建以给定点为中心的新 bounds。
Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn top_center(&self) -> Point<T>
pub fn top_center(&self) -> Point<T>
返回 bounds 的顶部中心点。
Sourcepub fn bottom_center(&self) -> Point<T>
pub fn bottom_center(&self) -> Point<T>
返回 bounds 的底部中心点。
Sourcepub fn left_center(&self) -> Point<T>
pub fn left_center(&self) -> Point<T>
返回 bounds 的左侧中心点。
Sourcepub fn right_center(&self) -> Point<T>
pub fn right_center(&self) -> Point<T>
返回 bounds 的右侧中心点。
Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn intersects(&self, other: &Bounds<T>) -> bool
pub fn intersects(&self, other: &Bounds<T>) -> bool
检查此 Bounds 是否与另一个 Bounds 相交。
两个 Bounds 实例相交是指它们在二维空间中存在重叠区域。
此方法检查两个 bounds 之间是否存在任何重叠区域。
§参数
other- 要检查相交的另一个Bounds的引用。
§返回值
如果两个 bounds 之间存在任何相交则返回 true,否则返回 false。
§示例
let bounds1 = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 10 },
};
let bounds2 = Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 10, height: 10 },
};
let bounds3 = Bounds {
origin: Point { x: 20, y: 20 },
size: Size { width: 10, height: 10 },
};
assert_eq!(bounds1.intersects(&bounds2), true); // 重叠的 bounds
assert_eq!(bounds1.intersects(&bounds3), false); // 不重叠的 boundsSource§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn half_perimeter(&self) -> T
pub fn half_perimeter(&self) -> T
计算矩形的半周长。
半周长计算为矩形宽度和高度之和。
此方法对类型 T 是泛型的,T 必须实现 Sub trait(用于从 bounds 的原点和尺寸计算宽度和高度)
以及 Add trait(用于将宽度和高度相加得到半周长)。
§示例
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 20 },
};
let half_perimeter = bounds.half_perimeter();
assert_eq!(half_perimeter, 30);Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn dilate(&self, amount: T) -> Bounds<T>
pub fn dilate(&self, amount: T) -> Bounds<T>
在所有方向上按指定量膨胀 bounds。
此方法按给定的 amount 扩展 bounds,增大尺寸并调整原点,
使 bounds 在所有方向上均匀向外扩展。
结果 bounds 的宽度和高度将增加 amount 的两倍(因为在两个方向上都扩展),
原点将在 x 和 y 方向上各移动 -amount。
§参数
amount- 膨胀 bounds 的量。
§示例
let mut bounds = Bounds {
origin: Point { x: 10, y: 10 },
size: Size { width: 10, height: 10 },
};
let expanded_bounds = bounds.dilate(5);
assert_eq!(expanded_bounds, Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 20, height: 20 },
});Source§impl<T: PartialOrd + Add<T, Output = T> + Sub<Output = T> + Clone + Debug + Default + PartialEq> Bounds<T>
impl<T: PartialOrd + Add<T, Output = T> + Sub<Output = T> + Clone + Debug + Default + PartialEq> Bounds<T>
Sourcepub fn intersect(&self, other: &Self) -> Self
pub fn intersect(&self, other: &Self) -> Self
计算两个 Bounds 对象的交集。
此方法计算两个 Bounds 的重叠区域。如果 bounds 不相交,
结果 Bounds 的宽度和高度将为零。
§参数
other- 要与之相交的另一个Bounds的引用。
§返回值
返回表示相交区域的 Bounds。如果没有相交,
返回的 Bounds 的宽度和高度将为零。
§示例
let bounds1 = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 10 },
};
let bounds2 = Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 10, height: 10 },
};
let intersection = bounds1.intersect(&bounds2);
assert_eq!(intersection, Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 5, height: 5 },
});Sourcepub fn union(&self, other: &Self) -> Self
pub fn union(&self, other: &Self) -> Self
计算两个 Bounds 的并集。
此方法计算包含当前 Bounds 和 other Bounds 的最小 Bounds。
结果 Bounds 的原点将是两个 Bounds 原点的最小值,
尺寸将包含两个 Bounds 的最远边界。
§参数
other- 要与之创建并集的另一个Bounds的引用。
§返回值
返回表示两个 Bounds 并集的 Bounds。
§示例
let bounds1 = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 10 },
};
let bounds2 = Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 15, height: 15 },
};
let union_bounds = bounds1.union(&bounds2);
assert_eq!(union_bounds, Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 20, height: 20 },
});Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn space_within(&self, outer: &Self) -> Edges<T>
pub fn space_within(&self, outer: &Self) -> Edges<T>
计算外部 bounds 内的可用空间。
Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn bottom_right(&self) -> Point<T>
pub fn bottom_right(&self) -> Point<T>
Sourcepub fn bottom_left(&self) -> Point<T>
pub fn bottom_left(&self) -> Point<T>
Source§impl<T> Bounds<T>
impl<T> Bounds<T>
Sourcepub fn contains(&self, point: &Point<T>) -> bool
pub fn contains(&self, point: &Point<T>) -> bool
检查给定的点是否在 bounds 内。
此方法判断一个点是否位于 bounds 定义的矩形内(包括边界)。 如果点的 x 坐标大于等于左边缘且小于等于右边缘, y 坐标大于等于上边缘且小于等于下边缘,则认为该点在内部。
§参数
point- 要检查的Point<T>的引用。
§返回值
如果点在 bounds 内则返回 true,否则返回 false。
§示例
let bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 10, height: 10 },
};
let inside_point = Point { x: 5, y: 5 };
let outside_point = Point { x: 15, y: 15 };
assert!(bounds.contains(&inside_point));
assert!(!bounds.contains(&outside_point));Sourcepub fn is_contained_within(&self, other: &Self) -> bool
pub fn is_contained_within(&self, other: &Self) -> bool
检查此 bounds 是否完全包含在另一个 bounds 内。
此方法判断当前 bounds 是否完全被给定的 bounds 包围。 如果 bounds 的原点(左上角)和右下角都包含在另一个 bounds 内, 则认为此 bounds 被包含在另一个 bounds 内。
§参数
other- 可能包含此 bounds 的另一个Bounds的引用。
§返回值
如果此 bounds 完全在另一个 bounds 内则返回 true,否则返回 false。
§示例
let outer_bounds = Bounds {
origin: Point { x: 0, y: 0 },
size: Size { width: 20, height: 20 },
};
let inner_bounds = Bounds {
origin: Point { x: 5, y: 5 },
size: Size { width: 10, height: 10 },
};
let overlapping_bounds = Bounds {
origin: Point { x: 15, y: 15 },
size: Size { width: 10, height: 10 },
};
assert!(inner_bounds.is_contained_within(&outer_bounds));
assert!(!overlapping_bounds.is_contained_within(&outer_bounds));Sourcepub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U>
pub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U>
对 bounds 的原点和尺寸应用函数,生成新的 Bounds<U>。
此方法允许通过指定闭包将 Bounds<T> 转换为 Bounds<U>,闭包定义了两种类型之间的转换方式。
闭包应用于 origin 和 size 字段,生成所需类型的新 bounds。
§参数
f- 接受类型T的值并返回类型U的值的闭包。
§返回值
返回原点和尺寸通过给定函数映射后的新 Bounds<U>。
§示例
let bounds = Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size { width: 10.0, height: 20.0 },
};
let new_bounds = bounds.map(|value| value as f64 * 1.5);
assert_eq!(new_bounds, Bounds {
origin: Point { x: 15.0, y: 15.0 },
size: Size { width: 15.0, height: 30.0 },
});Sourcepub fn map_origin(self, f: impl Fn(T) -> T) -> Bounds<T>
pub fn map_origin(self, f: impl Fn(T) -> T) -> Bounds<T>
对 bounds 的原点应用函数,生成具有新原点的新 Bounds
§示例
let bounds = Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size { width: 10.0, height: 20.0 },
};
let new_bounds = bounds.map_origin(|value| value * 1.5);
assert_eq!(new_bounds, Bounds {
origin: Point { x: 15.0, y: 15.0 },
size: Size { width: 10.0, height: 20.0 },
});Sourcepub fn map_size(self, f: impl Fn(T) -> T) -> Bounds<T>
pub fn map_size(self, f: impl Fn(T) -> T) -> Bounds<T>
对 bounds 的尺寸应用函数,生成具有新尺寸的新 Bounds
§示例
let bounds = Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size { width: 10.0, height: 20.0 },
};
let new_bounds = bounds.map_size(|value| value * 1.5);
assert_eq!(new_bounds, Bounds {
origin: Point { x: 10.0, y: 10.0 },
size: Size { width: 15.0, height: 30.0 },
});Source§impl<T: PartialOrd + Clone + Debug + Default + PartialEq> Bounds<T>
检查 bounds 是否表示空区域。
impl<T: PartialOrd + Clone + Debug + Default + PartialEq> Bounds<T>
检查 bounds 是否表示空区域。
§返回值
如果 bounds 的宽度或高度小于或等于零则返回 true,表示空区域。
Source§impl Bounds<Pixels>
impl Bounds<Pixels>
Sourcepub fn scale(&self, factor: f32) -> Bounds<ScaledPixels>
pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels>
按给定因子缩放 bounds,通常用于调整显示缩放。
此方法将 bounds 的原点和尺寸乘以提供的缩放因子,
生成一个新的 Bounds<ScaledPixels>,根据因子按比例放大或缩小。
这可用于确保 bounds 在不同显示密度下正确缩放。
§参数
factor- 应用于原点和尺寸的缩放因子,通常是显示器的缩放因子。
§返回值
返回表示缩放后 bounds 的新 Bounds<ScaledPixels>。
§示例
let bounds = Bounds {
origin: Point { x: Pixels::from(10.0), y: Pixels::from(20.0) },
size: Size { width: Pixels::from(30.0), height: Pixels::from(40.0) },
};
let display_scale_factor = 2.0;
let scaled_bounds = bounds.scale(display_scale_factor);
assert_eq!(scaled_bounds, Bounds {
origin: Point {
x: ScaledPixels::from(20.0),
y: ScaledPixels::from(40.0),
},
size: Size {
width: ScaledPixels::from(60.0),
height: ScaledPixels::from(80.0)
},
});Sourcepub fn to_device_pixels(self, factor: f32) -> Bounds<DevicePixels>
pub fn to_device_pixels(self, factor: f32) -> Bounds<DevicePixels>
将 bounds 从逻辑像素转换为物理像素
Trait Implementations§
impl<T: Copy + Clone + Debug + Default + PartialEq> Copy for Bounds<T>
Source§impl<'de, T> Deserialize<'de> for Bounds<T>
impl<'de, T> Deserialize<'de> for Bounds<T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: Clone + Debug + Default + PartialEq + Display + Add<T, Output = T>> Display for Bounds<T>
impl<T: Clone + Debug + Default + PartialEq + Display + Add<T, Output = T>> Display for Bounds<T>
impl<T: Eq + Clone + Debug + Default + PartialEq> Eq for Bounds<T>
Source§impl<T: Clone + Debug + Default + PartialEq> From<BoundsRefinement<T>> for Bounds<T>
impl<T: Clone + Debug + Default + PartialEq> From<BoundsRefinement<T>> for Bounds<T>
Source§fn from(value: BoundsRefinement<T>) -> Self
fn from(value: BoundsRefinement<T>) -> Self
Source§impl<T, S> MulAssign<S> for Bounds<T>
impl<T, S> MulAssign<S> for Bounds<T>
Source§fn mul_assign(&mut self, rhs: S)
fn mul_assign(&mut self, rhs: S)
*= operation. Read moreSource§impl<T: Clone + Debug + Default + PartialEq> Refineable for Bounds<T>
impl<T: Clone + Debug + Default + PartialEq> Refineable for Bounds<T>
type Refinement = BoundsRefinement<T>
Source§fn refine(&mut self, refinement: &Self::Refinement)
fn refine(&mut self, refinement: &Self::Refinement)
Source§fn refined(self, refinement: Self::Refinement) -> Self
fn refined(self, refinement: Self::Refinement) -> Self
self 并在其上调用 refineSource§fn is_superset_of(&self, refinement: &Self::Refinement) -> bool
fn is_superset_of(&self, refinement: &Self::Refinement) -> bool
true Read moreSource§fn subtract(&self, refinement: &Self::Refinement) -> Self::Refinement
fn subtract(&self, refinement: &Self::Refinement) -> Self::Refinement
impl<T: PartialEq + Clone + Debug + Default + PartialEq> StructuralPartialEq for Bounds<T>
Auto Trait Implementations§
impl<T> Forget for Bounds<T>
impl<T> Freeze for Bounds<T>
impl<T> Move for Bounds<T>
impl<T> RefUnwindSafe for Bounds<T>
impl<T> Send for Bounds<T>
impl<T> Sync for Bounds<T>
impl<T> Unpin for Bounds<T>
impl<T> UnsafeUnpin for Bounds<T>
impl<T> UnwindSafe for Bounds<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> CustomError for T
impl<T> CustomError for T
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more