-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rect.h
181 lines (155 loc) · 4.8 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (C) 2008,2009,2010,2011,2012 GlavSoft LLC.
// All rights reserved.
//
//-------------------------------------------------------------------------
// This file is part of the TightVNC software. Please visit our Web site:
//
#ifndef __RECT_H__
#define __RECT_H__
//#include "util/CommonHeader.h"
#include <windows.h>
#include <vector>
class Rect
{
public:
Rect(void) : left(0), top(0), right(0), bottom(0) {}
Rect(const Rect *rect) { setRect(rect); }
Rect(const Rect &rect) { setRect(&rect); }
Rect(int lt, int tp, int rt, int bm) { setRect(lt, tp, rt, bm); }
Rect(int width, int height) { setRect(0, 0, width, height); }
Rect(const RECT *rect) {fromWindowsRect(rect);}
virtual ~Rect(void) {}
int left;
int top;
int right;
int bottom;
inline void setRect(int lt, int tp, int rt, int bm)
{
left = lt;
top = tp;
right = rt;
bottom = bm;
}
inline void setRect(const Rect *rect)
{
left = rect->left;
top = rect->top;
right = rect->right;
bottom = rect->bottom;
}
inline Rect & operator=(const Rect &rect)
{
setRect(&rect);
return *this;
}
inline bool isValid() const
{
if (right < left || bottom < top) {
return false;
}
return true;
}
inline bool isPointInRect(int pointX, int pointY) {
if (pointX < left || pointX >= right) {
return false;
}
if (pointY < top || pointY >= bottom) {
return false;
}
return true;
}
// Return true this Rect fully contain the inner Rect, otherwise return false.
inline bool isFullyContainRect(const Rect *inner) {
if (inner->left < left || inner->top < top ||
inner->right > right || inner->bottom > bottom) {
return false;
} else {
return true;
}
}
// Convert to windows RECT
inline RECT toWindowsRect() const {
RECT winRect;
winRect.left = left;
winRect.top = top;
winRect.right = right;
winRect.bottom = bottom;
return winRect; } const
// Load from windows RECT
inline void fromWindowsRect(const RECT *winRect)
{
left = winRect->left;
top = winRect->top;
right = winRect->right;
bottom = winRect->bottom;
}
inline void move(int offsetX, int offsetY)
{
left += offsetX;
right += offsetX;
top += offsetY;
bottom += offsetY;
}
inline void setLocation(int destX, int destY)
{
int offsetX = destX - left;
int offsetY = destY - top;
left = destX;
right += offsetX;
top = destY;
bottom += offsetY;
}
inline bool isEqualTo(const Rect *rect) const { return rect->left == left &&
rect->top == top &&
rect->right == right &&
rect->bottom == bottom; }
inline void setWidth(int value) { right = left + value; }
inline void setHeight(int value) { bottom = top + value; }
inline int getWidth() const { return right - left; }
inline int getHeight() const { return bottom - top; }
void rotateOn90InsideDimension(int dimHeight)
{
Rect localCopy(this);
setWidth(localCopy.getHeight());
setHeight(localCopy.getWidth());
int newLeft = dimHeight - localCopy.top - localCopy.getHeight();
int newTop = localCopy.left;
setLocation(newLeft, newTop);
}
void rotateOn180InsideDimension(int dimWidth, int dimHeight)
{
int newLeft = dimWidth - left - getWidth();
int newTop = dimHeight - top - getHeight();
setLocation(newLeft, newTop);
}
void rotateOn270InsideDimension(int dimWidth)
{
Rect localCopy(this);
setWidth(localCopy.getHeight());
setHeight(localCopy.getWidth());
int newLeft = localCopy.top;
int newTop = dimWidth - localCopy.left - localCopy.getWidth();
setLocation(newLeft, newTop);
}
inline bool isEmpty() const { return getWidth() <= 0 || getHeight() <= 0; }
inline int area() const { return isEmpty() ? 0 : getWidth() * getHeight(); }
static int totalArea(std::vector<Rect> &v) {
int area = 0;
for (std::vector<Rect>::iterator i = v.begin(); i < v.end(); i++)
area += i->area();
return area;
}
inline void clear() { left = top = right = bottom = 0; }
Rect intersection(const Rect *other) const {
Rect result;
result.setRect((left > other->left) ? left : other->left,
(top > other->top) ? top : other->top,
(right < other->right) ? right : other->right,
(bottom < other->bottom) ? bottom : other->bottom);
if (!result.isValid()) {
result.clear();
}
return result;
}
};
#endif // __RECT_H__