BsRectI.cpp 2.9 KB

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