Rect.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Math/Rect.h"
  24. #include <cstdio>
  25. #include "../DebugNew.h"
  26. namespace Urho3D
  27. {
  28. const Rect Rect::FULL(-1.0f, -1.0f, 1.0f, 1.0f);
  29. const Rect Rect::POSITIVE(0.0f, 0.0f, 1.0f, 1.0f);
  30. const Rect Rect::ZERO(0.0f, 0.0f, 0.0f, 0.0f);
  31. const IntRect IntRect::ZERO(0, 0, 0, 0);
  32. void IntRect::Clip(const IntRect& rect)
  33. {
  34. if (rect.left_ > left_)
  35. left_ = rect.left_;
  36. if (rect.right_ < right_)
  37. right_ = rect.right_;
  38. if (rect.top_ > top_)
  39. top_ = rect.top_;
  40. if (rect.bottom_ < bottom_)
  41. bottom_ = rect.bottom_;
  42. if (left_ >= right_ || top_ >= bottom_)
  43. *this = IntRect();
  44. }
  45. void IntRect::Merge(const IntRect& rect)
  46. {
  47. if (Width() <= 0 || Height() <= 0)
  48. {
  49. *this = rect;
  50. }
  51. else if (rect.Width() > 0 && rect.Height() > 0)
  52. {
  53. if (rect.left_ < left_)
  54. left_ = rect.left_;
  55. if (rect.top_ < top_)
  56. top_ = rect.top_;
  57. if (rect.right_ > right_)
  58. right_ = rect.right_;
  59. if (rect.bottom_ > bottom_)
  60. bottom_ = rect.bottom_;
  61. }
  62. }
  63. String Rect::ToString() const
  64. {
  65. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  66. sprintf(tempBuffer, "%g %g %g %g", min_.x_, min_.y_, max_.x_, max_.y_);
  67. return String(tempBuffer);
  68. }
  69. String IntRect::ToString() const
  70. {
  71. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  72. sprintf(tempBuffer, "%d %d %d %d", left_, top_, right_, bottom_);
  73. return String(tempBuffer);
  74. }
  75. void Rect::Clip(const Rect& rect)
  76. {
  77. if (rect.min_.x_ > min_.x_)
  78. min_.x_ = rect.min_.x_;
  79. if (rect.max_.x_ < max_.x_)
  80. max_.x_ = rect.max_.x_;
  81. if (rect.min_.y_ > min_.y_)
  82. min_.y_ = rect.min_.y_;
  83. if (rect.max_.y_ < max_.y_)
  84. max_.y_ = rect.max_.y_;
  85. if (min_.x_ > max_.x_ || min_.y_ > max_.y_)
  86. {
  87. min_ = Vector2(M_INFINITY, M_INFINITY);
  88. max_ = Vector2(-M_INFINITY, -M_INFINITY);
  89. }
  90. }
  91. }