Rect.h revision 079ba2c85b15e882629b8d188f5fbdb42f7f8eea
1/*
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_UI_RECT_H
18#define ANDROID_UI_RECT_H
19
20#include <utils/Log.h>
21
22namespace android {
23namespace uirenderer {
24
25///////////////////////////////////////////////////////////////////////////////
26// Structs
27///////////////////////////////////////////////////////////////////////////////
28
29struct Rect {
30    float left;
31    float top;
32    float right;
33    float bottom;
34
35    Rect():
36            left(0),
37            top(0),
38            right(0),
39            bottom(0) {
40    }
41
42    Rect(float left, float top, float right, float bottom):
43            left(left),
44            top(top),
45            right(right),
46            bottom(bottom) {
47    }
48
49    Rect(const Rect& r) {
50        set(r);
51    }
52
53    Rect(Rect& r) {
54        set(r);
55    }
56
57    Rect& operator=(const Rect& r) {
58        set(r);
59        return *this;
60    }
61
62    Rect& operator=(Rect& r) {
63        set(r);
64        return *this;
65    }
66
67    friend int operator==(const Rect& a, const Rect& b) {
68        return !memcmp(&a, &b, sizeof(a));
69    }
70
71    friend int operator!=(const Rect& a, const Rect& b) {
72        return memcmp(&a, &b, sizeof(a));
73    }
74
75    bool isEmpty() const {
76        return left >= right || top >= bottom;
77    }
78
79    void setEmpty() {
80        memset(this, 0, sizeof(*this));
81    }
82
83    void set(float left, float top, float right, float bottom) {
84        this->left = left;
85        this->right = right;
86        this->top = top;
87        this->bottom = bottom;
88    }
89
90    void set(const Rect& r) {
91        set(r.left, r.top, r.right, r.bottom);
92    }
93
94    float getWidth() const {
95        return right - left;
96    }
97
98    float getHeight() const {
99        return bottom - top;
100    }
101
102    bool intersects(float left, float top, float right, float bottom) const {
103        return left < right && top < bottom &&
104                this->left < this->right && this->top < this->bottom &&
105                this->left < right && left < this->right &&
106                this->top < bottom && top < this->bottom;
107    }
108
109    bool intersects(const Rect& r) const {
110        return intersects(r.left, r.top, r.right, r.bottom);
111    }
112
113    bool intersect(float left, float top, float right, float bottom) {
114        if (left < right && top < bottom && !this->isEmpty() &&
115                this->left < right && left < this->right &&
116                this->top < bottom && top < this->bottom) {
117
118            if (this->left < left) this->left = left;
119            if (this->top < top) this->top = top;
120            if (this->right > right) this->right = right;
121            if (this->bottom > bottom) this->bottom = bottom;
122
123            return true;
124        }
125        return false;
126    }
127
128    bool intersect(const Rect& r) {
129        return intersect(r.left, r.top, r.right, r.bottom);
130    }
131
132    bool unionWith(const Rect& r) {
133        if (r.left < r.right && r.top < r.bottom) {
134            if (left < right && top < bottom) {
135                if (left > r.left) left = r.left;
136                if (top > r.top) top = r.top;
137                if (right < r.right) right = r.right;
138                if (bottom < r.bottom) bottom = r.bottom;
139                return true;
140            } else {
141                left = r.left;
142                top = r.top;
143                right = r.right;
144                bottom = r.bottom;
145                return true;
146            }
147        }
148        return false;
149    }
150
151    void dump() const {
152        LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
153    }
154
155}; // struct Rect
156
157}; // namespace uirenderer
158}; // namespace android
159
160#endif // ANDROID_RECT_H
161