도형에 대한 point, rect의 템플릿이 있으면 편할거라는 생각에 만들어 보았다.
Windows에서도 이미 RECT, POINT 등이 있어서 구분을 위해
namespace 는 Figure Primitive 약자로 FP라고 정했다.
// FigurePrimitive.h #if !defined(__FIGURE_PRIMITIVE__H__) #define __FIGURE_PRIMITIVE__H__ namespace FP { template<typename _T> struct RECT{ _T left; _T top; _T right; _T bottom; }; template<typename _T> struct POINT{ _T x; _T y; }; template<typename _T> class Point : public POINT<_T> { public: Point() { x = static_cast<_T>(0); y = static_cast<_T>(0); } Point(_T x, _T y) { this->x = x; this->y = y; } virtual ~Point() {} void Set(_T x, _T y) { this->x = x; this->y = y; } Point<_T>& Offset(_T x, _T y) { this->x += x; this->y += y; } }; template<typename _T> class Rect : public RECT<_T> { public: Rect() { _T init = static_cast<_T>(0); left = right = top = bottom = init; } Rect(_T left, _T top, _T right, _T bottom) { Set(left, top, right, bottom); } virtual ~Rect() {} void Set(_T left, _T top, _T right, _T bottom) { this->left = left; this->top = top; this->right = right; this->bottom = bottom; } Rect<_T>& Offset(_T x, _T y) { left += x; right += x; top += y; bottom += y; } _T Width() { return right - left; } _T Height() { return bottom - top; } bool PtInRect(const Point<_T>& pt) { return ((pt.x >= left && pt.x <= right) && (pt.y >= top && pt.y <= bottom)); } bool RectInRect(const Rect<_T>& rc) { return ((left <= rc.left) && (right >= rc.right) && (top <= rc.top) && (bottom >= rc.bottom)); } Point<_T> Center(void) { return Point<_T>(static_cast<_T>(left + Width() / 2.0), static_cast<_T>(top + Height() / 2.0)); } Point<_T> LeftTop(void) { return Point(left, top); } Point<_T> RightTop(void) { return Point(right, top); } Point<_T> LeftBottom(void) { return Point(left, bottom); } Point<_T> RightBottom(void) { return Point(right, bottom); } Rect<_T> IntersectRect(const Rect<_T>& rc) const { _T l = this->left > rc.left ? this->left : rc.left; _T r = this->right < rc.right ? this->right : rc.right; _T t = this->top > rc.top ? this->top : rc.top; _T b = this->bottom < rc.bottom ? this->bottom : rc.bottom; if (l > r) l = r = 0; if (t > b) t = b = 0; return Rect<_T>(l, t, r, b); } _T Area(void) const { return Width() * Height(); } bool operator == (const RECT<_T>& rc) { return (this->left == rc.left && this->top == rc.top && this->right == rc.right && this->bottom && rc.bottom); } bool operator != (const RECT<_T>& rc) { return !(this->left == rc.left && this->top == rc.top && this->right == rc.right && this->bottom && rc.bottom); } }; } #endif // __FIGURE_PRIMITIVE__H__
'C,C++' 카테고리의 다른 글
STL ifstream read text file (0) | 2017.04.17 |
---|---|
메모리 누수 (클래스 상속) (0) | 2016.04.28 |