Add constrain operation for points in regions

This commit is contained in:
Danny Robson 2013-07-30 23:52:09 +10:00
parent 847242a59f
commit 1f37a8d04c
2 changed files with 17 additions and 4 deletions

View File

@ -135,6 +135,17 @@ region<T>::overlaps (const region<T> &rhs) const {
} }
//-----------------------------------------------------------------------------
template <typename T>
point2
region<T>::constrain (const point2 &p) const {
point2 v;
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);
return v;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template<typename T> template<typename T>
region<T> region<T>

View File

@ -50,13 +50,15 @@ namespace util {
bool empty (void) const; bool empty (void) const;
point<2> base (void) const; point2 base (void) const;
point<2> centre (void) const; point2 centre (void) const;
bool includes (const point<2>&) const; // inclusive of borders bool includes (const point2&) const; // inclusive of borders
bool contains (const point<2>&) 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;
region overlap (const region<T>&) const; region overlap (const region<T>&) const;
bool operator ==(const region<T>& rhs) const; bool operator ==(const region<T>& rhs) const;