IRect.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 area if overlaps(a, b) is false
  52. static IRect cut(const IRect &a, const IRect &b) {
  53. int32_t leftSide = std::max(a.left(), b.left());
  54. int32_t topSide = std::max(a.top(), b.top());
  55. int32_t rightSide = std::min(a.right(), b.right());
  56. int32_t bottomSide = std::min(a.bottom(), b.bottom());
  57. return IRect(leftSide, topSide, rightSide - leftSide, bottomSide - topSide);
  58. }
  59. // Returns a bounding box of the union
  60. static IRect merge(const IRect &a, const IRect &b) {
  61. int32_t leftSide = std::min(a.left(), b.left());
  62. int32_t topSide = std::min(a.top(), b.top());
  63. int32_t rightSide = std::max(a.right(), b.right());
  64. int32_t bottomSide = std::max(a.bottom(), b.bottom());
  65. return IRect(leftSide, topSide, rightSide - leftSide, bottomSide - topSide);
  66. }
  67. // Returns true iff the rectangles have an overlapping area
  68. // Equivalent to hasArea(a * b)
  69. static bool overlaps(const IRect& a, const IRect& b) {
  70. return a.left() < b.right() && a.right() > b.left() && a.top() < b.bottom() && a.bottom() > b.top();
  71. }
  72. // Returns true iff the rectangles touches
  73. static inline bool touches(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. // Create the rectangle from exclusive intervals
  77. static IRect FromBounds(int32_t left, int32_t top, int32_t right, int32_t bottom) {
  78. return IRect(left, top, right - left, bottom - top);
  79. }
  80. // Create the rectangle from a size
  81. static IRect FromSize(int32_t width, int32_t height) {
  82. return IRect(0, 0, width, height);
  83. }
  84. static IRect FromSize(IVector2D size) {
  85. return IRect(0, 0, size.x, size.y);
  86. }
  87. };
  88. // Move without resizing
  89. inline IRect operator+(const IRect &old, const IVector2D &offset) {
  90. return IRect(old.left() + offset.x, old.top() + offset.y, old.width(), old.height());
  91. }
  92. inline IRect operator-(const IRect &old, const IVector2D &offset) {
  93. return IRect(old.left() - offset.x, old.top() - offset.y, old.width(), old.height());
  94. }
  95. // Scale everything around origin
  96. inline IRect operator*(const IRect &old, int32_t scalar) {
  97. return IRect(old.left() * scalar, old.top() * scalar, old.width() * scalar, old.height() * scalar);
  98. }
  99. // Check equality
  100. inline bool operator==(const IRect &a, const IRect &b) {
  101. return a.left() == b.left() && a.top() == b.top() && a.width() == b.width() && a.height() == b.height();
  102. }
  103. inline bool operator!=(const IRect &a, const IRect &b) {
  104. return !(a == b);
  105. }
  106. inline String& string_toStreamIndented(String& target, const IRect& source, const ReadableString& indentation) {
  107. string_append(target, indentation, U"(", source.left(), U",", source.top(), U",", source.width(), U",", source.height(), U")");
  108. return target;
  109. }
  110. }
  111. #endif