diff options
author | 2023-04-27 16:09:39 +0200 | |
---|---|---|
committer | 2023-04-27 16:09:39 +0200 | |
commit | c31ab8eee6a49a48b4e6ad92207b8ee0360a0eff (patch) | |
tree | 0b099fc1e3810c2709a40627e11da5dc4988822b /core/src/rectangle.rs | |
parent | e3730106e9d4f75de199e1b83cf285b8ff031968 (diff) | |
parent | a755472ee35dfb7839f989becafc6028921a3b99 (diff) | |
download | iced-c31ab8eee6a49a48b4e6ad92207b8ee0360a0eff.tar.gz iced-c31ab8eee6a49a48b4e6ad92207b8ee0360a0eff.tar.bz2 iced-c31ab8eee6a49a48b4e6ad92207b8ee0360a0eff.zip |
Merge pull request #1811 from iced-rs/incremental-rendering
Incremental rendering
Diffstat (limited to 'core/src/rectangle.rs')
-rw-r--r-- | core/src/rectangle.rs | 48 |
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> { |