Add a point constrain method to region

Force a point to not exceed the region boundaries.
This commit is contained in:
Danny Robson 2013-08-05 16:38:05 +10:00
parent ff55c368cf
commit f256f4d0e9
2 changed files with 11 additions and 2 deletions

View File

@ -136,9 +136,17 @@ region<T>::overlaps (const region<T> &rhs) const {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T>
void
region<T>::constrain (point2 &p) const {
p.x = std::min (std::max (static_cast<T> (p.x), x), x + w);
p.y = std::min (std::max (static_cast<T> (p.y), y), y + h);
}
template <typename T> template <typename T>
point2 point2
region<T>::constrain (const point2 &p) const { region<T>::constrained (const point2 &p) const {
point2 v; point2 v;
v.x = std::min (std::max (static_cast<T> (p.x), x), x + w); v.x = std::min (std::max (static_cast<T> (p.x), x), x + w);
v.y = std::min (std::max (static_cast<T> (p.y), y), y + h); v.y = std::min (std::max (static_cast<T> (p.y), y), y + h);

View File

@ -57,7 +57,8 @@ namespace util {
bool contains (const point2&) const; // exclusive of borders bool contains (const point2&) const; // exclusive of borders
bool overlaps (const region<T>&) const; // exclusive of borders bool overlaps (const region<T>&) const; // exclusive of borders
point2 constrain (const point2&) const; void constrain (point2&) const;
point2 constrained (const point2&) const;
region overlap (const region<T>&) const; region overlap (const region<T>&) const;