From 1f37a8d04c29db858297975ab1d887f6ba7f903e Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 30 Jul 2013 23:52:09 +1000 Subject: [PATCH] Add constrain operation for points in regions --- region.cpp | 11 +++++++++++ region.hpp | 10 ++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/region.cpp b/region.cpp index 38008f67..b9a70ce0 100644 --- a/region.cpp +++ b/region.cpp @@ -135,6 +135,17 @@ region::overlaps (const region &rhs) const { } +//----------------------------------------------------------------------------- +template +point2 +region::constrain (const point2 &p) const { + point2 v; + v.x = std::min (std::max (static_cast (p.x), x), x + w); + v.y = std::min (std::max (static_cast (p.y), y), y + h); + + return v; +} + //----------------------------------------------------------------------------- template region diff --git a/region.hpp b/region.hpp index 73cd3011..0036255a 100644 --- a/region.hpp +++ b/region.hpp @@ -50,13 +50,15 @@ namespace util { bool empty (void) const; - point<2> base (void) const; - point<2> centre (void) const; + point2 base (void) const; + point2 centre (void) const; - bool includes (const point<2>&) const; // inclusive of borders - bool contains (const point<2>&) const; // exclusive of borders + bool includes (const point2&) const; // inclusive of borders + bool contains (const point2&) const; // exclusive of borders bool overlaps (const region&) const; // exclusive of borders + point2 constrain (const point2&) const; + region overlap (const region&) const; bool operator ==(const region& rhs) const;