StraightLineExtensionsTests.cs 15 KB

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