2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_UI_RECT
18 #define ANDROID_UI_RECT
20 #include <utils/TypeHelpers.h>
23 #include <android/rect.h>
27 class Rect : public ARect
30 typedef int32_t value_type;
32 // we don't provide copy-ctor and operator= on purpose
33 // because we want the compiler generated versions
37 inline Rect(int32_t w, int32_t h) {
38 left = top = 0; right = w; bottom = h;
40 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
41 left = l; top = t; right = r; bottom = b;
43 inline Rect(const Point& lt, const Point& rb) {
44 left = lt.x; top = lt.y; right = rb.x; bottom = rb.y;
50 left = top = right = bottom = 0;
53 // a valid rectangle has a non negative width and height
54 inline bool isValid() const {
55 return (width()>=0) && (height()>=0);
58 // an empty rect has a zero width or height, or is invalid
59 inline bool isEmpty() const {
60 return (width()<=0) || (height()<=0);
63 inline void set(const Rect& rhs) {
68 inline int32_t width() const {
73 inline int32_t height() const {
77 void setLeftTop(const Point& lt) {
82 void setRightBottom(const Point& rb) {
87 // the following 4 functions return the 4 corners of the rect as Point
88 Point leftTop() const {
89 return Point(left, top);
91 Point rightBottom() const {
92 return Point(right, bottom);
94 Point rightTop() const {
95 return Point(right, top);
97 Point leftBottom() const {
98 return Point(left, bottom);
102 inline bool operator == (const Rect& rhs) const {
103 return (left == rhs.left) && (top == rhs.top) &&
104 (right == rhs.right) && (bottom == rhs.bottom);
107 inline bool operator != (const Rect& rhs) const {
108 return !operator == (rhs);
111 // operator < defines an order which allows to use rectangles in sorted
113 bool operator < (const Rect& rhs) const;
115 Rect& offsetToOrigin() {
121 Rect& offsetTo(const Point& p) {
122 return offsetTo(p.x, p.y);
124 Rect& offsetBy(const Point& dp) {
125 return offsetBy(dp.x, dp.y);
127 Rect& operator += (const Point& rhs) {
128 return offsetBy(rhs.x, rhs.y);
130 Rect& operator -= (const Point& rhs) {
131 return offsetBy(-rhs.x, -rhs.y);
133 const Rect operator + (const Point& rhs) const;
134 const Rect operator - (const Point& rhs) const;
136 void translate(int32_t dx, int32_t dy) { // legacy, don't use.
140 Rect& offsetTo(int32_t x, int32_t y);
141 Rect& offsetBy(int32_t x, int32_t y);
142 bool intersect(const Rect& with, Rect* result) const;
145 ANDROID_BASIC_TYPES_TRAITS(Rect)
147 }; // namespace android
149 #endif // ANDROID_UI_RECT