From a6c1534b59936c60c3fd2d5e957a1363741dbb0d Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Sat, 14 Sep 2024 15:48:28 +0200 Subject: [PATCH] wip feat(area): add `extend` method with tests wip: add aditionnal tests --- src/content/area.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/content/area.rs b/src/content/area.rs index d7cb050..6d195c4 100644 --- a/src/content/area.rs +++ b/src/content/area.rs @@ -1,3 +1,5 @@ +use std::cmp::{max, min}; + use super::{ContentError, Size}; /// Location at which to display the subtitle. @@ -74,6 +76,14 @@ impl Area { && self.0.y1 <= area.0.y1 && self.0.y2 >= area.0.y2 } + + /// Extend the area from an other `Area` + pub fn extend(&mut self, area: Self) { + self.0.x1 = min(self.0.x1, area.0.x1); + self.0.y1 = min(self.0.y1, area.0.y1); + self.0.x2 = max(self.0.x2, area.0.x2); + self.0.y2 = max(self.0.y2, area.0.y2); + } } impl TryFrom for Area { @@ -299,4 +309,27 @@ mod tests { y2: 19, }))); } + + #[test] + fn area_extend() { + let area_ref = Area(AREA_REF); + + assert!( + { + let mut new = area_ref; + new.extend(Area(AreaValues { + x1: 10, + y1: 20, + x2: 20, + y2: 30, + })); + new + } == Area(AreaValues { + x1: 10, + y1: 10, + x2: 20, + y2: 30 + }) + ) + } }