1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup UI_Utils 18 * @{ 19 * 20 * @brief Defines basic UI utils. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file rect.h 28 * 29 * @brief Defines a rectangle, including the position data of the four boundaries of the rectangle, and provides 30 * functions for rectangle inclusion, intersection, and aggregation. 31 * @since 1.0 32 * @version 1.0 33 */ 34 35 #ifndef GRAPHIC_LITE_RECT_H 36 #define GRAPHIC_LITE_RECT_H 37 38 #include "gfx_utils/graphic_math.h" 39 #include "gfx_utils/graphic_types.h" 40 #include "gfx_utils/heap_base.h" 41 42 namespace OHOS { 43 /** 44 * @brief Defines a rectangle, including the position data of the four boundaries of the rectangle, and provides 45 * functions for rectangle inclusion, intersection, and aggregation. 46 * @since 1.0 47 * @version 1.0 48 */ 49 template <typename T> class CommonRect : public HeapBase { 50 public: 51 /** 52 * @brief A constructor used to create a <b>CommonRect</b> instance. 53 * @since 1.0 54 * @version 1.0 55 */ CommonRect()56 CommonRect() : left_(0), top_(0), right_(0), bottom_(0) {} 57 58 /** 59 * @brief A destructor used to delete the <b>CommonRect</b> instance. 60 * @since 1.0 61 * @version 1.0 62 */ ~CommonRect()63 ~CommonRect() {} 64 65 /** 66 * @brief A constructor used to create a <b>CommonRect</b> instance based on the coordinates of the four boundaries. 67 * @param left Indicates the coordinate of the left boundary. 68 * @param top Indicates the coordinate of the top boundary. 69 * @param right Indicates the coordinate of the right boundary. 70 * @param bottom Indicates the coordinate of the bottom boundary. 71 * @since 1.0 72 * @version 1.0 73 */ CommonRect(T left,T top,T right,T bottom)74 CommonRect(T left, T top, T right, T bottom) 75 { 76 left_ = left; 77 right_ = right; 78 top_ = top; 79 bottom_ = bottom; 80 } 81 82 /** 83 * @brief A constructor used to create a <b>CommonRect</b> instance by copying another rectangle. 84 * 85 * @param other Indicates the rectangle to copy. 86 * @since 1.0 87 * @version 1.0 88 */ CommonRect(const CommonRect<T> & other)89 CommonRect(const CommonRect<T>& other) 90 91 { 92 left_ = other.left_; 93 right_ = other.right_; 94 top_ = other.top_; 95 bottom_ = other.bottom_; 96 } 97 98 /** 99 * @brief A constructor used to create a <b>CommonRect</b> instance by copying another rectangle. 100 * 101 * @param other Indicates the rectangle to copy. 102 * @since 1.0 103 * @version 1.0 104 */ CommonRect(const CommonRect<T> && other)105 CommonRect(const CommonRect<T>&& other) 106 { 107 left_ = other.left_; 108 right_ = other.right_; 109 top_ = other.top_; 110 bottom_ = other.bottom_; 111 } 112 113 /** 114 * @brief Sets the coordinates of the four boundaries of a rectangle. 115 * 116 * @param left Indicates the coordinate of the left boundary. 117 * @param top Indicates the coordinate of the top boundary. 118 * @param right Indicates the coordinate of the right boundary. 119 * @param bottom Indicates the coordinate of the bottom boundary. 120 * @since 1.0 121 * @version 1.0 122 */ SetRect(T left,T top,T right,T bottom)123 void SetRect(T left, T top, T right, T bottom) 124 { 125 left_ = left; 126 right_ = right; 127 top_ = top; 128 bottom_ = bottom; 129 } 130 131 /** 132 * @brief Obtains the rectangle width. 133 * @return Returns the rectangle width. 134 * @since 1.0 135 * @version 1.0 136 */ GetWidth()137 T GetWidth() const 138 { 139 return right_ - left_ + 1; 140 } 141 142 /** 143 * @brief Obtains the rectangle height. 144 * @return Returns the rectangle height. 145 * @since 1.0 146 * @version 1.0 147 */ GetHeight()148 T GetHeight() const 149 { 150 return bottom_ - top_ + 1; 151 } 152 153 /** 154 * @brief Obtains the left boundary coordinate of the rectangle. 155 * @return Returns the left boundary coordinate. 156 * @since 1.0 157 * @version 1.0 158 */ GetX()159 T GetX() const 160 { 161 return left_; 162 } 163 164 /** 165 * @brief Obtains the top boundary coordinate of the rectangle. 166 * @return Returns the top boundary coordinate. 167 * @since 1.0 168 * @version 1.0 169 */ GetY()170 T GetY() const 171 { 172 return top_; 173 } 174 175 /** 176 * @brief Obtains the left boundary coordinate of the rectangle. 177 * @return Returns the left boundary coordinate. 178 * @since 1.0 179 * @version 1.0 180 */ GetLeft()181 T GetLeft() const 182 { 183 return left_; 184 } 185 186 /** 187 * @brief Obtains the top boundary coordinate of the rectangle. 188 * @return Returns the top boundary coordinate. 189 * @since 1.0 190 * @version 1.0 191 */ GetTop()192 T GetTop() const 193 { 194 return top_; 195 } 196 197 /** 198 * @brief Obtains the right boundary coordinate of the rectangle. 199 * @return Returns the right boundary coordinate. 200 * @since 1.0 201 * @version 1.0 202 */ GetRight()203 T GetRight() const 204 { 205 return right_; 206 } 207 208 /** 209 * @brief Obtains the bottom boundary coordinate of the rectangle. 210 * @return Returns the bottom boundary coordinate. 211 * @since 1.0 212 * @version 1.0 213 */ GetBottom()214 T GetBottom() const 215 { 216 return bottom_; 217 } 218 219 /** 220 * @brief Changes the left boundary coordinate of the rectangle without changing the rectangle width. 221 * @param x Indicates the coordinate of the left boundary. 222 * @since 1.0 223 * @version 1.0 224 */ SetX(T x)225 void SetX(T x) 226 { 227 right_ += x - left_; 228 left_ = x; 229 } 230 231 /** 232 * @brief Changes the top boundary coordinate of the rectangle without changing the rectangle height. 233 * @param y Indicates the coordinate of the top boundary. 234 * @since 1.0 235 * @version 1.0 236 */ SetY(T y)237 void SetY(T y) 238 { 239 bottom_ += y - top_; 240 top_ = y; 241 } 242 243 /** 244 * @brief Changes the coordinates of the left and top boundaries of the rectangle without changing the rectangle 245 * width and height. 246 * @param x Indicates the coordinate of the left boundary. 247 * @param y Indicates the coordinate of the top boundary. 248 * @since 1.0 249 * @version 1.0 250 */ SetPosition(T x,T y)251 void SetPosition(T x, T y) 252 { 253 right_ += x - left_; 254 bottom_ += y - top_; 255 left_ = x; 256 top_ = y; 257 } 258 259 /** 260 * @brief Changes the width of the rectangle without changing the coordinate of the left boundary. 261 * @param width Indicates the width of the rectangle. 262 * @since 1.0 263 * @version 1.0 264 */ SetWidth(T width)265 void SetWidth(T width) 266 { 267 right_ = left_ + width - 1; 268 } 269 270 /** 271 * @brief Changes the height of the rectangle without changing the coordinate of the top boundary. 272 * @param height Indicates the height of the rectangle. 273 * @since 1.0 274 * @version 1.0 275 */ SetHeight(T height)276 void SetHeight(T height) 277 { 278 bottom_ = top_ + height - 1; 279 } 280 281 /** 282 * @brief Sets the coordinate of the left boundary of a rectangle. 283 * @param left Indicates the coordinate of the left boundary. 284 * @since 1.0 285 * @version 1.0 286 */ SetLeft(T left)287 void SetLeft(T left) 288 { 289 left_ = left; 290 } 291 292 /** 293 * @brief Sets the coordinate of the top boundary of a rectangle. 294 * @param top Indicates the coordinate of the top boundary. 295 * @since 1.0 296 * @version 1.0 297 */ SetTop(T top)298 void SetTop(T top) 299 { 300 top_ = top; 301 } 302 303 /** 304 * @brief Sets the coordinate of the right boundary of a rectangle. 305 * @param right Indicates the coordinate of the right boundary. 306 * @since 1.0 307 * @version 1.0 308 */ SetRight(T right)309 void SetRight(T right) 310 { 311 right_ = right; 312 } 313 314 /** 315 * @brief Sets the coordinate of the bottom boundary of a rectangle. 316 * @param bottom Indicates the coordinate of the bottom boundary. 317 * @since 1.0 318 * @version 1.0 319 */ SetBottom(T bottom)320 void SetBottom(T bottom) 321 { 322 bottom_ = bottom; 323 } 324 325 /** 326 * @brief Sets the width and height of a rectangle. 327 * @param width Indicates the width of the rectangle. 328 * @param height Indicates the height of the rectangle. 329 * @since 1.0 330 * @version 1.0 331 */ Resize(T width,T height)332 void Resize(T width, T height) 333 { 334 right_ = left_ + width - 1; 335 bottom_ = top_ + height - 1; 336 } 337 338 /** 339 * @brief Obtains the area of a rectangle. 340 * @return Returns the area of the rectangle. 341 * @since 1.0 342 * @version 1.0 343 */ GetSize()344 uint32_t GetSize() const 345 { 346 return static_cast<uint32_t>(right_ - left_ + 1) * (bottom_ - top_ + 1); 347 } 348 349 /** 350 * @brief Checks whether two rectangles intersect. 351 * @param rect1 Indicates the first rectangle to check. 352 * @param rect2 Indicates the second rectangle to check. 353 * @return Returns <b>true</b> if the two rectangles intersect; returns <b>false</b> otherwise. 354 * @since 1.0 355 * @version 1.0 356 */ Intersect(const CommonRect<T> & rect1,const CommonRect<T> & rect2)357 bool Intersect(const CommonRect<T>& rect1, const CommonRect<T>& rect2) 358 { 359 /* Get the smaller area from 'rect1' and 'rect2' */ 360 left_ = MATH_MAX(rect1.left_, rect2.left_); 361 top_ = MATH_MAX(rect1.top_, rect2.top_); 362 right_ = MATH_MIN(rect1.right_, rect2.right_); 363 bottom_ = MATH_MIN(rect1.bottom_, rect2.bottom_); 364 if ((left_ > right_) || (top_ > bottom_)) { 365 return false; 366 } 367 368 return true; 369 } 370 371 /** 372 * @brief Obtains the minimum rectangle that contains another two rectangles. 373 * @param rect1 Indicates the first rectangle to contain. 374 * @param rect2 Indicates the second rectangle to contain. 375 * @since 1.0 376 * @version 1.0 377 */ Join(const CommonRect<T> & rect1,const CommonRect<T> & rect2)378 void Join(const CommonRect<T>& rect1, const CommonRect<T>& rect2) 379 { 380 left_ = MATH_MIN(rect1.left_, rect2.left_); 381 top_ = MATH_MIN(rect1.top_, rect2.top_); 382 right_ = MATH_MAX(rect1.right_, rect2.right_); 383 bottom_ = MATH_MAX(rect1.bottom_, rect2.bottom_); 384 } 385 386 /** 387 * @brief Checks whether the rectangle contains a coordinate point. 388 * @param point Indicates the coordinate point. 389 * @return Returns <b>true</b> if the input coordinate point is contained; returns <b>false</b> otherwise. 390 * @since 1.0 391 * @version 1.0 392 */ IsContains(const Vector2<T> & point)393 bool IsContains(const Vector2<T>& point) const 394 { 395 bool isContains = false; 396 397 if ((point.x_ >= this->left_) && (point.x_ <= this->right_) && (point.y_ >= this->top_) && 398 (point.y_ <= this->bottom_)) { 399 isContains = true; 400 } 401 402 return isContains; 403 } 404 405 /** 406 * @brief Checks whether the rectangle contains a coordinate point. 407 * @param point Indicates the coordinate point. 408 * @return Returns <b>true</b> if the input coordinate point is contained; returns <b>false</b> otherwise. 409 * @since 1.0 410 * @version 1.0 411 */ IsContains(const Point & point)412 bool IsContains(const Point& point) const 413 { 414 bool isContains = false; 415 416 if ((point.x >= this->left_) && (point.x <= this->right_) && (point.y >= this->top_) && 417 (point.y <= this->bottom_)) { 418 isContains = true; 419 } 420 421 return isContains; 422 } 423 424 /** 425 * @brief Checks whether the rectangle is adjacent to another rectangle horizontally or vertically. 426 * @param other Indicates the rectangle to be used for check. 427 * @return Returns <b>true</b> if the rectangle is adjacent to the input rectangle; returns <b>false</b> otherwise. 428 * @since 1.0 429 * @version 1.0 430 */ IsExtends(const CommonRect<T> & other)431 bool IsExtends(const CommonRect<T>& other) const 432 { 433 if (left_ == other.left_ && right_ == other.right_) { 434 return (top_ == other.bottom_ + 1) || (bottom_ == other.top_ - 1); 435 } 436 437 if (top_ == other.top_ && bottom_ == other.bottom_) { 438 return (left_ == other.right_ + 1) || (right_ == other.left_ - 1); 439 } 440 441 return false; 442 } 443 444 /** 445 * @brief Checks whether the rectangle intersects with another rectangle. 446 * @param other Indicates the rectangle to be used for check. 447 * @return Returns <b>true</b> if the two rectangles intersect; returns <b>false</b> otherwise. 448 * @since 1.0 449 * @version 1.0 450 */ IsIntersect(const CommonRect<T> & other)451 bool IsIntersect(const CommonRect<T>& other) const 452 { 453 if ((this->left_ <= other.right_) && (this->right_ >= other.left_) && (this->top_ <= other.bottom_) && 454 (this->bottom_ >= other.top_)) { 455 return true; 456 } else { 457 return false; 458 } 459 } 460 461 /** 462 * @brief Checks whether the rectangle contains another rectangle. 463 * 464 * @param other Indicates the rectangle to be used for check. 465 * @return Returns <b>true</b> if the input rectangle is contained; returns <b>false</b> otherwise. 466 * @since 1.0 467 * @version 1.0 468 */ IsContains(const CommonRect<T> & other)469 bool IsContains(const CommonRect<T>& other) const 470 { 471 bool isContains = false; 472 473 if (other.left_ >= this->left_ && other.top_ >= this->top_ && other.right_ <= this->right_ && 474 other.bottom_ <= this->bottom_) { 475 isContains = true; 476 } 477 478 return isContains; 479 } 480 481 void Inflate(T delta); 482 void operator=(const CommonRect<T>& other) 483 { 484 left_ = other.left_; 485 right_ = other.right_; 486 top_ = other.top_; 487 bottom_ = other.bottom_; 488 } 489 void operator=(const CommonRect<T>&& other) 490 { 491 left_ = other.left_; 492 right_ = other.right_; 493 top_ = other.top_; 494 bottom_ = other.bottom_; 495 } 496 bool operator==(const CommonRect<T>& other) const 497 { 498 if (left_ == other.left_ && right_ == other.right_ && top_ == other.top_ && bottom_ == other.bottom_) { 499 return true; 500 } else { 501 return false; 502 } 503 } 504 505 protected: 506 T left_; 507 T top_; 508 T right_; 509 T bottom_; 510 }; 511 using Rect = CommonRect<int16_t>; 512 using Rect32 = CommonRect<int32_t>; 513 } // namespace OHOS 514 #endif // GRAPHIC_LITE_RECT_H