Rect.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Math/Rect.h"
  5. #include <cstdio>
  6. #include "../DebugNew.h"
  7. namespace Urho3D
  8. {
  9. const Rect Rect::FULL(-1.0f, -1.0f, 1.0f, 1.0f);
  10. const Rect Rect::POSITIVE(0.0f, 0.0f, 1.0f, 1.0f);
  11. const Rect Rect::ZERO(0.0f, 0.0f, 0.0f, 0.0f);
  12. const IntRect IntRect::ZERO(0, 0, 0, 0);
  13. void IntRect::Clip(const IntRect& rect)
  14. {
  15. if (rect.left_ > left_)
  16. left_ = rect.left_;
  17. if (rect.right_ < right_)
  18. right_ = rect.right_;
  19. if (rect.top_ > top_)
  20. top_ = rect.top_;
  21. if (rect.bottom_ < bottom_)
  22. bottom_ = rect.bottom_;
  23. if (left_ >= right_ || top_ >= bottom_)
  24. *this = IntRect();
  25. }
  26. void IntRect::Merge(const IntRect& rect)
  27. {
  28. if (Width() <= 0 || Height() <= 0)
  29. {
  30. *this = rect;
  31. }
  32. else if (rect.Width() > 0 && rect.Height() > 0)
  33. {
  34. if (rect.left_ < left_)
  35. left_ = rect.left_;
  36. if (rect.top_ < top_)
  37. top_ = rect.top_;
  38. if (rect.right_ > right_)
  39. right_ = rect.right_;
  40. if (rect.bottom_ > bottom_)
  41. bottom_ = rect.bottom_;
  42. }
  43. }
  44. String Rect::ToString() const
  45. {
  46. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  47. sprintf(tempBuffer, "%g %g %g %g", min_.x_, min_.y_, max_.x_, max_.y_);
  48. return String(tempBuffer);
  49. }
  50. String IntRect::ToString() const
  51. {
  52. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  53. sprintf(tempBuffer, "%d %d %d %d", left_, top_, right_, bottom_);
  54. return String(tempBuffer);
  55. }
  56. void Rect::Clip(const Rect& rect)
  57. {
  58. if (rect.min_.x_ > min_.x_)
  59. min_.x_ = rect.min_.x_;
  60. if (rect.max_.x_ < max_.x_)
  61. max_.x_ = rect.max_.x_;
  62. if (rect.min_.y_ > min_.y_)
  63. min_.y_ = rect.min_.y_;
  64. if (rect.max_.y_ < max_.y_)
  65. max_.y_ = rect.max_.y_;
  66. if (min_.x_ > max_.x_ || min_.y_ > max_.y_)
  67. {
  68. min_ = Vector2(M_INFINITY, M_INFINITY);
  69. max_ = Vector2(-M_INFINITY, -M_INFINITY);
  70. }
  71. }
  72. }