IRect.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GEOMETRY_IRECT
  24. #define DFPSR_GEOMETRY_IRECT
  25. #include <stdint.h>
  26. #include <math.h>
  27. #include <algorithm>
  28. #include "IVector.h"
  29. namespace dsr {
  30. class IRect {
  31. private:
  32. int32_t l, t, w, h;
  33. public:
  34. IRect() : l(0), t(0), w(0), h(0) {}
  35. IRect(int32_t left, int32_t top, int32_t width, int32_t height) : l(left), t(top), w(width), h(height) {}
  36. public:
  37. int32_t left() const { return this->l; }
  38. int32_t top() const { return this->t; }
  39. int32_t width() const { return this->w; }
  40. int32_t height() const { return this->h; }
  41. int32_t right() const { return this->l + this->w; }
  42. int32_t bottom() const { return this->t + this->h; }
  43. IVector2D size() const { return IVector2D(this->w, this->h); }
  44. int32_t area() const { return this->w * this->h; }
  45. IVector2D upperLeft() const { return IVector2D(this->l, this->t); }
  46. IVector2D upperRight() const { return IVector2D(this->l + this->w, this->t); }
  47. IVector2D lowerLeft() const { return IVector2D(this->l, this->t + this->h); }
  48. IVector2D lowerRight() const { return IVector2D(this->l + this->w, this->t + this->h); }
  49. bool hasArea() const { return this->w > 0 && this->h > 0; }
  50. IRect expanded(int units) const { return IRect(this->l - units, this->t - units, this->w + units * 2, this->h + units * 2); }
  51. // Returns the intersection between a and b or a rectangle that has no width nor height if overlaps(a, b) is false
  52. static IRect cut(const IRect &a, const IRect &b) {
  53. if (overlaps(a, b)) {
  54. int32_t leftSide = std::max(a.left(), b.left());
  55. int32_t topSide = std::max(a.top(), b.top());
  56. int32_t rightSide = std::min(a.right(), b.right());
  57. int32_t bottomSide = std::min(a.bottom(), b.bottom());
  58. return IRect(leftSide, topSide, rightSide - leftSide, bottomSide - topSide);
  59. } else {
  60. return IRect();
  61. }
  62. }
  63. // Returns a bounding box of the union
  64. static IRect merge(const IRect &a, const IRect &b) {
  65. int32_t leftSide = std::min(a.left(), b.left());
  66. int32_t topSide = std::min(a.top(), b.top());
  67. int32_t rightSide = std::max(a.right(), b.right());
  68. int32_t bottomSide = std::max(a.bottom(), b.bottom());
  69. return IRect(leftSide, topSide, rightSide - leftSide, bottomSide - topSide);
  70. }
  71. // Returns true iff the rectangles have an overlapping area
  72. // Equivalent to hasArea(a * b)
  73. static bool overlaps(const IRect& a, const IRect& b) {
  74. return a.left() < b.right() && a.right() > b.left() && a.top() < b.bottom() && a.bottom() > b.top();
  75. }
  76. // Returns true iff the rectangles touches
  77. static inline bool touches(const IRect& a, const IRect& b) {
  78. return a.left() <= b.right() && a.right() >= b.left() && a.top() <= b.bottom() && a.bottom() >= b.top();
  79. }
  80. // Create the rectangle from exclusive intervals
  81. static IRect FromBounds(int32_t left, int32_t top, int32_t right, int32_t bottom) {
  82. return IRect(left, top, right - left, bottom - top);
  83. }
  84. // Create the rectangle from a size
  85. static IRect FromSize(int32_t width, int32_t height) {
  86. return IRect(0, 0, width, height);
  87. }
  88. static IRect FromSize(IVector2D size) {
  89. return IRect(0, 0, size.x, size.y);
  90. }
  91. };
  92. // Move without resizing
  93. inline IRect operator+(const IRect &old, const IVector2D &offset) {
  94. return IRect(old.left() + offset.x, old.top() + offset.y, old.width(), old.height());
  95. }
  96. inline IRect operator-(const IRect &old, const IVector2D &offset) {
  97. return IRect(old.left() - offset.x, old.top() - offset.y, old.width(), old.height());
  98. }
  99. // Scale everything around origin
  100. inline IRect operator*(const IRect &old, int32_t scalar) {
  101. return IRect(old.left() * scalar, old.top() * scalar, old.width() * scalar, old.height() * scalar);
  102. }
  103. // Check equality
  104. inline bool operator==(const IRect &a, const IRect &b) {
  105. return a.left() == b.left() && a.top() == b.top() && a.width() == b.width() && a.height() == b.height();
  106. }
  107. inline bool operator!=(const IRect &a, const IRect &b) {
  108. return !(a == b);
  109. }
  110. inline String& string_toStreamIndented(String& target, const IRect& source, const ReadableString& indentation) {
  111. string_append(target, indentation, U"(", source.left(), U",", source.top(), U",", source.width(), U",", source.height(), U")");
  112. return target;
  113. }
  114. }
  115. #endif