Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
244 chris 1
#ifndef __TRectANGLE_H__
2
#define __TRectANGLE_H__
3
 
4
#include "Common.h"
5
#include "Point.h"
6
 
7
#include <list>
8
 
9
namespace Sexy
10
{
11
 
12
template<class _T> class TRect
13
{
14
public:
15
        _T mX;
16
        _T mY;
17
        _T mWidth;
18
        _T mHeight;
19
 
20
public:
21
        TRect(_T theX, _T theY, _T theWidth, _T theHeight) :
22
          mX(theX), mY(theY), mWidth(theWidth), mHeight(theHeight)
23
        {
24
        }
25
 
26
        TRect(const TRect<_T>& theTRect) :
27
          mX(theTRect.mX), mY(theTRect.mY), mWidth(theTRect.mWidth), mHeight(theTRect.mHeight)
28
        {
29
        }
30
 
31
        TRect() :
32
          mX(0), mY(0), mWidth(0), mHeight(0)
33
        {
34
        }
35
 
36
        bool                                    Intersects(const TRect<_T>& theTRect) const
37
        {
38
                return !((theTRect.mX + theTRect.mWidth <= mX) ||
39
                        (theTRect.mY + theTRect.mHeight <= mY) ||
40
                        (theTRect.mX >= mX + mWidth) ||
41
                        (theTRect.mY >= mY + mHeight));
42
        }
43
 
44
        TRect<_T>                               Intersection(const TRect<_T>& theTRect) const
45
        {
46
                _T x1 = max(mX, theTRect.mX);
47
                _T x2 = min(mX + mWidth, theTRect.mX + theTRect.mWidth);
48
                _T y1 = max(mY, theTRect.mY);
49
                _T y2 = min(mY + mHeight, theTRect.mY + theTRect.mHeight);
50
                if (((x2 - x1) < 0) || ((y2 - y1) < 0))
51
                        return TRect<_T>(0,0,0,0);
52
                else
53
                        return TRect<_T>(x1, y1, x2 - x1, y2 - y1);
54
        }
55
 
56
        TRect<_T>                               Union(const TRect<_T>& theTRect)       
57
        {
58
                _T x1 = min(mX, theTRect.mX);
59
                _T x2 = max(mX + mWidth, theTRect.mX + theTRect.mWidth);
60
                _T y1 = min(mY, theTRect.mY);
61
                _T y2 = max(mY + mHeight, theTRect.mY + theTRect.mHeight);
62
                        return TRect<_T>(x1, y1, x2 - x1, y2 - y1);
63
        }
64
 
65
        bool                                    Contains(_T theX, _T theY) const
66
        {
67
                return ((theX >= mX) && (theX < mX + mWidth) &&
68
                        (theY >= mY) && (theY < mY + mHeight));
69
        }
70
 
71
        bool                                    Contains(const TPoint<_T>& thePoint) const
72
        {
73
                return ((thePoint.mX >= mX) && (thePoint.mX < mX + mWidth) &&
74
                        (thePoint.mY >= mY) && (thePoint.mY < mY + mHeight));
75
        }
76
 
77
        void                                    Offset(_T theX, _T theY)
78
        {
79
                mX += theX;
80
                mY += theY;
81
        }
82
 
83
        void                                    Offset(const TPoint<_T>& thePoint)
84
        {
85
                mX += thePoint.mX;
86
                mY += thePoint.mY;
87
        }
88
 
89
        TRect                                   Inflate(_T theX, _T theY)
90
        {
91
                mX -= theX;
92
                mWidth += theX*2;
93
                mY -= theY;
94
                mHeight += theY*2;
95
 
96
                return *this;
97
        }
98
 
99
        bool operator==(const TRect<_T>& theRect) const
100
        {
101
                return (mX == theRect.mX) && (mY == theRect.mY) && (mWidth == theRect.mWidth) && (mHeight == theRect.mHeight);
102
        }
103
 
104
        RECT                                    ToRECT() const
105
        {
106
                RECT aRect = {mX, mY, mX + mWidth, mY + mHeight};
107
                return aRect;
108
        }
109
};
110
 
111
typedef TRect<int> Rect;
112
typedef TRect<double> FRect;
113
 
114
 
115
 
116
}
117
 
118
#endif //__TRectANGLE_H__