blob: 724bd1a2b7423d7a0b7fe9e64607af8d66b54664 [file] [log] [blame]
Romain Guybb9524b2010-06-22 18:56:38 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_RECT_H
18#define ANDROID_RECT_H
19
20namespace android {
21
22///////////////////////////////////////////////////////////////////////////////
23// Structs
24///////////////////////////////////////////////////////////////////////////////
25
26struct Rect {
27 float left;
28 float top;
29 float right;
30 float bottom;
31
32 Rect(): left(0), top(0), right(0), bottom(0) { }
33
34 Rect(const Rect& r) {
35 set(r);
36 }
37
38 Rect(Rect& r) {
39 set(r);
40 }
41
42 Rect& operator=(const Rect& r) {
43 set(r);
44 return *this;
45 }
46
47 Rect& operator=(Rect& r) {
48 set(r);
49 return *this;
50 }
51
52 friend int operator==(const Rect& a, const Rect& b) {
53 return !memcmp(&a, &b, sizeof(a));
54 }
55
56 friend int operator!=(const Rect& a, const Rect& b) {
57 return memcmp(&a, &b, sizeof(a));
58 }
59
60 bool isEmpty() const {
61 return left >= right || top >= bottom;
62 }
63
64 void setEmpty() {
65 memset(this, 0, sizeof(*this));
66 }
67
68 void set(float left, float top, float right, float bottom) {
69 this->left = left;
70 this->right = right;
71 this->top = top;
72 this->bottom = bottom;
73 }
74
75 void set(const Rect& r) {
76 set(r.left, r.top, r.right, r.bottom);
77 }
78
79 float getWidth() const {
80 return right - left;
81 }
82
83 float getHeight() const {
84 return bottom - top;
85 }
86
87 bool intersects(float left, float top, float right, float bottom) const {
88 return left < right && top < bottom &&
89 this->left < this->right && this->top < this->bottom &&
90 this->left < right && left < this->right &&
91 this->top < bottom && top < this->bottom;
92 }
93
94 bool intersects(const Rect& r) const {
95 return intersects(r.left, r.top, r.right, r.bottom);
96 }
97
98 bool intersect(float left, float top, float right, float bottom) {
99 if (left < right && top < bottom && !this->isEmpty() &&
100 this->left < right && left < this->right &&
101 this->top < bottom && top < this->bottom) {
102
103 if (this->left < left) this->left = left;
104 if (this->top < top) this->top = top;
105 if (this->right > right) this->right = right;
106 if (this->bottom > bottom) this->bottom = bottom;
107
108 return true;
109 }
110 return false;
111 }
112
113 bool intersect(const Rect& r) {
114 return intersect(r.left, r.top, r.right, r.bottom);
115 }
116
117 void dump() const {
118 LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
119 }
120
121}; // struct Rect
122
123}; // namespace android
124
125#endif // ANDROID_RECT_H