StraightLineExtensions.cs 9.6 KB

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