From f256f4d0e9babb6afd1a5ee3405e70da02c2e9e7 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 5 Aug 2013 16:38:05 +1000 Subject: [PATCH] Add a point constrain method to region Force a point to not exceed the region boundaries. --- region.cpp | 10 +++++++++- region.hpp | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/region.cpp b/region.cpp index b9a70ce0..1ce9f143 100644 --- a/region.cpp +++ b/region.cpp @@ -136,9 +136,17 @@ region::overlaps (const region &rhs) const { //----------------------------------------------------------------------------- +template +void +region::constrain (point2 &p) const { + p.x = std::min (std::max (static_cast (p.x), x), x + w); + p.y = std::min (std::max (static_cast (p.y), y), y + h); +} + + template point2 -region::constrain (const point2 &p) const { +region::constrained (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); diff --git a/region.hpp b/region.hpp index 0036255a..905969a5 100644 --- a/region.hpp +++ b/region.hpp @@ -57,7 +57,8 @@ namespace util { bool contains (const point2&) const; // exclusive of borders bool overlaps (const region&) const; // exclusive of borders - point2 constrain (const point2&) const; + void constrain (point2&) const; + point2 constrained (const point2&) const; region overlap (const region&) const;