Rect2I.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Math
  8. * @{
  9. */
  10. /// <summary>
  11. /// Represents a 2D rectangle using integer values. Rectangle is represented with an origin in top left and
  12. /// width/height.
  13. /// </summary>
  14. [StructLayout(LayoutKind.Sequential), SerializeObject]
  15. public struct Rect2I
  16. {
  17. public int x, y, width, height;
  18. /// <summary>
  19. /// Creates a new 2D rectangle.
  20. /// </summary>
  21. /// <param name="x">Left-most coordinate of the rectangle.</param>
  22. /// <param name="y">Top-most coordinate of the rectangle.</param>
  23. /// <param name="width">Width of the rectangle.</param>
  24. /// <param name="height">Height of the rectangle.</param>
  25. public Rect2I(int x, int y, int width, int height)
  26. {
  27. this.x = x;
  28. this.y = y;
  29. this.width = width;
  30. this.height = height;
  31. }
  32. public static bool operator== (Rect2I lhs, Rect2I rhs)
  33. {
  34. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.width == rhs.width && lhs.height == rhs.height;
  35. }
  36. public static bool operator!= (Rect2I lhs, Rect2I rhs)
  37. {
  38. return !(lhs == rhs);
  39. }
  40. /// <summary>
  41. /// Returns true if the rectangle contains the provided point.
  42. /// </summary>
  43. /// <param name="point">Point to check if it is in rectangle.</param>
  44. /// <returns>True if the point within rectangle bounds.</returns>
  45. public bool Contains(Vector2I point)
  46. {
  47. if(point.x >= x && point.x < (x + width))
  48. {
  49. if(point.y >= y && point.y < (y + height))
  50. return true;
  51. }
  52. return false;
  53. }
  54. /// <summary>
  55. /// Returns true if the rectangle overlaps the provided rectangle. Also returns true if the rectangles are
  56. // contained within each other completely (no intersecting edges).
  57. /// </summary>
  58. /// <param name="other">Other rectangle to compare with.</param>
  59. /// <returns>True if the rectangles overlap.</returns>
  60. public bool Overlaps(Rect2I other)
  61. {
  62. int otherRight = other.x + other.width;
  63. int myRight = x + width;
  64. int otherBottom = other.y + other.height;
  65. int myBottom = y + height;
  66. if(x < otherRight && myRight > other.x &&
  67. y < otherBottom && myBottom > other.y)
  68. return true;
  69. return false;
  70. }
  71. /// <summary>
  72. /// Clips current rectangle so that it does not overlap the provided rectangle. After clipping no area of this
  73. /// rectangle will intersect the clip area.
  74. /// </summary>
  75. /// <param name="clipRect">Rectangle to clip against.</param>
  76. public void Clip(Rect2I clipRect)
  77. {
  78. int newLeft = Math.Max(x, clipRect.x);
  79. int newTop = Math.Max(y, clipRect.y);
  80. int newRight = Math.Min(x + width, clipRect.x + clipRect.width);
  81. int newBottom = Math.Min(y + height, clipRect.y + clipRect.height);
  82. x = Math.Min(newLeft, newRight);
  83. y = Math.Min(newTop, newBottom);
  84. width = Math.Max(0, newRight - newLeft);
  85. height = Math.Max(0, newBottom - newTop);
  86. }
  87. /// <inheritdoc/>
  88. public override bool Equals(object other)
  89. {
  90. if (!(other is Rect2I))
  91. return false;
  92. Rect2I rect = (Rect2I)other;
  93. if (x.Equals(rect.x) && y.Equals(rect.y) && width.Equals(rect.width) && height.Equals(rect.height))
  94. return true;
  95. return false;
  96. }
  97. /// <inheritdoc/>
  98. public override int GetHashCode()
  99. {
  100. return base.GetHashCode();
  101. }
  102. /// <inheritdoc/>
  103. public override string ToString()
  104. {
  105. return String.Format("(x:{0} y:{1} width:{2} height:{3})", x, y, width, height);
  106. }
  107. }
  108. /** @} */
  109. }