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