summaryrefslogtreecommitdiff
path: root/src/rect.cpp
blob: f024495dc15f26889b3fbedbd022bda218d9bb26 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright © 2008 Dennis Kasprzyk
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Dennis Kasprzyk not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Dennis Kasprzyk makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * DENNIS KASPRZYK DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL DENNIS KASPRZYK BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors: Dennis Kasprzyk <onestone@compiz-fusion.org>
 */

#include <core/core.h>
#include <core/rect.h>

CompRect::CompRect ()
{
    mRegion.rects = &mRegion.extents;
    mRegion.numRects = 1;
    mRegion.extents.x1 = 0;
    mRegion.extents.x2 = 0;
    mRegion.extents.y1 = 0;
    mRegion.extents.y2 = 0;
}

CompRect::CompRect (int x, int y, int width, int height)
{
    mRegion.rects = &mRegion.extents;
    mRegion.numRects = 1;
    mRegion.extents.x1 = x;
    mRegion.extents.y1 = y;
    mRegion.extents.x2 = x + width;
    mRegion.extents.y2 = y + height;
}

CompRect::CompRect (const CompRect& r)
{
    mRegion = r.mRegion;
    mRegion.rects = &mRegion.extents;
}

CompRect::CompRect (const XRectangle xr)
{
    mRegion.rects = &mRegion.extents;
    mRegion.numRects = 1;
    mRegion.extents.x1 = xr.x;
    mRegion.extents.y1 = xr.y;
    mRegion.extents.x2 = xr.x + xr.width;
    mRegion.extents.y2 = xr.y + xr.height;
}

const Region
CompRect::region () const
{
    return const_cast<const Region> (&mRegion);
}

void
CompRect::setGeometry (int x,
		       int y,
		       int width,
		       int height)
{
    mRegion.extents.x1 = x;
    mRegion.extents.y1 = y;
    mRegion.extents.x2 = x + width;
    mRegion.extents.y2 = y + height;
}

void
CompRect::setX (int x)
{
    int width = mRegion.extents.x2 - mRegion.extents.x1;

    mRegion.extents.x1 = x;
    mRegion.extents.x2 = x + width;
}

void
CompRect::setY (int y)
{
    int height = mRegion.extents.y2 - mRegion.extents.y1;

    mRegion.extents.y1 = y;
    mRegion.extents.y2 = y + height;
}

void
CompRect::setPos (const CompPoint& pos)
{
    setX (pos.x ());
    setY (pos.y ());
}

void
CompRect::setWidth (int width)
{
    mRegion.extents.x2 = mRegion.extents.x1 + width;
}

void
CompRect::setHeight (int height)
{
    mRegion.extents.y2 = mRegion.extents.y1 + height;
}

void
CompRect::setSize (const CompSize& size)
{
    mRegion.extents.x2 = mRegion.extents.x1 + size.width ();
    mRegion.extents.y2 = mRegion.extents.y1 + size.height ();
}

void
CompRect::setLeft (int x1)
{
    mRegion.extents.x1 = x1;
    if (mRegion.extents.x2 < x1)
	mRegion.extents.x2 = x1;
}

void
CompRect::setTop (int y1)
{
    mRegion.extents.y1 = y1;
    if (mRegion.extents.y2 < y1)
	mRegion.extents.y2 = y1;
}

void
CompRect::setRight (int x2)
{
    mRegion.extents.x2 = x2;
    if (mRegion.extents.x1 > x2)
	mRegion.extents.x1 = x2;
}

void
CompRect::setBottom (int y2)
{
    mRegion.extents.y2 = y2;
    if (mRegion.extents.y1 > y2)
	mRegion.extents.y1 = y2;
}

bool
CompRect::contains (const CompPoint& point) const
{
    if (point.x () < x1 ())
	return false;
    if (point.x () > x2 ())
	return false;
    if (point.y () < y1 ())
	return false;
    if (point.y () > y2 ())
	return false;

    return true;
}

bool
CompRect::contains (const CompRect& rect) const
{
    if (rect.x1 () < x1 ())
	return false;
    if (rect.x2 () > x2 ())
	return false;
    if (rect.y1 () < y1 ())
	return false;
    if (rect.y2 () > y2 ())
	return false;

    return true;
}

bool
CompRect::intersects (const CompRect& rect) const
{
    int l, r, t, b;

    /* extents of overlapping rectangle */
    l = MAX (left (), rect.left ());
    r = MIN (right (), rect.right ());
    t = MAX (top (), rect.top ());
    b = MIN (bottom (), rect.bottom ());

    return (l < r) && (t < b);
}

bool
CompRect::isEmpty () const
{
    if (mRegion.extents.x1 != mRegion.extents.x2)
	return false;
    if (mRegion.extents.y1 != mRegion.extents.y2)
	return false;

    return true;
}

int
CompRect::area () const
{
    if (mRegion.extents.x2 < mRegion.extents.x1)
	return 0;
    if (mRegion.extents.y2 < mRegion.extents.y1)
	return 0;

    return (mRegion.extents.x2 - mRegion.extents.x1) *
	   (mRegion.extents.y2 - mRegion.extents.y1);
}

bool
CompRect::operator== (const CompRect &rect) const
{
    if (mRegion.extents.x1 != rect.mRegion.extents.x1)
	return false;
    if (mRegion.extents.y1 != rect.mRegion.extents.y1)
	return false;
    if (mRegion.extents.x2 != rect.mRegion.extents.x2)
	return false;
    if (mRegion.extents.y2 != rect.mRegion.extents.y2)
	return false;

    return true;
}

bool
CompRect::operator!= (const CompRect &rect) const
{
    return !(*this == rect);
}

CompRect
CompRect::operator& (const CompRect &rect) const
{
    CompRect result (*this);

    result &= rect;
    return result;
}

CompRect&
CompRect::operator&= (const CompRect &rect)
{
    int l, r, t, b;

    /* extents of overlapping rectangle */
    l = MAX (left (), rect.left ());
    r = MIN (right (), rect.right ());
    t = MAX (top (), rect.top ());
    b = MIN (bottom (), rect.bottom ());

    mRegion.extents.x1 = l;
    mRegion.extents.x2 = r;
    mRegion.extents.y1 = t;
    mRegion.extents.y2 = b;

    return *this;
}

CompRect &
CompRect::operator= (const CompRect &rect)
{
    mRegion.extents.x1 = rect.mRegion.extents.x1;
    mRegion.extents.y1 = rect.mRegion.extents.y1;
    mRegion.extents.x2 = rect.mRegion.extents.x2;
    mRegion.extents.y2 = rect.mRegion.extents.y2;

    return *this;
}