StraightLineExtensions.cs 9.6 KB

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