blob: 66b95762d050fb81746813a87eac121af411e7d6 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopian35801ce2009-05-26 17:44:57 -07002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
Mathias Agopian35801ce2009-05-26 17:44:57 -07004 * 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08007 *
Mathias Agopian35801ce2009-05-26 17:44:57 -07008 * 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.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080015 */
16
17#include <ui/Rect.h>
18
19namespace android {
20
Mathias Agopian35801ce2009-05-26 17:44:57 -070021static inline int min(int a, int b) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022 return (a<b) ? a : b;
23}
24
Mathias Agopian35801ce2009-05-26 17:44:57 -070025static inline int max(int a, int b) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026 return (a>b) ? a : b;
27}
28
29void Rect::makeInvalid() {
30 left = 0;
31 top = 0;
32 right = -1;
33 bottom = -1;
34}
35
36bool Rect::operator < (const Rect& rhs) const
37{
38 if (top<rhs.top) {
39 return true;
40 } else if (top == rhs.top) {
41 if (left < rhs.left) {
42 return true;
43 } else if (left == rhs.left) {
44 if (bottom<rhs.bottom) {
45 return true;
46 } else if (bottom == rhs.bottom) {
47 if (right<rhs.right) {
48 return true;
49 }
50 }
51 }
52 }
53 return false;
54}
55
56Rect& Rect::offsetTo(int x, int y)
57{
58 right -= left - x;
59 bottom -= top - y;
60 left = x;
61 top = y;
62 return *this;
63}
64
65Rect& Rect::offsetBy(int x, int y)
66{
67 left += x;
68 top += y;
69 right+= x;
70 bottom+=y;
71 return *this;
72}
73
Mathias Agopian35801ce2009-05-26 17:44:57 -070074const Rect Rect::operator + (const Point& rhs) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075{
Mathias Agopian35801ce2009-05-26 17:44:57 -070076 const Rect result(left+rhs.x, top+rhs.y, right+rhs.x, bottom+rhs.y);
77 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078}
79
Mathias Agopian35801ce2009-05-26 17:44:57 -070080const Rect Rect::operator - (const Point& rhs) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081{
Mathias Agopian35801ce2009-05-26 17:44:57 -070082 const Rect result(left-rhs.x, top-rhs.y, right-rhs.x, bottom-rhs.y);
83 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084}
85
86bool Rect::intersect(const Rect& with, Rect* result) const
87{
88 result->left = max(left, with.left);
89 result->top = max(top, with.top);
90 result->right = min(right, with.right);
91 result->bottom = min(bottom, with.bottom);
92 return !(result->isEmpty());
93}
94
95}; // namespace android