StraightLineExtensionsTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. using Xunit.Abstractions;
  2. namespace UnitTests.DrawingTests;
  3. public class StraightLineExtensionsTests (ITestOutputHelper output)
  4. {
  5. [Fact]
  6. public void LineCanvasIntegrationTest ()
  7. {
  8. var lc = new LineCanvas ();
  9. lc.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Single);
  10. lc.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Single);
  11. lc.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Single);
  12. lc.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Single);
  13. OutputAssert.AssertEqual (
  14. output,
  15. @"
  16. ┌────────┐
  17. │ │
  18. │ │
  19. │ │
  20. └────────┘",
  21. $"{Environment.NewLine}{lc}"
  22. );
  23. IReadOnlyCollection<StraightLine> origLines = lc.Lines;
  24. lc = new (origLines.Exclude (Point.Empty, 10, Orientation.Horizontal));
  25. OutputAssert.AssertEqual (
  26. output,
  27. @"
  28. │ │
  29. │ │
  30. │ │
  31. └────────┘",
  32. $"{Environment.NewLine}{lc}"
  33. );
  34. lc = new (origLines.Exclude (new (0, 1), 10, Orientation.Horizontal));
  35. OutputAssert.AssertEqual (
  36. output,
  37. @"
  38. ┌────────┐
  39. │ │
  40. │ │
  41. └────────┘",
  42. $"{Environment.NewLine}{lc}"
  43. );
  44. lc = new (origLines.Exclude (new (0, 2), 10, Orientation.Horizontal));
  45. OutputAssert.AssertEqual (
  46. output,
  47. @"
  48. ┌────────┐
  49. │ │
  50. │ │
  51. └────────┘",
  52. $"{Environment.NewLine}{lc}"
  53. );
  54. lc = new (origLines.Exclude (new (0, 3), 10, Orientation.Horizontal));
  55. OutputAssert.AssertEqual (
  56. output,
  57. @"
  58. ┌────────┐
  59. │ │
  60. │ │
  61. └────────┘",
  62. $"{Environment.NewLine}{lc}"
  63. );
  64. lc = new (origLines.Exclude (new (0, 4), 10, Orientation.Horizontal));
  65. OutputAssert.AssertEqual (
  66. output,
  67. @"
  68. ┌────────┐
  69. │ │
  70. │ │
  71. │ │",
  72. $"{Environment.NewLine}{lc}"
  73. );
  74. lc = new (origLines.Exclude (Point.Empty, 10, Orientation.Vertical));
  75. OutputAssert.AssertEqual (
  76. output,
  77. @"
  78. ────────┐
  79. ────────┘",
  80. $"{Environment.NewLine}{lc}"
  81. );
  82. lc = new (origLines.Exclude (new (1, 0), 10, Orientation.Vertical));
  83. OutputAssert.AssertEqual (
  84. output,
  85. @"
  86. ┌ ───────┐
  87. │ │
  88. │ │
  89. │ │
  90. └ ───────┘",
  91. $"{Environment.NewLine}{lc}"
  92. );
  93. lc = new (origLines.Exclude (new (8, 0), 10, Orientation.Vertical));
  94. OutputAssert.AssertEqual (
  95. output,
  96. @"
  97. ┌─────── ┐
  98. │ │
  99. │ │
  100. │ │
  101. └─────── ┘",
  102. $"{Environment.NewLine}{lc}"
  103. );
  104. lc = new (origLines.Exclude (new (9, 0), 10, Orientation.Vertical));
  105. OutputAssert.AssertEqual (
  106. output,
  107. @"
  108. ┌────────
  109. └────────",
  110. $"{Environment.NewLine}{lc}"
  111. );
  112. }
  113. #region Parallel Tests
  114. [Fact]
  115. public void TestExcludeParallel_HorizontalLines_LeftOnly ()
  116. {
  117. // x=1 to x=10
  118. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  119. StraightLine [] after = new [] { l1 }
  120. // exclude x=3 to x=103
  121. .Exclude (new (3, 2), 100, Orientation.Horizontal)
  122. .ToArray ();
  123. // x=1 to x=2
  124. StraightLine afterLine = Assert.Single (after);
  125. Assert.Equal (l1.Start, afterLine.Start);
  126. Assert.Equal (2, afterLine.Length);
  127. }
  128. [Fact]
  129. public void TestExcludeParallel_HorizontalLines_RightOnly ()
  130. {
  131. // x=1 to x=10
  132. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  133. StraightLine [] after = new [] { l1 }
  134. // exclude x=0 to x=2
  135. .Exclude (new (0, 2), 3, Orientation.Horizontal)
  136. .ToArray ();
  137. // x=3 to x=10
  138. StraightLine afterLine = Assert.Single (after);
  139. Assert.Equal (3, afterLine.Start.X);
  140. Assert.Equal (2, afterLine.Start.Y);
  141. Assert.Equal (8, afterLine.Length);
  142. }
  143. [Fact]
  144. public void TestExcludeParallel_HorizontalLines_HorizontalSplit ()
  145. {
  146. // x=1 to x=10
  147. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  148. StraightLine [] after = new [] { l1 }
  149. // exclude x=4 to x=5
  150. .Exclude (new (4, 2), 2, Orientation.Horizontal)
  151. .ToArray ();
  152. // x=1 to x=3,
  153. // x=6 to x=10
  154. Assert.Equal (2, after.Length);
  155. StraightLine afterLeft = after [0];
  156. StraightLine afterRight = after [1];
  157. Assert.Equal (1, afterLeft.Start.X);
  158. Assert.Equal (2, afterLeft.Start.Y);
  159. Assert.Equal (3, afterLeft.Length);
  160. Assert.Equal (6, afterRight.Start.X);
  161. Assert.Equal (2, afterRight.Start.Y);
  162. Assert.Equal (5, afterRight.Length);
  163. }
  164. [Fact]
  165. public void TestExcludeParallel_HorizontalLines_CoverCompletely ()
  166. {
  167. // x=1 to x=10
  168. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  169. StraightLine [] after = new [] { l1 }
  170. // exclude x=4 to x=5
  171. .Exclude (new (1, 2), 10, Orientation.Horizontal)
  172. .ToArray ();
  173. Assert.Empty (after);
  174. }
  175. [Fact]
  176. public void TestExcludeParallel_VerticalLines_TopOnly ()
  177. {
  178. // y=1 to y=10
  179. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  180. StraightLine [] after = new [] { l1 }
  181. // exclude y=3 to y=103
  182. .Exclude (new (2, 3), 100, Orientation.Vertical)
  183. .ToArray ();
  184. // y=1 to y=2
  185. StraightLine afterLine = Assert.Single (after);
  186. Assert.Equal (l1.Start, afterLine.Start);
  187. Assert.Equal (2, afterLine.Length);
  188. }
  189. [Fact]
  190. public void TestExcludeParallel_HorizontalLines_BottomOnly ()
  191. {
  192. // y=1 to y=10
  193. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  194. StraightLine [] after = new [] { l1 }
  195. // exclude y=0 to y=2
  196. .Exclude (new (2, 0), 3, Orientation.Vertical)
  197. .ToArray ();
  198. // y=3 to y=10
  199. StraightLine afterLine = Assert.Single (after);
  200. Assert.Equal (3, afterLine.Start.Y);
  201. Assert.Equal (2, afterLine.Start.X);
  202. Assert.Equal (8, afterLine.Length);
  203. }
  204. [Fact]
  205. public void TestExcludeParallel_VerticalLines_VerticalSplit ()
  206. {
  207. // y=1 to y=10
  208. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  209. StraightLine [] after = new [] { l1 }
  210. // exclude y=4 to y=5
  211. .Exclude (new (2, 4), 2, Orientation.Vertical)
  212. .ToArray ();
  213. // y=1 to y=3,
  214. // y=6 to y=10
  215. Assert.Equal (2, after.Length);
  216. StraightLine afterLeft = after [0];
  217. StraightLine afterRight = after [1];
  218. Assert.Equal (1, afterLeft.Start.Y);
  219. Assert.Equal (2, afterLeft.Start.X);
  220. Assert.Equal (3, afterLeft.Length);
  221. Assert.Equal (6, afterRight.Start.Y);
  222. Assert.Equal (2, afterRight.Start.X);
  223. Assert.Equal (5, afterRight.Length);
  224. }
  225. [Fact]
  226. public void TestExcludeParallel_VerticalLines_CoverCompletely ()
  227. {
  228. // y=1 to y=10
  229. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  230. StraightLine [] after = new [] { l1 }
  231. // exclude y=4 to y=5
  232. .Exclude (new (2, 1), 10, Orientation.Vertical)
  233. .ToArray ();
  234. Assert.Empty (after);
  235. }
  236. #endregion
  237. #region Perpendicular Intersection Tests
  238. [Fact]
  239. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_Splits ()
  240. {
  241. // x=1 to x=10
  242. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  243. StraightLine [] after = new [] { l1 }
  244. // exclude x=3 y=0-10
  245. .Exclude (new (3, 0), 10, Orientation.Vertical)
  246. .ToArray ();
  247. // x=1 to x=2,
  248. // x=4 to x=10
  249. Assert.Equal (2, after.Length);
  250. StraightLine afterLeft = after [0];
  251. StraightLine afterRight = after [1];
  252. Assert.Equal (1, afterLeft.Start.X);
  253. Assert.Equal (2, afterLeft.Start.Y);
  254. Assert.Equal (2, afterLeft.Length);
  255. Assert.Equal (4, afterRight.Start.X);
  256. Assert.Equal (2, afterRight.Start.Y);
  257. Assert.Equal (7, afterRight.Length);
  258. }
  259. [Fact]
  260. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_ClipLeft ()
  261. {
  262. // x=1 to x=10
  263. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  264. StraightLine [] after = new [] { l1 }
  265. // exclude x=1 y=0-10
  266. .Exclude (new (1, 0), 10, Orientation.Vertical)
  267. .ToArray ();
  268. // x=2 to x=10,
  269. StraightLine lineAfter = Assert.Single (after);
  270. Assert.Equal (2, lineAfter.Start.X);
  271. Assert.Equal (2, lineAfter.Start.Y);
  272. Assert.Equal (9, lineAfter.Length);
  273. }
  274. [Fact]
  275. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_ClipRight ()
  276. {
  277. // x=1 to x=10
  278. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  279. StraightLine [] after = new [] { l1 }
  280. // exclude x=10 y=0-10
  281. .Exclude (new (10, 0), 10, Orientation.Vertical)
  282. .ToArray ();
  283. // x=1 to x=9,
  284. StraightLine lineAfter = Assert.Single (after);
  285. Assert.Equal (1, lineAfter.Start.X);
  286. Assert.Equal (2, lineAfter.Start.Y);
  287. Assert.Equal (9, lineAfter.Length);
  288. }
  289. [Fact]
  290. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_MissLeft ()
  291. {
  292. // x=1 to x=10
  293. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  294. StraightLine [] after = new [] { l1 }
  295. // exclude x=0 y=0-10
  296. .Exclude (Point.Empty, 10, Orientation.Vertical)
  297. .ToArray ();
  298. // Exclusion line is too far to the left so hits nothing
  299. Assert.Same (Assert.Single (after), l1);
  300. }
  301. [Fact]
  302. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_MissRight ()
  303. {
  304. // x=1 to x=10
  305. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  306. StraightLine [] after = new [] { l1 }
  307. // exclude x=11 y=0-10
  308. .Exclude (new (11, 0), 10, Orientation.Vertical)
  309. .ToArray ();
  310. // Exclusion line is too far to the right so hits nothing
  311. Assert.Same (Assert.Single (after), l1);
  312. }
  313. [Fact]
  314. public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_ClipTop ()
  315. {
  316. // y=1 to y=10
  317. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  318. StraightLine [] after = new [] { l1 }
  319. // exclude y=1 x=0-10
  320. .Exclude (new (0, 1), 10, Orientation.Horizontal)
  321. .ToArray ();
  322. // y=2 to y=10,
  323. StraightLine lineAfter = Assert.Single (after);
  324. Assert.Equal (2, lineAfter.Start.Y);
  325. Assert.Equal (2, lineAfter.Start.X);
  326. Assert.Equal (9, lineAfter.Length);
  327. }
  328. [Fact]
  329. public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_ClipBottom ()
  330. {
  331. // y=1 to y=10
  332. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  333. StraightLine [] after = new [] { l1 }
  334. // exclude y=10 x=0-10
  335. .Exclude (new (0, 10), 10, Orientation.Horizontal)
  336. .ToArray ();
  337. // y=1 to y=9,
  338. StraightLine lineAfter = Assert.Single (after);
  339. Assert.Equal (1, lineAfter.Start.Y);
  340. Assert.Equal (2, lineAfter.Start.X);
  341. Assert.Equal (9, lineAfter.Length);
  342. }
  343. [Fact]
  344. public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_MissTop ()
  345. {
  346. // y=1 to y=10
  347. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  348. StraightLine [] after = new [] { l1 }
  349. // exclude y=0 x=0-10
  350. .Exclude (Point.Empty, 10, Orientation.Horizontal)
  351. .ToArray ();
  352. // Exclusion line is too far above so hits nothing
  353. Assert.Same (Assert.Single (after), l1);
  354. }
  355. [Fact]
  356. public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_MissBottom ()
  357. {
  358. // y=1 to y=10
  359. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  360. StraightLine [] after = new [] { l1 }
  361. // exclude y=11 x=0-10
  362. .Exclude (new (0, 11), 10, Orientation.Horizontal)
  363. .ToArray ();
  364. // Exclusion line is too far to the right so hits nothing
  365. Assert.Same (Assert.Single (after), l1);
  366. }
  367. #endregion Perpendicular Intersection Tests
  368. }