Rect.h revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
1/* 2 * Copyright (C) 2010 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_HWUI_RECT_H 18#define ANDROID_HWUI_RECT_H 19 20#include <cmath> 21 22#include <utils/Log.h> 23 24namespace android { 25namespace uirenderer { 26 27/////////////////////////////////////////////////////////////////////////////// 28// Structs 29/////////////////////////////////////////////////////////////////////////////// 30 31struct Rect { 32 float left; 33 float top; 34 float right; 35 float bottom; 36 37 // Used by Region 38 typedef float value_type; 39 40 inline Rect(): 41 left(0), 42 top(0), 43 right(0), 44 bottom(0) { 45 } 46 47 inline Rect(float left, float top, float right, float bottom): 48 left(left), 49 top(top), 50 right(right), 51 bottom(bottom) { 52 } 53 54 inline Rect(float width, float height): 55 left(0.0f), 56 top(0.0f), 57 right(width), 58 bottom(height) { 59 } 60 61 inline Rect(const Rect& r) { 62 set(r); 63 } 64 65 inline Rect(Rect& r) { 66 set(r); 67 } 68 69 Rect& operator=(const Rect& r) { 70 set(r); 71 return *this; 72 } 73 74 Rect& operator=(Rect& r) { 75 set(r); 76 return *this; 77 } 78 79 friend int operator==(const Rect& a, const Rect& b) { 80 return !memcmp(&a, &b, sizeof(a)); 81 } 82 83 friend int operator!=(const Rect& a, const Rect& b) { 84 return memcmp(&a, &b, sizeof(a)); 85 } 86 87 inline void clear() { 88 left = top = right = bottom = 0.0f; 89 } 90 91 inline bool isEmpty() const { 92 return left >= right || top >= bottom; 93 } 94 95 inline void setEmpty() { 96 left = top = right = bottom = 0.0f; 97 } 98 99 inline void set(float left, float top, float right, float bottom) { 100 this->left = left; 101 this->right = right; 102 this->top = top; 103 this->bottom = bottom; 104 } 105 106 inline void set(const Rect& r) { 107 set(r.left, r.top, r.right, r.bottom); 108 } 109 110 inline float getWidth() const { 111 return right - left; 112 } 113 114 inline float getHeight() const { 115 return bottom - top; 116 } 117 118 bool intersects(float left, float top, float right, float bottom) const { 119 return left < right && top < bottom && 120 this->left < this->right && this->top < this->bottom && 121 this->left < right && left < this->right && 122 this->top < bottom && top < this->bottom; 123 } 124 125 bool intersects(const Rect& r) const { 126 return intersects(r.left, r.top, r.right, r.bottom); 127 } 128 129 bool intersect(float left, float top, float right, float bottom) { 130 if (left < right && top < bottom && !this->isEmpty() && 131 this->left < right && left < this->right && 132 this->top < bottom && top < this->bottom) { 133 134 if (this->left < left) this->left = left; 135 if (this->top < top) this->top = top; 136 if (this->right > right) this->right = right; 137 if (this->bottom > bottom) this->bottom = bottom; 138 139 return true; 140 } 141 return false; 142 } 143 144 bool intersect(const Rect& r) { 145 return intersect(r.left, r.top, r.right, r.bottom); 146 } 147 148 bool unionWith(const Rect& r) { 149 if (r.left < r.right && r.top < r.bottom) { 150 if (left < right && top < bottom) { 151 if (left > r.left) left = r.left; 152 if (top > r.top) top = r.top; 153 if (right < r.right) right = r.right; 154 if (bottom < r.bottom) bottom = r.bottom; 155 return true; 156 } else { 157 left = r.left; 158 top = r.top; 159 right = r.right; 160 bottom = r.bottom; 161 return true; 162 } 163 } 164 return false; 165 } 166 167 void translate(float dx, float dy) { 168 left += dx; 169 right += dx; 170 top += dy; 171 bottom += dy; 172 } 173 174 void snapToPixelBoundaries() { 175 left = floorf(left + 0.5f); 176 top = floorf(top + 0.5f); 177 right = floorf(right + 0.5f); 178 bottom = floorf(bottom + 0.5f); 179 } 180 181 void dump() const { 182 LOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom); 183 } 184 185}; // struct Rect 186 187}; // namespace uirenderer 188}; // namespace android 189 190#endif // ANDROID_HWUI_RECT_H 191