61 lines
1.1 KiB
C++

/*
* Rect.h
*
* Created on: Dec 4, 2016
* Author: tibi
*/
#ifndef UTILS_RECT_H_
#define UTILS_RECT_H_
namespace farmlands {
namespace utils {
template <typename T>
class Rect
{
public:
// Constructors
Rect()
: x(), y(), w(), h() { }
Rect(T x, T y, T w, T h)
: x(x), y(y), w(w), h(h) { }
bool contains(T px, T py) const
{
bool containsX = (x <= px && px <= x + w);
bool containsY = (y <= py && py <= y + h);
return containsX && containsY;
}
bool intersects(const Rect<T>& other) const
{
bool intersectsX = (x <= other.x && other.x <= x + w) || (other.x <= x && other.x + other.w >= x);
bool intersectsY = (y <= other.y && other.y <= y + h) || (other.y <= y && other.y + other.h >= y);
return intersectsX && intersectsY;
}
bool operator==(const Rect<T>& other) const
{
return (x == other.x) && (y == other.y) && (w == other.w) && (h == other.h);
}
bool operator!=(const Rect<T>& other) const
{
return !operator==(other);
}
// Values
T x, y, w, h;
};
typedef Rect<float> RectF;
} /* namespace utils */
} /* namespace farmlands */
#endif /* UTILS_RECT_H_ */