CmRect.cpp 2.6 KB

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