1 /* 2 * Copyright (C) 2009 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 #include <ui/Rect.h> 18 19 namespace android { 20 21 static inline int32_t min(int32_t a, int32_t b) { 22 return (a<b) ? a : b; 23 } 24 25 static inline int32_t max(int32_t a, int32_t b) { 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(int32_t x, int32_t 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(int32_t x, int32_t y) 66 { 67 left += x; 68 top += y; 69 right+= x; 70 bottom+=y; 71 return *this; 72 } 73 74 const Rect Rect::operator + (const Point& rhs) const 75 { 76 const Rect result(left+rhs.x, top+rhs.y, right+rhs.x, bottom+rhs.y); 77 return result; 78 } 79 80 const Rect Rect::operator - (const Point& rhs) const 81 { 82 const Rect result(left-rhs.x, top-rhs.y, right-rhs.x, bottom-rhs.y); 83 return result; 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 96