summaryrefslogtreecommitdiffstats
path: root/core/src/rectangle.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2023-05-11 16:45:08 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-11 16:45:08 +0200
commit669f7cc74b2e7918e86a8197916f503f2d3d9b93 (patch)
treeacb365358235be6ce115b50db9404d890b6e77a6 /core/src/rectangle.rs
parentbc62013b6cde52174bf4c4286939cf170bfa7760 (diff)
parent63d3fc6996b848e10e77e6924bfebdf6ba82852e (diff)
downloadiced-669f7cc74b2e7918e86a8197916f503f2d3d9b93.tar.gz
iced-669f7cc74b2e7918e86a8197916f503f2d3d9b93.tar.bz2
iced-669f7cc74b2e7918e86a8197916f503f2d3d9b93.zip
Merge pull request #1830 from iced-rs/advanced-text
Advanced text
Diffstat (limited to 'core/src/rectangle.rs')
-rw-r--r--core/src/rectangle.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs
index 4fe91519..7ff324cb 100644
--- a/core/src/rectangle.rs
+++ b/core/src/rectangle.rs
@@ -66,6 +66,11 @@ impl Rectangle<f32> {
Size::new(self.width, self.height)
}
+ /// Returns the area of the [`Rectangle`].
+ pub fn area(&self) -> f32 {
+ self.width * self.height
+ }
+
/// Returns true if the given [`Point`] is contained in the [`Rectangle`].
pub fn contains(&self, point: Point) -> bool {
self.x <= point.x
@@ -74,6 +79,15 @@ impl Rectangle<f32> {
&& point.y <= self.y + self.height
}
+ /// Returns true if the current [`Rectangle`] is completely within the given
+ /// `container`.
+ pub fn is_within(&self, container: &Rectangle) -> bool {
+ container.contains(self.position())
+ && container.contains(
+ self.position() + Vector::new(self.width, self.height),
+ )
+ }
+
/// Computes the intersection with the given [`Rectangle`].
pub fn intersection(
&self,
@@ -100,6 +114,30 @@ impl Rectangle<f32> {
}
}
+ /// Returns whether the [`Rectangle`] intersects with the given one.
+ pub fn intersects(&self, other: &Self) -> bool {
+ self.intersection(other).is_some()
+ }
+
+ /// Computes the union with the given [`Rectangle`].
+ pub fn union(&self, other: &Self) -> Self {
+ let x = self.x.min(other.x);
+ let y = self.y.min(other.y);
+
+ let lower_right_x = (self.x + self.width).max(other.x + other.width);
+ let lower_right_y = (self.y + self.height).max(other.y + other.height);
+
+ let width = lower_right_x - x;
+ let height = lower_right_y - y;
+
+ Rectangle {
+ x,
+ y,
+ width,
+ height,
+ }
+ }
+
/// Snaps the [`Rectangle`] to __unsigned__ integer coordinates.
pub fn snap(self) -> Rectangle<u32> {
Rectangle {
@@ -109,6 +147,16 @@ impl Rectangle<f32> {
height: self.height as u32,
}
}
+
+ /// Expands the [`Rectangle`] a given amount.
+ pub fn expand(self, amount: f32) -> Self {
+ Self {
+ x: self.x - amount,
+ y: self.y - amount,
+ width: self.width + amount * 2.0,
+ height: self.height + amount * 2.0,
+ }
+ }
}
impl std::ops::Mul<f32> for Rectangle<f32> {