-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrect.h
144 lines (123 loc) · 3.75 KB
/
rect.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef __AOI_RECT_H__
#define __AOI_RECT_H__
#include "point.h"
namespace aoi
{
enum EQuadrant
{
UnknowQuadrant = 0,
RightTop = 1, // Top right: quadrant 1
LeftTop = 2, // Top left: quadrant 2
LeftDown = 3, // Bottom left: Quadrant 3
RightDown = 4, // Bottom right: Quadrant 4
};
class Rect
{
public:
Rect() : mLeft(0), mRight(0), mTop(0), mBottom(0), mMidX(0), mMidY(0) { }
Rect(float left, float right, float bottom, float top)
: mLeft(left)
, mRight(right)
, mTop(top)
, mBottom(bottom)
, mMidX(left + (right - left) / 2)
, mMidY(bottom + (top - bottom) / 2)
{
}
Rect(const Rect& other)
: mLeft(other.mLeft)
, mRight(other.mRight)
, mTop(other.mTop)
, mBottom(other.mBottom)
, mMidX(other.mMidX)
, mMidY(other.mMidY)
{
}
Rect(Point center, Size halfExtents)
: mLeft(center.X - halfExtents.X)
, mRight(center.X + halfExtents.X)
, mTop(center.Y + halfExtents.Y)
, mBottom(center.Y - halfExtents.Y)
, mMidX(mLeft + (mRight - mLeft) / 2)
, mMidY(mBottom + (mTop - mBottom) / 2)
{
}
inline void Reset()
{
mLeft = 0;
mRight = 0;
mTop = 0;
mBottom = 0;
mMidX = 0;
mMidY = 0;
}
inline void Reset(float left, float right, float bottom, float top)
{
mLeft = left;
mRight = right;
mTop = top;
mBottom = bottom;
mMidX = left + (right - left) / 2;
mMidY = bottom + (top - bottom) / 2;
}
inline float Left() const { return mLeft; }
inline float Right() const { return mRight; }
inline float Bottom() const { return mBottom; }
inline float Top() const { return mTop; }
inline float MidX() const { return mMidX; }
inline float MidY() const { return mMidY; }
inline bool Contains(const Rect& Rect) const
{
return (mLeft <= Rect.mLeft
&& mBottom <= Rect.mBottom
&& Rect.mRight <= mRight
&& Rect.mTop <= mTop);
}
inline bool Contains(float x, float y) const
{
return (x >= mLeft && x <= mRight
&& y >= mBottom && y <= mTop);
}
inline bool Contains(const Point& point) const
{
return Contains(point.X, point.Y);
}
inline bool Contains(const Point* point) const
{
return Contains(point->X, point->Y);
}
inline bool Intersects(const Rect& Rect) const
{
return !(mRight < Rect.mLeft
|| Rect.mRight < mLeft
|| mTop < Rect.mBottom
|| Rect.mTop < mBottom);
}
inline EQuadrant GetQuadrant(const Point& point)
{
return Contains(point) ? GetQuadrant2(point) : UnknowQuadrant;
}
inline EQuadrant GetQuadrant(const Point* point)
{
return GetQuadrant(*point);
}
inline EQuadrant GetQuadrant2(const Point& point)
{
return (point.Y >= mMidY
? (point.X >= mMidX ? RightTop : LeftTop)
: (point.X >= mMidX ? RightDown : LeftDown));
}
inline EQuadrant GetQuadrant2(const Point* point)
{
return GetQuadrant2(*point);
}
private:
float mLeft;
float mRight;
float mTop;
float mBottom;
float mMidX;
float mMidY;
};
}
#endif