StraightLineExtensions.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. namespace Terminal.Gui;
  2. /// <summary>Extension methods for <see cref="StraightLine"/> (including collections).</summary>
  3. public static class StraightLineExtensions
  4. {
  5. /// <summary>
  6. /// Splits or removes all lines in the <paramref name="collection"/> such that none cover the given exclusion
  7. /// area.
  8. /// </summary>
  9. /// <param name="collection">Lines to adjust</param>
  10. /// <param name="start">First point to remove from collection</param>
  11. /// <param name="length">The number of sequential points to exclude</param>
  12. /// <param name="orientation">Orientation of the exclusion line</param>
  13. /// <returns></returns>
  14. public static IEnumerable<StraightLine> Exclude (
  15. this IEnumerable<StraightLine> collection,
  16. Point start,
  17. int length,
  18. Orientation orientation
  19. )
  20. {
  21. List<StraightLine> toReturn = new ();
  22. if (length == 0)
  23. {
  24. return collection;
  25. }
  26. foreach (StraightLine l in collection)
  27. {
  28. if (l.Length == 0)
  29. {
  30. toReturn.Add (l);
  31. continue;
  32. }
  33. // lines are parallel. For any straight line one axis (x or y) is constant
  34. // e.g. Horizontal lines have constant y
  35. int econstPoint = orientation == Orientation.Horizontal ? start.Y : start.X;
  36. int lconstPoint = l.Orientation == Orientation.Horizontal ? l.Start.Y : l.Start.X;
  37. // For the varying axis what is the max/mins
  38. // i.e. points on horizontal lines vary by x, vertical lines vary by y
  39. int eDiffMin = GetLineStartOnDiffAxis (start, length, orientation);
  40. int eDiffMax = GetLineEndOnDiffAxis (start, length, orientation);
  41. int lDiffMin = GetLineStartOnDiffAxis (l.Start, l.Length, l.Orientation);
  42. int lDiffMax = GetLineEndOnDiffAxis (l.Start, l.Length, l.Orientation);
  43. // line is parallel to exclusion
  44. if (l.Orientation == orientation)
  45. {
  46. // Do the parallel lines share constant plane
  47. if (econstPoint != lconstPoint)
  48. {
  49. // No, so no way they overlap
  50. toReturn.Add (l);
  51. }
  52. else
  53. {
  54. if (lDiffMax < eDiffMin)
  55. {
  56. // Line ends before exclusion starts
  57. toReturn.Add (l);
  58. }
  59. else if (lDiffMin > eDiffMax)
  60. {
  61. // Line starts after exclusion ends
  62. toReturn.Add (l);
  63. }
  64. else
  65. {
  66. //lines overlap!
  67. // Is there a bit we can keep on the left?
  68. if (lDiffMin < eDiffMin)
  69. {
  70. // Create line up to exclusion point
  71. int from = lDiffMin;
  72. int len = eDiffMin - lDiffMin;
  73. if (len > 0)
  74. {
  75. toReturn.Add (CreateLineFromDiff (l, from, len));
  76. }
  77. }
  78. // Is there a bit we can keep on the right?
  79. if (lDiffMax > eDiffMax)
  80. {
  81. // Create line up to exclusion point
  82. int from = eDiffMax + 1;
  83. int len = lDiffMax - eDiffMax;
  84. if (len > 0)
  85. {
  86. // A single line with length 1 and -1 are the same (fills only the single cell)
  87. // They differ only in how they join to other lines (i.e. to create corners)
  88. // Using negative for the later half of the line ensures line joins in a way
  89. // consistent with its pre-snipped state.
  90. if (len == 1)
  91. {
  92. len = -1;
  93. }
  94. toReturn.Add (CreateLineFromDiff (l, from, len));
  95. }
  96. }
  97. }
  98. }
  99. }
  100. else
  101. {
  102. // line is perpendicular to exclusion
  103. // Does the constant plane of the exclusion appear within the differing plane of the line?
  104. if (econstPoint >= lDiffMin && econstPoint <= lDiffMax)
  105. {
  106. // Yes, e.g. Vertical exclusion's x is within xmin/xmax of the horizontal line
  107. // Vice versa must also be true
  108. // for example there is no intersection if the vertical exclusion line does not
  109. // stretch down far enough to reach the line
  110. if (lconstPoint >= eDiffMin && lconstPoint <= eDiffMax)
  111. {
  112. // Perpendicular intersection occurs here
  113. Point intersection = l.Orientation == Orientation.Horizontal
  114. ? new Point (econstPoint, lconstPoint)
  115. : new Point (lconstPoint, econstPoint);
  116. // To snip out this single point we will use a recursive call
  117. // snipping 1 length along the orientation of l (i.e. parallel)
  118. toReturn.AddRange (new [] { l }.Exclude (intersection, 1, l.Orientation));
  119. }
  120. else
  121. {
  122. // No intersection
  123. toReturn.Add (l);
  124. }
  125. }
  126. else
  127. {
  128. // Lines do not intersect
  129. toReturn.Add (l);
  130. }
  131. }
  132. }
  133. return toReturn;
  134. }
  135. /// <summary>
  136. /// Creates a new line which is part of <paramref name="l"/> from the point on the varying axis
  137. /// <paramref name="from"/> to <paramref name="length"/>. Horizontal lines have points that vary by x while vertical
  138. /// lines have points that vary by y
  139. /// </summary>
  140. /// <param name="l">Line to create sub part from</param>
  141. /// <param name="from">Point on varying axis to start at</param>
  142. /// <param name="length">Length of line to return</param>
  143. /// <returns>The new line</returns>
  144. private static StraightLine CreateLineFromDiff (StraightLine l, int from, int length)
  145. {
  146. var start = new Point (
  147. l.Orientation == Orientation.Horizontal ? from : l.Start.X,
  148. l.Orientation == Orientation.Horizontal ? l.Start.Y : from
  149. );
  150. return new StraightLine (start, length, l.Orientation, l.Style, l.Attribute);
  151. }
  152. /// <summary>
  153. /// <para>
  154. /// Calculates the single digit point where a line ends on the differing axis i.e. the maximum (controlling for
  155. /// negative lengths).
  156. /// </para>
  157. /// <para>
  158. /// For lines with <see cref="Orientation.Horizontal"/> this is an x coordinate. For lines that are
  159. /// <see cref="Orientation.Vertical"/> this is a y coordinate.
  160. /// </para>
  161. /// </summary>
  162. /// <param name="start">Where the line starts</param>
  163. /// <param name="length">Length of the line</param>
  164. /// <param name="orientation">Orientation of the line</param>
  165. /// <returns>The maximum x or y (whichever is differing) point on the line, controlling for negative lengths. </returns>
  166. private static int GetLineEndOnDiffAxis (Point start, int length, Orientation orientation)
  167. {
  168. if (length == 0)
  169. {
  170. throw new ArgumentException ("0 length lines are not supported", nameof (length));
  171. }
  172. int sub = length > 0 ? 1 : -1;
  173. if (orientation == Orientation.Vertical)
  174. {
  175. // Points on line differ by y
  176. return Math.Max (start.Y + length - sub, start.Y);
  177. }
  178. // Points on line differ by x
  179. return Math.Max (start.X + length - sub, start.X);
  180. }
  181. /// <summary>
  182. /// <para>
  183. /// Calculates the single digit point where a line starts on the differing axis i.e. the minimum (controlling for
  184. /// negative lengths).
  185. /// </para>
  186. /// <para>
  187. /// For lines with <see cref="Orientation.Horizontal"/> this is an x coordinate. For lines that are
  188. /// <see cref="Orientation.Vertical"/> this is a y coordinate.
  189. /// </para>
  190. /// </summary>
  191. /// <param name="start">Where the line starts</param>
  192. /// <param name="length">Length of the line</param>
  193. /// <param name="orientation">Orientation of the line</param>
  194. /// <returns>The minimum x or y (whichever is differing) point on the line, controlling for negative lengths. </returns>
  195. private static int GetLineStartOnDiffAxis (Point start, int length, Orientation orientation)
  196. {
  197. if (length == 0)
  198. {
  199. throw new ArgumentException ("0 length lines are not supported", nameof (length));
  200. }
  201. int sub = length > 0 ? 1 : -1;
  202. if (orientation == Orientation.Vertical)
  203. {
  204. // Points on line differ by y
  205. return Math.Min (start.Y + length - sub, start.Y);
  206. }
  207. // Points on line differ by x
  208. return Math.Min (start.X + length - sub, start.X);
  209. }
  210. }