Region.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /// <summary>
  2. /// Represents a region composed of one or more rectangles, providing methods for union, intersection, exclusion, and
  3. /// complement operations.
  4. /// </summary>
  5. public class Region : IDisposable
  6. {
  7. private List<Rectangle> _rectangles;
  8. /// <summary>
  9. /// Initializes a new instance of the <see cref="Region"/> class.
  10. /// </summary>
  11. public Region () { _rectangles = new (); }
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="Region"/> class with the specified rectangle.
  14. /// </summary>
  15. /// <param name="rectangle">The initial rectangle for the region.</param>
  16. public Region (Rectangle rectangle) { _rectangles = new() { rectangle }; }
  17. /// <summary>
  18. /// Adds the specified rectangle to the region.
  19. /// </summary>
  20. /// <param name="rectangle">The rectangle to add to the region.</param>
  21. public void Union (Rectangle rectangle)
  22. {
  23. _rectangles.Add (rectangle);
  24. _rectangles = MergeRectangles (_rectangles);
  25. }
  26. /// <summary>
  27. /// Adds the specified region to this region.
  28. /// </summary>
  29. /// <param name="region">The region to add to this region.</param>
  30. public void Union (Region region)
  31. {
  32. _rectangles.AddRange (region._rectangles);
  33. _rectangles = MergeRectangles (_rectangles);
  34. }
  35. /// <summary>
  36. /// Updates the region to be the intersection of itself with the specified rectangle.
  37. /// </summary>
  38. /// <param name="rectangle">The rectangle to intersect with the region.</param>
  39. public void Intersect (Rectangle rectangle)
  40. {
  41. _rectangles = _rectangles.Select (r => Rectangle.Intersect (r, rectangle)).Where (r => !r.IsEmpty).ToList ();
  42. }
  43. /// <summary>
  44. /// Updates the region to be the intersection of itself with the specified region.
  45. /// </summary>
  46. /// <param name="region">The region to intersect with this region.</param>
  47. public void Intersect (Region region)
  48. {
  49. List<Rectangle> intersections = new List<Rectangle> ();
  50. foreach (Rectangle rect1 in _rectangles)
  51. {
  52. foreach (Rectangle rect2 in region._rectangles)
  53. {
  54. Rectangle intersected = Rectangle.Intersect (rect1, rect2);
  55. if (!intersected.IsEmpty)
  56. {
  57. intersections.Add (intersected);
  58. }
  59. }
  60. }
  61. _rectangles = intersections;
  62. }
  63. /// <summary>
  64. /// Removes the portion of the specified rectangle from the region.
  65. /// </summary>
  66. /// <param name="rectangle">The rectangle to exclude from the region.</param>
  67. public void Exclude (Rectangle rectangle) { _rectangles = _rectangles.SelectMany (r => SubtractRectangle (r, rectangle)).ToList (); }
  68. /// <summary>
  69. /// Removes the portion of the specified region from this region.
  70. /// </summary>
  71. /// <param name="region">The region to exclude from this region.</param>
  72. public void Exclude (Region region)
  73. {
  74. foreach (Rectangle rect in region._rectangles)
  75. {
  76. _rectangles = _rectangles.SelectMany (r => SubtractRectangle (r, rect)).ToList ();
  77. }
  78. }
  79. /// <summary>
  80. /// Updates the region to be the complement of itself within the specified bounds.
  81. /// </summary>
  82. /// <param name="bounds">The bounding rectangle to use for complementing the region.</param>
  83. public void Complement (Rectangle bounds)
  84. {
  85. if (bounds.IsEmpty || _rectangles.Count == 0)
  86. {
  87. _rectangles.Clear ();
  88. return;
  89. }
  90. List<Rectangle> complementRectangles = new List<Rectangle> { bounds };
  91. foreach (Rectangle rect in _rectangles)
  92. {
  93. complementRectangles = complementRectangles.SelectMany (r => SubtractRectangle (r, rect)).ToList ();
  94. }
  95. _rectangles = complementRectangles;
  96. }
  97. /// <summary>
  98. /// Creates an exact copy of the region.
  99. /// </summary>
  100. /// <returns>A new <see cref="Region"/> that is a copy of this instance.</returns>
  101. public Region Clone ()
  102. {
  103. var clone = new Region ();
  104. clone._rectangles = new (_rectangles);
  105. return clone;
  106. }
  107. /// <summary>
  108. /// Gets a bounding rectangle for the entire region.
  109. /// </summary>
  110. /// <returns>A <see cref="Rectangle"/> that bounds the region.</returns>
  111. public Rectangle GetBounds ()
  112. {
  113. if (_rectangles.Count == 0)
  114. {
  115. return Rectangle.Empty;
  116. }
  117. int left = _rectangles.Min (r => r.Left);
  118. int top = _rectangles.Min (r => r.Top);
  119. int right = _rectangles.Max (r => r.Right);
  120. int bottom = _rectangles.Max (r => r.Bottom);
  121. return new (left, top, right - left, bottom - top);
  122. }
  123. /// <summary>
  124. /// Determines whether the region is empty.
  125. /// </summary>
  126. /// <returns><c>true</c> if the region is empty; otherwise, <c>false</c>.</returns>
  127. public bool IsEmpty () { return !_rectangles.Any (); }
  128. /// <summary>
  129. /// Determines whether the specified point is contained within the region.
  130. /// </summary>
  131. /// <param name="x">The x-coordinate of the point.</param>
  132. /// <param name="y">The y-coordinate of the point.</param>
  133. /// <returns><c>true</c> if the point is contained within the region; otherwise, <c>false</c>.</returns>
  134. public bool Contains (int x, int y) { return _rectangles.Any (r => r.Contains (x, y)); }
  135. /// <summary>
  136. /// Determines whether the specified rectangle is contained within the region.
  137. /// </summary>
  138. /// <param name="rectangle">The rectangle to check for containment.</param>
  139. /// <returns><c>true</c> if the rectangle is contained within the region; otherwise, <c>false</c>.</returns>
  140. public bool Contains (Rectangle rectangle) { return _rectangles.Any (r => r.Contains (rectangle)); }
  141. /// <summary>
  142. /// Returns an array of rectangles that represent the region.
  143. /// </summary>
  144. /// <returns>An array of <see cref="Rectangle"/> objects that make up the region.</returns>
  145. public Rectangle [] GetRegionScans () { return _rectangles.ToArray (); }
  146. /// <summary>
  147. /// Merges overlapping rectangles into a minimal set of non-overlapping rectangles.
  148. /// </summary>
  149. /// <param name="rectangles">The list of rectangles to merge.</param>
  150. /// <returns>A list of merged rectangles.</returns>
  151. private List<Rectangle> MergeRectangles (List<Rectangle> rectangles)
  152. {
  153. // Simplified merging logic: this does not handle all edge cases for merging overlapping rectangles.
  154. // For a full implementation, a plane sweep algorithm or similar would be needed.
  155. List<Rectangle> merged = new List<Rectangle> (rectangles);
  156. bool mergedAny;
  157. do
  158. {
  159. mergedAny = false;
  160. for (var i = 0; i < merged.Count; i++)
  161. {
  162. for (int j = i + 1; j < merged.Count; j++)
  163. {
  164. if (merged [i].IntersectsWith (merged [j]))
  165. {
  166. merged [i] = Rectangle.Union (merged [i], merged [j]);
  167. merged.RemoveAt (j);
  168. mergedAny = true;
  169. break;
  170. }
  171. }
  172. if (mergedAny)
  173. {
  174. break;
  175. }
  176. }
  177. }
  178. while (mergedAny);
  179. return merged;
  180. }
  181. /// <summary>
  182. /// Subtracts the specified rectangle from the original rectangle, returning the resulting rectangles.
  183. /// </summary>
  184. /// <param name="original">The original rectangle.</param>
  185. /// <param name="subtract">The rectangle to subtract from the original.</param>
  186. /// <returns>An enumerable collection of resulting rectangles after subtraction.</returns>
  187. private IEnumerable<Rectangle> SubtractRectangle (Rectangle original, Rectangle subtract)
  188. {
  189. if (!original.IntersectsWith (subtract))
  190. {
  191. yield return original;
  192. yield break;
  193. }
  194. // Top segment
  195. if (original.Top < subtract.Top)
  196. {
  197. yield return new (original.Left, original.Top, original.Width, subtract.Top - original.Top);
  198. }
  199. // Bottom segment
  200. if (original.Bottom > subtract.Bottom)
  201. {
  202. yield return new (original.Left, subtract.Bottom, original.Width, original.Bottom - subtract.Bottom);
  203. }
  204. // Left segment
  205. if (original.Left < subtract.Left)
  206. {
  207. int top = Math.Max (original.Top, subtract.Top);
  208. int bottom = Math.Min (original.Bottom, subtract.Bottom);
  209. if (bottom > top)
  210. {
  211. yield return new (original.Left, top, subtract.Left - original.Left, bottom - top);
  212. }
  213. }
  214. // Right segment
  215. if (original.Right > subtract.Right)
  216. {
  217. int top = Math.Max (original.Top, subtract.Top);
  218. int bottom = Math.Min (original.Bottom, subtract.Bottom);
  219. if (bottom > top)
  220. {
  221. yield return new (subtract.Right, top, original.Right - subtract.Right, bottom - top);
  222. }
  223. }
  224. }
  225. /// <summary>
  226. /// Releases all resources used by the <see cref="Region"/>.
  227. /// </summary>
  228. public void Dispose () { _rectangles.Clear (); }
  229. }