Rect2I.cs 3.8 KB

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