Rect.h revision 4b8160fdfd3281d1d66c0cae9bb9c647b4333491
1/*
2 * Copyright (C) 2006 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
18#define ANDROID_UI_RECT
19
20#include <utils/TypeHelpers.h>
21#include <ui/Point.h>
22
23namespace android {
24
25class Rect
26{
27public:
28    int left;
29    int top;
30    int right;
31    int bottom;
32
33    typedef int value_type;
34
35    // we don't provide copy-ctor and operator= on purpose
36    // because we want the compiler generated versions
37
38    inline Rect() {
39    }
40    inline Rect(int w, int h)
41        : left(0), top(0), right(w), bottom(h) {
42    }
43    inline Rect(int l, int t, int r, int b)
44        : left(l), top(t), right(r), bottom(b) {
45    }
46    inline Rect(const Point& lt, const Point& rb)
47        : left(lt.x), top(lt.y), right(rb.x), bottom(rb.y) {
48    }
49
50    void makeInvalid();
51
52    inline void clear() {
53        left = top = right = bottom = 0;
54    }
55
56    // a valid rectangle has a non negative width and height
57    inline bool isValid() const {
58        return (width()>=0) && (height()>=0);
59    }
60
61    // an empty rect has a zero width or height, or is invalid
62    inline bool isEmpty() const {
63        return (width()<=0) || (height()<=0);
64    }
65
66    inline void set(const Rect& rhs) {
67        operator = (rhs);
68    }
69
70    // rectangle's width
71    inline int width() const {
72        return right-left;
73    }
74
75    // rectangle's height
76    inline int height() const {
77        return bottom-top;
78    }
79
80    void setLeftTop(const Point& lt) {
81        left = lt.x;
82        top  = lt.y;
83    }
84
85    void setRightBottom(const Point& rb) {
86        right = rb.x;
87        bottom  = rb.y;
88    }
89
90    // the following 4 functions return the 4 corners of the rect as Point
91    Point leftTop() const {
92        return Point(left, top);
93    }
94    Point rightBottom() const {
95        return Point(right, bottom);
96    }
97    Point rightTop() const {
98        return Point(right, top);
99    }
100    Point leftBottom() const {
101        return Point(left, bottom);
102    }
103
104    // comparisons
105    inline bool operator == (const Rect& rhs) const {
106        return (left == rhs.left) && (top == rhs.top) &&
107               (right == rhs.right) && (bottom == rhs.bottom);
108    }
109
110    inline bool operator != (const Rect& rhs) const {
111        return !operator == (rhs);
112    }
113
114    // operator < defines an order which allows to use rectangles in sorted
115    // vectors.
116    bool operator < (const Rect& rhs) const;
117
118    Rect& offsetToOrigin() {
119        right -= left;
120        bottom -= top;
121        left = top = 0;
122        return *this;
123    }
124    Rect& offsetTo(const Point& p) {
125        return offsetTo(p.x, p.y);
126    }
127    Rect& offsetBy(const Point& dp) {
128        return offsetBy(dp.x, dp.y);
129    }
130    Rect& operator += (const Point& rhs) {
131        return offsetBy(rhs.x, rhs.y);
132    }
133    Rect& operator -= (const Point& rhs) {
134        return offsetBy(-rhs.x, -rhs.y);
135    }
136    const Rect operator + (const Point& rhs) const;
137    const Rect operator - (const Point& rhs) const;
138
139    void translate(int dx, int dy) { // legacy, don't use.
140        offsetBy(dx, dy);
141    }
142
143    Rect&   offsetTo(int x, int y);
144    Rect&   offsetBy(int x, int y);
145    bool    intersect(const Rect& with, Rect* result) const;
146};
147
148ANDROID_BASIC_TYPES_TRAITS(Rect)
149
150}; // namespace android
151
152#endif // ANDROID_UI_RECT
153