CmRect.cpp 2.6 KB

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