BsRectF.cpp 2.8 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 "BsRectF.h"
  5. #include "BsVector2.h"
  6. #include "BsMatrix4.h"
  7. #include "BsMath.h"
  8. namespace BansheeEngine
  9. {
  10. const RectF RectF::EMPTY = RectF();
  11. RectF::RectF()
  12. :x(0), y(0), width(0), height(0)
  13. { }
  14. RectF::RectF(float _x, float _y, float _width, float _height)
  15. :x(_x), y(_y), width(_width), height(_height)
  16. { }
  17. bool RectF::contains(const Vector2& 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 RectF::overlaps(const RectF& other) const
  27. {
  28. float otherRight = other.x + other.width;
  29. float myRight = x + width;
  30. float otherBottom = other.y + other.height;
  31. float 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 RectF::encapsulate(const RectF& other)
  38. {
  39. float myRight = x + width;
  40. float myBottom = y + height;
  41. float otherRight = other.x + other.width;
  42. float 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 RectF::clip(const RectF& clipRect)
  57. {
  58. float newLeft = std::max(x, clipRect.x);
  59. float newTop = std::max(y, clipRect.y);
  60. float newRight = std::min(x + width, clipRect.x + clipRect.width);
  61. float newBottom = std::min(y + height, clipRect.y + clipRect.height);
  62. x = newLeft;
  63. y = newTop;
  64. width = newRight - newLeft;
  65. height = newBottom - newTop;
  66. }
  67. void RectF::transform(const Matrix4& matrix)
  68. {
  69. Vector4 verts[4];
  70. verts[0] = Vector4(x, y, 0.0f, 1.0f);
  71. verts[1] = Vector4(x + width, y, 0.0f, 1.0f);
  72. verts[2] = Vector4(x, y + height, 0.0f, 1.0f);
  73. verts[3] = Vector4(x + width, 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 = minX;
  92. y = minY;
  93. width = maxX - x;
  94. height = maxY - y;
  95. }
  96. }