StraightLineCanvas.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Terminal.Gui.Graphs {
  5. /// <summary>
  6. /// Facilitates box drawing and line intersection detection
  7. /// and rendering.
  8. /// </summary>
  9. public class StraightLineCanvas {
  10. private List<StraightLine> lines = new List<StraightLine> ();
  11. private ConsoleDriver driver;
  12. public StraightLineCanvas (ConsoleDriver driver)
  13. {
  14. this.driver = driver;
  15. }
  16. /// <summary>
  17. /// Add a new line to the canvas starting at <paramref name="from"/>.
  18. /// Use positive <paramref name="length"/> for Right and negative for Left
  19. /// when <see cref="Orientation"/> is <see cref="Orientation.Horizontal"/>.
  20. /// Use positive <paramref name="length"/> for Down and negative for Up
  21. /// when <see cref="Orientation"/> is <see cref="Orientation.Vertical"/>.
  22. /// </summary>
  23. /// <param name="from">Starting point.</param>
  24. /// <param name="length">Length of line. 0 for a dot.
  25. /// Positive for Down/Right. Negative for Up/Left.</param>
  26. /// <param name="orientation">Direction of the line.</param>
  27. public void AddLine (Point from, int length, Orientation orientation, BorderStyle style)
  28. {
  29. lines.Add (new StraightLine (from, length, orientation, style));
  30. }
  31. /// <summary>
  32. /// Evaluate all currently defined lines that lie within
  33. /// <paramref name="inArea"/> and generate a 'bitmap' that
  34. /// shows what characters (if any) should be rendered at each
  35. /// point so that all lines connect up correctly with appropriate
  36. /// intersection symbols.
  37. /// <returns></returns>
  38. /// </summary>
  39. /// <param name="inArea"></param>
  40. /// <returns>Map as 2D array where first index is rows and second is column</returns>
  41. public Rune? [,] GenerateImage (Rect inArea)
  42. {
  43. Rune? [,] canvas = new Rune? [inArea.Height, inArea.Width];
  44. // walk through each pixel of the bitmap
  45. for (int y = 0; y < inArea.Height; y++) {
  46. for (int x = 0; x < inArea.Width; x++) {
  47. var intersects = lines
  48. .Select (l => l.Intersects (x, y))
  49. .Where (i => i != null)
  50. .ToArray ();
  51. // TODO: use Driver and LineStyle to map
  52. canvas [y, x] = GetRuneForIntersects (intersects);
  53. }
  54. }
  55. return canvas;
  56. }
  57. private Rune? GetRuneForIntersects (IntersectionDefinition [] intersects)
  58. {
  59. if (!intersects.Any ())
  60. return null;
  61. var runeType = GetRuneTypeForIntersects (intersects);
  62. switch (runeType) {
  63. case IntersectionRuneType.None: return null;
  64. case IntersectionRuneType.Dot: return (Rune)'.';
  65. case IntersectionRuneType.ULCorner: return driver.ULCorner;
  66. case IntersectionRuneType.URCorner: return driver.URCorner;
  67. case IntersectionRuneType.LLCorner: return driver.LLCorner;
  68. case IntersectionRuneType.LRCorner: return driver.LRCorner;
  69. case IntersectionRuneType.TopTee: return driver.TopTee;
  70. case IntersectionRuneType.BottomTee: return driver.BottomTee;
  71. case IntersectionRuneType.RightTee: return driver.RightTee;
  72. case IntersectionRuneType.LeftTee: return driver.LeftTee;
  73. case IntersectionRuneType.Crosshair: return '┼';
  74. case IntersectionRuneType.HLine: return driver.HLine;
  75. case IntersectionRuneType.VLine: return driver.VLine;
  76. default: throw new ArgumentOutOfRangeException (nameof (runeType));
  77. }
  78. }
  79. private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition [] intersects)
  80. {
  81. // ignore dots
  82. intersects = intersects.Where (i => i.Type != IntersectionType.Dot).ToArray ();
  83. var set = new HashSet<IntersectionType>(intersects.Select(i=>i.Type));
  84. #region Crosshair Conditions
  85. if (Has (set,
  86. IntersectionType.PassOverHorizontal,
  87. IntersectionType.PassOverVertical
  88. )) {
  89. return IntersectionRuneType.Crosshair;
  90. }
  91. if (Has (set,
  92. IntersectionType.PassOverVertical,
  93. IntersectionType.StartLeft,
  94. IntersectionType.StartRight
  95. )) {
  96. return IntersectionRuneType.Crosshair;
  97. }
  98. if (Has (set,
  99. IntersectionType.PassOverHorizontal,
  100. IntersectionType.StartUp,
  101. IntersectionType.StartDown
  102. )) {
  103. return IntersectionRuneType.Crosshair;
  104. }
  105. if (Has (set,
  106. IntersectionType.StartLeft,
  107. IntersectionType.StartRight,
  108. IntersectionType.StartUp,
  109. IntersectionType.StartDown)) {
  110. return IntersectionRuneType.Crosshair;
  111. }
  112. #endregion
  113. #region Corner Conditions
  114. if (Exactly (set,
  115. IntersectionType.StartRight,
  116. IntersectionType.StartDown)) {
  117. return IntersectionRuneType.ULCorner;
  118. }
  119. if (Exactly (set,
  120. IntersectionType.StartLeft,
  121. IntersectionType.StartDown)) {
  122. return IntersectionRuneType.URCorner;
  123. }
  124. if (Exactly (set,
  125. IntersectionType.StartUp,
  126. IntersectionType.StartLeft)) {
  127. return IntersectionRuneType.LRCorner;
  128. }
  129. if (Exactly (set,
  130. IntersectionType.StartUp,
  131. IntersectionType.StartRight)) {
  132. return IntersectionRuneType.LLCorner;
  133. }
  134. #endregion Corner Conditions
  135. #region T Conditions
  136. if (Has (set,
  137. IntersectionType.PassOverHorizontal,
  138. IntersectionType.StartDown)) {
  139. return IntersectionRuneType.TopTee;
  140. }
  141. if (Has (set,
  142. IntersectionType.StartRight,
  143. IntersectionType.StartLeft,
  144. IntersectionType.StartDown)) {
  145. return IntersectionRuneType.TopTee;
  146. }
  147. if (Has (set,
  148. IntersectionType.PassOverHorizontal,
  149. IntersectionType.StartUp)) {
  150. return IntersectionRuneType.BottomTee;
  151. }
  152. if (Has (set,
  153. IntersectionType.StartRight,
  154. IntersectionType.StartLeft,
  155. IntersectionType.StartUp)) {
  156. return IntersectionRuneType.BottomTee;
  157. }
  158. if (Has (set,
  159. IntersectionType.PassOverVertical,
  160. IntersectionType.StartRight)) {
  161. return IntersectionRuneType.LeftTee;
  162. }
  163. if (Has (set,
  164. IntersectionType.StartRight,
  165. IntersectionType.StartDown,
  166. IntersectionType.StartUp)) {
  167. return IntersectionRuneType.LeftTee;
  168. }
  169. if (Has (set,
  170. IntersectionType.PassOverVertical,
  171. IntersectionType.StartLeft)) {
  172. return IntersectionRuneType.RightTee;
  173. }
  174. if (Has (set,
  175. IntersectionType.StartLeft,
  176. IntersectionType.StartDown,
  177. IntersectionType.StartUp)) {
  178. return IntersectionRuneType.RightTee;
  179. }
  180. #endregion
  181. if(All(intersects, Orientation.Horizontal)) {
  182. return IntersectionRuneType.HLine;
  183. }
  184. if (All (intersects, Orientation.Vertical)) {
  185. return IntersectionRuneType.VLine;
  186. }
  187. return IntersectionRuneType.Dot;
  188. }
  189. private bool All (IntersectionDefinition[] intersects, Orientation orientation)
  190. {
  191. return intersects.All (i=>i.Line.Orientation == orientation);
  192. }
  193. /// <summary>
  194. /// Returns true if the <paramref name="intersects"/> collection has all the <paramref name="types"/>
  195. /// specified (i.e. AND).
  196. /// </summary>
  197. /// <param name="intersects"></param>
  198. /// <param name="types"></param>
  199. /// <returns></returns>
  200. private bool Has (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  201. {
  202. return types.All (t => intersects.Contains (t));
  203. }
  204. /// <summary>
  205. /// Returns true if all requested <paramref name="types"/> appear in <paramref name="intersects"/>
  206. /// and there are no additional <see cref="IntersectionRuneType"/>
  207. /// </summary>
  208. /// <param name="intersects"></param>
  209. /// <param name="types"></param>
  210. /// <returns></returns>
  211. private bool Exactly (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  212. {
  213. return intersects.SetEquals (types);
  214. }
  215. class IntersectionDefinition {
  216. /// <summary>
  217. /// The point at which the intersection happens
  218. /// </summary>
  219. public Point Point { get; }
  220. /// <summary>
  221. /// Defines how <see cref="Line"/> position relates
  222. /// to <see cref="Point"/>.
  223. /// </summary>
  224. public IntersectionType Type { get; }
  225. /// <summary>
  226. /// The line that intersects <see cref="Point"/>
  227. /// </summary>
  228. public StraightLine Line { get; }
  229. public IntersectionDefinition (Point point, IntersectionType type, StraightLine line)
  230. {
  231. Point = point;
  232. Type = type;
  233. Line = line;
  234. }
  235. }
  236. /// <summary>
  237. /// The type of Rune that we will use before considering
  238. /// double width, curved borders etc
  239. /// </summary>
  240. enum IntersectionRuneType {
  241. None,
  242. Dot,
  243. ULCorner,
  244. URCorner,
  245. LLCorner,
  246. LRCorner,
  247. TopTee,
  248. BottomTee,
  249. RightTee,
  250. LeftTee,
  251. Crosshair,
  252. HLine,
  253. VLine,
  254. }
  255. enum IntersectionType {
  256. /// <summary>
  257. /// There is no intersection
  258. /// </summary>
  259. None,
  260. /// <summary>
  261. /// A line passes directly over this point traveling along
  262. /// the horizontal axis
  263. /// </summary>
  264. PassOverHorizontal,
  265. /// <summary>
  266. /// A line passes directly over this point traveling along
  267. /// the vertical axis
  268. /// </summary>
  269. PassOverVertical,
  270. /// <summary>
  271. /// A line starts at this point and is traveling up
  272. /// </summary>
  273. StartUp,
  274. /// <summary>
  275. /// A line starts at this point and is traveling right
  276. /// </summary>
  277. StartRight,
  278. /// <summary>
  279. /// A line starts at this point and is traveling down
  280. /// </summary>
  281. StartDown,
  282. /// <summary>
  283. /// A line starts at this point and is traveling left
  284. /// </summary>
  285. StartLeft,
  286. /// <summary>
  287. /// A line exists at this point who has 0 length
  288. /// </summary>
  289. Dot
  290. }
  291. class StraightLine {
  292. public Point Start { get; }
  293. public int Length { get; }
  294. public Orientation Orientation { get; }
  295. public BorderStyle Style { get; }
  296. public StraightLine (Point start, int length, Orientation orientation, BorderStyle style)
  297. {
  298. this.Start = start;
  299. this.Length = length;
  300. this.Orientation = orientation;
  301. this.Style = style;
  302. }
  303. internal IntersectionDefinition Intersects (int x, int y)
  304. {
  305. if (IsDot ()) {
  306. if (StartsAt (x, y)) {
  307. return new IntersectionDefinition (Start, IntersectionType.Dot, this);
  308. } else {
  309. return null;
  310. }
  311. }
  312. switch (Orientation) {
  313. case Orientation.Horizontal: return IntersectsHorizontally (x, y);
  314. case Orientation.Vertical: return IntersectsVertically (x, y);
  315. default: throw new ArgumentOutOfRangeException (nameof (Orientation));
  316. }
  317. }
  318. private IntersectionDefinition IntersectsHorizontally (int x, int y)
  319. {
  320. if (Start.Y != y) {
  321. return null;
  322. } else {
  323. if (StartsAt (x, y)) {
  324. return new IntersectionDefinition (
  325. Start,
  326. Length < 0 ? IntersectionType.StartLeft : IntersectionType.StartRight,
  327. this
  328. );
  329. }
  330. if (EndsAt (x, y)) {
  331. return new IntersectionDefinition (
  332. Start,
  333. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  334. this
  335. );
  336. } else {
  337. var xmin = Math.Min (Start.X, Start.X + Length);
  338. var xmax = Math.Max (Start.X, Start.X + Length);
  339. if (xmin < x && xmax > x) {
  340. return new IntersectionDefinition (
  341. new Point (x, y),
  342. IntersectionType.PassOverHorizontal,
  343. this
  344. );
  345. }
  346. }
  347. return null;
  348. }
  349. }
  350. private IntersectionDefinition IntersectsVertically (int x, int y)
  351. {
  352. if (Start.X != x) {
  353. return null;
  354. } else {
  355. if (StartsAt (x, y)) {
  356. return new IntersectionDefinition (
  357. Start,
  358. Length < 0 ? IntersectionType.StartUp : IntersectionType.StartDown,
  359. this
  360. );
  361. }
  362. if (EndsAt (x, y)) {
  363. return new IntersectionDefinition (
  364. Start,
  365. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  366. this
  367. );
  368. } else {
  369. var ymin = Math.Min (Start.Y, Start.Y + Length);
  370. var ymax = Math.Max (Start.Y, Start.Y + Length);
  371. if (ymin < y && ymax > y) {
  372. return new IntersectionDefinition (
  373. new Point (x, y),
  374. IntersectionType.PassOverVertical,
  375. this
  376. );
  377. }
  378. }
  379. return null;
  380. }
  381. }
  382. private bool EndsAt (int x, int y)
  383. {
  384. if (Orientation == Orientation.Horizontal) {
  385. return Start.X + Length == x && Start.Y == y;
  386. }
  387. return Start.X == x && Start.Y + Length == y;
  388. }
  389. private bool StartsAt (int x, int y)
  390. {
  391. return Start.X == x && Start.Y == y;
  392. }
  393. private bool IsDot ()
  394. {
  395. return Length == 0;
  396. }
  397. }
  398. }
  399. }