2
0

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