2
0

BsRect2.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Math/BsRect2.h"
  4. #include "Math/BsVector2.h"
  5. #include "Math/BsMatrix4.h"
  6. #include "Math/BsMath.h"
  7. namespace bs
  8. {
  9. const Rect2 Rect2::EMPTY = Rect2();
  10. Rect2::Rect2()
  11. :x(0), y(0), width(0), height(0)
  12. { }
  13. Rect2::Rect2(float _x, float _y, float _width, float _height)
  14. :x(_x), y(_y), width(_width), height(_height)
  15. { }
  16. bool Rect2::contains(const Vector2& point) const
  17. {
  18. if(point.x >= x && point.x <= (x + width))
  19. {
  20. if(point.y >= y && point.y <= (y + height))
  21. return true;
  22. }
  23. return false;
  24. }
  25. bool Rect2::overlaps(const Rect2& other) const
  26. {
  27. float otherRight = other.x + other.width;
  28. float myRight = x + width;
  29. float otherBottom = other.y + other.height;
  30. float myBottom = y + height;
  31. if(x < otherRight && myRight > other.x &&
  32. y < otherBottom && myBottom > other.y)
  33. return true;
  34. return false;
  35. }
  36. void Rect2::encapsulate(const Rect2& other)
  37. {
  38. float myRight = x + width;
  39. float myBottom = y + height;
  40. float otherRight = other.x + other.width;
  41. float otherBottom = other.y + other.height;
  42. if(other.x < x)
  43. x = other.x;
  44. if(other.y < y)
  45. y = other.y;
  46. if(otherRight > myRight)
  47. width = otherRight - x;
  48. else
  49. width = myRight - x;
  50. if(otherBottom > myBottom)
  51. height = otherBottom - y;
  52. else
  53. height = myBottom - y;
  54. }
  55. void Rect2::clip(const Rect2& clipRect)
  56. {
  57. float newLeft = std::max(x, clipRect.x);
  58. float newTop = std::max(y, clipRect.y);
  59. float newRight = std::min(x + width, clipRect.x + clipRect.width);
  60. float newBottom = std::min(y + height, clipRect.y + clipRect.height);
  61. x = newLeft;
  62. y = newTop;
  63. width = newRight - newLeft;
  64. height = newBottom - newTop;
  65. }
  66. void Rect2::transform(const Matrix4& matrix)
  67. {
  68. Vector4 verts[4];
  69. verts[0] = Vector4(x, y, 0.0f, 1.0f);
  70. verts[1] = Vector4(x + width, y, 0.0f, 1.0f);
  71. verts[2] = Vector4(x, y + height, 0.0f, 1.0f);
  72. verts[3] = Vector4(x + width, y + height, 0.0f, 1.0f);
  73. for(UINT32 i = 0; i < 4; i++)
  74. verts[i] = matrix.multiply(verts[i]);
  75. float minX = std::numeric_limits<float>::max();
  76. float maxX = std::numeric_limits<float>::min();
  77. float minY = std::numeric_limits<float>::max();
  78. float maxY = std::numeric_limits<float>::min();
  79. for(UINT32 i = 0; i < 4; i++)
  80. {
  81. if(verts[i].x < minX)
  82. minX = verts[i].x;
  83. if(verts[i].y < minY)
  84. minY = verts[i].y;
  85. if(verts[i].x > maxX)
  86. maxX = verts[i].x;
  87. if(verts[i].y > maxY)
  88. maxY = verts[i].y;
  89. }
  90. x = minX;
  91. y = minY;
  92. width = maxX - x;
  93. height = maxY - y;
  94. }
  95. }