The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | * |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 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. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <ui/Rect.h> |
| 18 | |
| 19 | namespace android { |
| 20 | |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 21 | static inline int min(int a, int b) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | return (a<b) ? a : b; |
| 23 | } |
| 24 | |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 25 | static inline int max(int a, int b) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | return (a>b) ? a : b; |
| 27 | } |
| 28 | |
| 29 | void Rect::makeInvalid() { |
| 30 | left = 0; |
| 31 | top = 0; |
| 32 | right = -1; |
| 33 | bottom = -1; |
| 34 | } |
| 35 | |
| 36 | bool 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 | |
| 56 | Rect& 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 | |
| 65 | Rect& 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 Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 74 | const Rect Rect::operator + (const Point& rhs) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | { |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 76 | const Rect result(left+rhs.x, top+rhs.y, right+rhs.x, bottom+rhs.y); |
| 77 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 80 | const Rect Rect::operator - (const Point& rhs) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | { |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 82 | const Rect result(left-rhs.x, top-rhs.y, right-rhs.x, bottom-rhs.y); |
| 83 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool 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 |