Region.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 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. /// Offsets all rectangles in the region by the specified amounts.
  148. /// </summary>
  149. /// <param name="offsetX">The amount to offset along the x-axis.</param>
  150. /// <param name="offsetY">The amount to offset along the y-axis.</param>
  151. public void Offset (int offsetX, int offsetY)
  152. {
  153. for (int i = 0; i < _rectangles.Count; i++)
  154. {
  155. var rect = _rectangles [i];
  156. _rectangles [i] = new Rectangle (rect.Left + offsetX, rect.Top + offsetY, rect.Width, rect.Height);
  157. }
  158. }
  159. /// <summary>
  160. /// Merges overlapping rectangles into a minimal set of non-overlapping rectangles.
  161. /// </summary>
  162. /// <param name="rectangles">The list of rectangles to merge.</param>
  163. /// <returns>A list of merged rectangles.</returns>
  164. private List<Rectangle> MergeRectangles (List<Rectangle> rectangles)
  165. {
  166. // Simplified merging logic: this does not handle all edge cases for merging overlapping rectangles.
  167. // For a full implementation, a plane sweep algorithm or similar would be needed.
  168. List<Rectangle> merged = new List<Rectangle> (rectangles);
  169. bool mergedAny;
  170. do
  171. {
  172. mergedAny = false;
  173. for (var i = 0; i < merged.Count; i++)
  174. {
  175. for (int j = i + 1; j < merged.Count; j++)
  176. {
  177. if (merged [i].IntersectsWith (merged [j]))
  178. {
  179. merged [i] = Rectangle.Union (merged [i], merged [j]);
  180. merged.RemoveAt (j);
  181. mergedAny = true;
  182. break;
  183. }
  184. }
  185. if (mergedAny)
  186. {
  187. break;
  188. }
  189. }
  190. }
  191. while (mergedAny);
  192. return merged;
  193. }
  194. /// <summary>
  195. /// Subtracts the specified rectangle from the original rectangle, returning the resulting rectangles.
  196. /// </summary>
  197. /// <param name="original">The original rectangle.</param>
  198. /// <param name="subtract">The rectangle to subtract from the original.</param>
  199. /// <returns>An enumerable collection of resulting rectangles after subtraction.</returns>
  200. private IEnumerable<Rectangle> SubtractRectangle (Rectangle original, Rectangle subtract)
  201. {
  202. if (!original.IntersectsWith (subtract))
  203. {
  204. yield return original;
  205. yield break;
  206. }
  207. // Top segment
  208. if (original.Top < subtract.Top)
  209. {
  210. yield return new (original.Left, original.Top, original.Width, subtract.Top - original.Top);
  211. }
  212. // Bottom segment
  213. if (original.Bottom > subtract.Bottom)
  214. {
  215. yield return new (original.Left, subtract.Bottom, original.Width, original.Bottom - subtract.Bottom);
  216. }
  217. // Left segment
  218. if (original.Left < subtract.Left)
  219. {
  220. int top = Math.Max (original.Top, subtract.Top);
  221. int bottom = Math.Min (original.Bottom, subtract.Bottom);
  222. if (bottom > top)
  223. {
  224. yield return new (original.Left, top, subtract.Left - original.Left, bottom - top);
  225. }
  226. }
  227. // Right segment
  228. if (original.Right > subtract.Right)
  229. {
  230. int top = Math.Max (original.Top, subtract.Top);
  231. int bottom = Math.Min (original.Bottom, subtract.Bottom);
  232. if (bottom > top)
  233. {
  234. yield return new (subtract.Right, top, original.Right - subtract.Right, bottom - top);
  235. }
  236. }
  237. }
  238. /// <summary>
  239. /// Releases all resources used by the <see cref="Region"/>.
  240. /// </summary>
  241. public void Dispose () { _rectangles.Clear (); }
  242. }