From afae7d3f0f8b03729e3f1342ef4d2182e9e303f9 Mon Sep 17 00:00:00 2001 From: mental Date: Sun, 4 Mar 2007 19:05:38 +0000 Subject: [PATCH] Convert union and intersection to use NR::Maybe --- src/libnr/nr-rect.cpp | 39 ++++++++++++++++++++++++++------------- src/libnr/nr-rect.h | 4 ++-- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/libnr/nr-rect.cpp b/src/libnr/nr-rect.cpp index 460816021..c4e684f1f 100644 --- a/src/libnr/nr-rect.cpp +++ b/src/libnr/nr-rect.cpp @@ -260,27 +260,40 @@ void Rect::expandTo(Point p) { } /** Returns the set of points shared by both rectangles. */ -Maybe Rect::intersection(const Rect &a, const Rect &b) { - Rect r; - for ( int i=0 ; i < 2 ; i++ ) { - r._min[i] = MAX(a._min[i], b._min[i]); - r._max[i] = MIN(a._max[i], b._max[i]); - - if ( r._min[i] > r._max[i] ) { - return Nothing(); - } +Maybe Rect::intersection(Maybe const &a, Maybe const &b) { + if ( a == Nothing() || b == Nothing() ) { + return Nothing(); + } else { + Rect const &ra=a.assume(); + Rect const &rb=b.assume(); + Rect r; + for ( int i=0 ; i < 2 ; i++ ) { + r._min[i] = MAX(ra._min[i], rb._min[i]); + r._max[i] = MIN(ra._max[i], rb._max[i]); + if ( r._min[i] > r._max[i] ) { + return Nothing(); + } } return r; + } } /** returns the smallest rectangle containing both rectangles */ -Rect Rect::union_bounds(const Rect &a, const Rect &b) { - Rect r; +Maybe Rect::union_bounds(Maybe const &a, Maybe const &b) { + if ( a == Nothing() ) { + return b; + } else if ( b == Nothing() ) { + return a; + } else { + Rect const &ra=a.assume(); + Rect const &rb=b.assume(); + Rect r; for ( int i=0; i < 2 ; i++ ) { - r._min[i] = MIN(a._min[i], b._min[i]); - r._max[i] = MAX(a._max[i], b._max[i]); + r._min[i] = MIN(ra._min[i], rb._min[i]); + r._max[i] = MAX(ra._max[i], rb._max[i]); } return r; + } } } // namespace NR diff --git a/src/libnr/nr-rect.h b/src/libnr/nr-rect.h index e0b3f30ae..91431d0e6 100644 --- a/src/libnr/nr-rect.h +++ b/src/libnr/nr-rect.h @@ -181,10 +181,10 @@ public: } /** Returns the set of points shared by both rectangles. */ - static Maybe intersection(Rect const &a, Rect const &b); + static Maybe intersection(Maybe const &a, Maybe const &b); /** Returns the smallest rectangle that encloses both rectangles. */ - static Rect union_bounds(Rect const &a, Rect const &b); + static Maybe union_bounds(Maybe const &a, Maybe const &b); /** Scales the rect by s, with origin at 0, 0 */ inline Rect operator*(double const s) const { -- 2.30.2