hnOsmium0001

rect2 + rect2i cpp

Sep 11th, 2020
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. template <class Vector, class UVector>
  3. auto Rect2KImpl<Vector, UVector>::FromCorners(Vector tl, Vector br) -> Rect2KImpl<Vector, UVector> {
  4.     return Rect2KImpl<Vector, UVector>{
  5.         .TopLeft = tl,
  6.         .BottomRight = br,
  7.     };
  8. }
  9.  
  10. template <class Vector, class UVector>
  11. auto Rect2KImpl<Vector, UVector>::FromHalfDim(Vector center, UScalar halfDimension) -> Rect2KImpl<Vector, UVector> {
  12.     return Rect2KImpl<Vector, UVector>{
  13.         .TopLeft = Vector{ center.x - halfDimension, center.y - halfDimension },
  14.         .BottomRight = Vector{ center.x + halfDimension, center.y + halfDimension },
  15.     };
  16. }
  17.  
  18. template <class Vector, class UVector>
  19. auto Rect2KImpl<Vector, UVector>::FromDim(Vector topLeft, UVector dimensions) -> Rect2KImpl<Vector, UVector> {
  20.     return Rect2KImpl<Vector, UVector>{
  21.         .TopLeft = Vector{ topLeft },
  22.         .BottomRight = Vector{ topLeft.x + dimensions.x, topLeft.y + dimensions.y },
  23.     };
  24. }
  25.  
  26. template <class Vector, class UVector>
  27. auto Rect2KImpl<Vector, UVector>::FromScalars(Scalar x, Scalar y, UScalar width, UScalar height) -> Rect2KImpl<Vector, UVector> {
  28.     return Rect2KImpl<Vector, UVector>{
  29.         .TopLeft = Vector{ x, y },
  30.         .BottomRight = Vector{ x + width, y + height },
  31.     };
  32. }
  33.  
  34. template <class Vector, class UVector>
  35. auto Rect2KImpl<Vector, UVector>::GetCenter() const -> Vector {
  36.     UScalar halfWidth = (BottomRight.x - TopLeft.x) / 2;
  37.     UScalar halfHeight = (BottomRight.y - TopLeft.y) / 2;
  38.     return { TopLeft.x + halfWidth, TopLeft.y + halfHeight };
  39. }
  40.  
  41. template <class Vector, class UVector>
  42. auto Rect2KImpl<Vector, UVector>::GetTopRight() const -> Vector {
  43.     return { BottomRight.x, TopLeft.y };
  44. }
  45.  
  46. template <class Vector, class UVector>
  47. auto Rect2KImpl<Vector, UVector>::SetTopRight(Vector topRight) -> void {
  48.     BottomRight.x = topRight.x;
  49.     TopLeft.y = topRight.y;
  50. }
  51.  
  52. template <class Vector, class UVector>
  53. auto Rect2KImpl<Vector, UVector>::GetBottomLeft() const -> Vector {
  54.     return { TopLeft.x, BottomRight.y };
  55. }
  56.  
  57. template <class Vector, class UVector>
  58. auto Rect2KImpl<Vector, UVector>::SetBottomLeft(Vector bottomLeft) -> void {
  59.     TopLeft.x = bottomLeft.x;
  60.     BottomRight.y = bottomLeft.y;
  61. }
  62.  
  63. template <class Vector, class UVector>
  64. auto Rect2KImpl<Vector, UVector>::GetX() const -> Scalar {
  65.     return TopLeft.x;
  66. }
  67.  
  68. template <class Vector, class UVector>
  69. auto Rect2KImpl<Vector, UVector>::SetX(Scalar x) -> void {
  70.     TopLeft.x = x;
  71. }
  72.  
  73. template <class Vector, class UVector>
  74. auto Rect2KImpl<Vector, UVector>::GetY() const -> Scalar {
  75.     return TopLeft.y;
  76. }
  77.  
  78. template <class Vector, class UVector>
  79. auto Rect2KImpl<Vector, UVector>::SetY(Scalar y) -> void {
  80.     TopLeft.y = y;
  81. }
  82.  
  83. template <class Vector, class UVector>
  84. auto Rect2KImpl<Vector, UVector>::GetWidth() const -> UScalar {
  85.     return BottomRight.x - TopLeft.x;
  86. }
  87.  
  88. template <class Vector, class UVector>
  89. auto Rect2KImpl<Vector, UVector>::SetWidth(UScalar width) -> void {
  90.     BottomRight.x = TopLeft.x + width;
  91. }
  92.  
  93. template <class Vector, class UVector>
  94. auto Rect2KImpl<Vector, UVector>::GetHeight() const -> UScalar {
  95.     return BottomRight.y - TopLeft.y;
  96. }
  97.  
  98. template <class Vector, class UVector>
  99. auto Rect2KImpl<Vector, UVector>::SetHeight(UScalar height) -> void {
  100.     BottomRight.y = TopLeft.y + height;
  101. }
  102.  
  103. template <class Vector, class UVector>
  104. auto Rect2KImpl<Vector, UVector>::GetDimensions() const -> UVector {
  105.     return { Width, Height };
  106. }
  107.  
  108. template <class Vector, class UVector>
  109. auto Rect2KImpl<Vector, UVector>::SetDimensions(UVector dimensions) -> void {
  110.     Width = dimensions.x;
  111.     Height = dimensions.y;
  112. }
  113.  
  114. template <class Vector, class UVector>
  115. auto Rect2KImpl<Vector, UVector>::GetHalfDimensions() const -> UVector {
  116.     return Dimensions / static_cast<UScalar>(2);
  117. }
  118.  
  119. template <class Vector, class UVector>
  120. auto Rect2KImpl<Vector, UVector>::SetHalfDimensions(UVector halfDimensions) -> void {
  121.     Dimensions = halfDimensions * static_cast<UScalar>(2);
  122. }
  123.  
  124. template <class Vector, class UVector>
  125. auto Rect2KImpl<Vector, UVector>::HasPoint(Vector pt) const -> bool {
  126.     return pt.x >= TopLeft.x &&
  127.            pt.x <= BottomRight.y &&
  128.            pt.y >= TopLeft.y &&
  129.            pt.y <= BottomRight.y;
  130. }
  131.  
  132. template <class Vector, class UVector>
  133. auto Rect2KImpl<Vector, UVector>::IntersectsWith(const Rect2KImpl<Vector, UVector>& rect) const -> bool {
  134.     return rect.HasPoint(TopLeft) ||
  135.            rect.HasPoint(TopRight) ||
  136.            rect.HasPoint(BottomLeft) ||
  137.            rect.HasPoint(BottomRight);
  138. }
  139.  
  140. template struct Rect2KImpl<glm::ivec2, glm::uvec2>;
  141. template struct Rect2KImpl<glm::vec2, glm::vec2>;
  142.  
RAW Paste Data