StraightLineExtensions.cs 7.4 KB

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