51 lines
		
	
	
		
			916 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			916 B
		
	
	
	
		
			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)
 | |
| 		{
 | |
| 			bool containsX = (x <= px && px <= x + w);
 | |
| 			bool containsY = (y <= px && px <= x + w);
 | |
| 
 | |
| 			return containsX && containsY;
 | |
| 		}
 | |
| 
 | |
| 		bool intersects(const Rect<T> other)
 | |
| 		{
 | |
| 			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;
 | |
| 		}
 | |
| 
 | |
| 		// Values
 | |
| 		T x, y, w, h;
 | |
| 	};
 | |
| 
 | |
| 	typedef Rect<float> RectF;
 | |
| 
 | |
| } /* namespace utils */
 | |
| } /* namespace farmlands */
 | |
| 
 | |
| #endif /* UTILS_RECT_H_ */
 |