blob: 5694e000025db3790589dbc772c122c9d2aba4dc [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Mathias Agopianf1472a72009-05-26 17:44:57 -07002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
Mathias Agopianf1472a72009-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 Project9066cfe2009-03-03 19:31:44 -08007 *
Mathias Agopianf1472a72009-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 Project9066cfe2009-03-03 19:31:44 -080015 */
16
17#include <ui/Rect.h>
18
19namespace android {
20
Dianne Hackborn289b9b62010-07-09 11:44:11 -070021static inline int32_t min(int32_t a, int32_t b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022 return (a<b) ? a : b;
23}
24
Dianne Hackborn289b9b62010-07-09 11:44:11 -070025static inline int32_t max(int32_t a, int32_t b) {
The Android Open Source Project9066cfe2009-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
Dianne Hackborn289b9b62010-07-09 11:44:11 -070056Rect& Rect::offsetTo(int32_t x, int32_t y)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057{
58 right -= left - x;
59 bottom -= top - y;
60 left = x;
61 top = y;
62 return *this;
63}
64
Dianne Hackborn289b9b62010-07-09 11:44:11 -070065Rect& Rect::offsetBy(int32_t x, int32_t y)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066{
67 left += x;
68 top += y;
69 right+= x;
70 bottom+=y;
71 return *this;
72}
73
Mathias Agopianf1472a72009-05-26 17:44:57 -070074const Rect Rect::operator + (const Point& rhs) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075{
Mathias Agopianf1472a72009-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 Project9066cfe2009-03-03 19:31:44 -080078}
79
Mathias Agopianf1472a72009-05-26 17:44:57 -070080const Rect Rect::operator - (const Point& rhs) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081{
Mathias Agopianf1472a72009-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 Project9066cfe2009-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