From 6fe0ef03514f18cea0c30dd353ff8b0ed1a1b4b7 Mon Sep 17 00:00:00 2001 From: BobLd Date: Mon, 17 Feb 2020 14:39:55 +0000 Subject: [PATCH] Add line tests and improve GeometryExtensions --- .../Geometry/PdfLineTests.cs | 381 +++++++++++++++++- .../Geometry/GeometryExtensions.cs | 49 +-- 2 files changed, 393 insertions(+), 37 deletions(-) diff --git a/src/UglyToad.PdfPig.Tests/Geometry/PdfLineTests.cs b/src/UglyToad.PdfPig.Tests/Geometry/PdfLineTests.cs index 0b94e072..712e61a4 100644 --- a/src/UglyToad.PdfPig.Tests/Geometry/PdfLineTests.cs +++ b/src/UglyToad.PdfPig.Tests/Geometry/PdfLineTests.cs @@ -7,6 +7,10 @@ public class PdfLineTests { + private static readonly DoubleComparer DoubleComparer = new DoubleComparer(3); + private static readonly DoubleComparer PreciseDoubleComparer = new DoubleComparer(6); + private static readonly PointComparer PointComparer = new PointComparer(DoubleComparer); + [Fact] public void OriginIsZero() { @@ -21,18 +25,315 @@ [Fact] public void Length() { - var line = new PdfLine(2, 1, 6, 4); - Assert.Equal(5d, line.Length); - - var line2 = new PdfLine(-2, 8, -7, -5); - Assert.Equal(13.93d, Math.Round(line2.Length, 2)); + Assert.Equal(578.0238169967487, new PdfLine(772.7935893651529, 299.87394515637465, 233.21554238153897, 92.58896203207878).Length, 6); + Assert.Equal(732.2945881804645, new PdfLine(194.8448667191307, 319.9187761462183, 925.189355248612, 373.32562919904933).Length, 6); + Assert.Equal(836.8198676151344, new PdfLine(854.5271180179672, 571.4252436090782, 20.526363588710296, 502.7941103906799).Length, 6); + Assert.Equal(598.3011468750585, new PdfLine(670.0839162642875, 261.3083276757181, 972.8798288884752, 777.3305148441533).Length, 6); + Assert.Equal(276.7760632587749, new PdfLine(957.852909406517, 949.260084067982, 682.9812789799355, 981.6726739006094).Length, 6); + Assert.Equal(314.41965030454816, new PdfLine(147.86567208537681, 382.7888325277287, 272.5846931925505, 94.1629808240001).Length, 6); + Assert.Equal(174.13822965911558, new PdfLine(147.82640833766558, 875.9676661144384, 290.3606045554035, 775.9270446780725).Length, 6); + Assert.Equal(297.29917790336884, new PdfLine(638.6170220470308, 768.2362490341656, 783.2867033379487, 508.5104619101235).Length, 6); + Assert.Equal(839.2831421489764, new PdfLine(465.6101221171739, 787.9959266813818, 900.0525086500069, 69.90400674416696).Length, 6); + Assert.Equal(607.3221473016613, new PdfLine(673.1433266782943, 553.7033541157308, 107.5182083942644, 774.8558267884955).Length, 6); + Assert.Equal(454.55607010141694, new PdfLine(386.2113257237346, 333.90288712225333, 591.6938420537859, 739.3633122548604).Length, 6); + Assert.Equal(626.11440600493, new PdfLine(479.4067404011179, 781.3982869554078, 472.4226714313526, 155.32283448379735).Length, 6); + Assert.Equal(615.792237887848, new PdfLine(868.1899797889077, 125.67193509355945, 280.9371277792105, 310.96674896971933).Length, 6); + Assert.Equal(142.5850044391949, new PdfLine(470.1012782013631, 528.9274203113266, 472.256082185328, 671.4961416730472).Length, 6); + Assert.Equal(487.4183136781511, new PdfLine(31.98887876729284, 555.9383587303098, 519.063658539301, 574.2351185227507).Length, 6); + Assert.Equal(840.2681584074232, new PdfLine(840.272207349764, 81.7172212744205, 524.8912200782769, 860.5531545113664).Length, 6); + Assert.Equal(818.356052074226, new PdfLine(119.5826241579242, 615.1292410442892, 901.9316823443093, 375.05304663636883).Length, 6); + Assert.Equal(392.3489708735156, new PdfLine(489.55892955681145, 595.6045336298662, 490.9073801378142, 987.9511872750036).Length, 6); + Assert.Equal(595.9236956932385, new PdfLine(174.13216497246796, 21.05642885679826, 663.999686747385, 360.40118305050714).Length, 6); + Assert.Equal(648.6437173566884, new PdfLine(143.4958123304927, 27.0537943602297, 792.1223500942808, 31.774660262229972).Length, 6); + Assert.Equal(193.5135366822075, new PdfLine(589.854666457571, 77.67384122398701, 412.2699278651426, 154.55784149917383).Length, 6); + Assert.Equal(176.18452804214598, new PdfLine(402.3919544552986, 655.8678416433016, 568.7872692682821, 713.7780059473497).Length, 6); + Assert.Equal(419.7063148472268, new PdfLine(607.6036840066815, 326.3708984367765, 273.4359245817611, 580.3104567642808).Length, 6); + Assert.Equal(741.0841681950342, new PdfLine(73.7785282991219, 748.6670328340001, 266.73884757275266, 33.14480429379307).Length, 6); + Assert.Equal(694.9308637190488, new PdfLine(904.9561547564438, 943.2478269070753, 728.9100150532171, 270.9854807616918).Length, 6); + Assert.Equal(348.61640600324927, new PdfLine(550.1396395591563, 956.8674477659421, 744.1453993717004, 667.2208291180114).Length, 6); + Assert.Equal(194.3852034492696, new PdfLine(663.0868792265696, 574.673661199057, 707.6220973512975, 385.4589110694838).Length, 6); + Assert.Equal(273.11874669170936, new PdfLine(520.1418305187533, 516.9457554127794, 704.3396253844193, 315.2900533831166).Length, 6); + Assert.Equal(829.728108753847, new PdfLine(165.24214199291708, 596.2797685006535, 937.1876756520194, 900.4981563735753).Length, 6); + Assert.Equal(782.7060348711881, new PdfLine(17.175643631104464, 471.6532293854632, 655.4886161125887, 18.679404329721816).Length, 6); + Assert.Equal(830.9426722957604, new PdfLine(90.6565559268615, 223.6378235497558, 840.088133504139, 582.5501180898741).Length, 6); + Assert.Equal(395.00250441819514, new PdfLine(497.1902525443935, 118.45344200568675, 377.30799909828573, 494.82456710075763).Length, 6); + Assert.Equal(578.8861067295652, new PdfLine(789.5022287448717, 549.3983044175613, 294.1068291117959, 848.8854036170072).Length, 6); + Assert.Equal(1018.5247728778488, new PdfLine(9.632939021435805, 900.9420815844204, 995.5448229006015, 645.265586396247).Length, 6); + Assert.Equal(261.5619031316215, new PdfLine(102.65598656650377, 814.8187504453743, 131.81357169794717, 554.8870971225473).Length, 6); + Assert.Equal(108.49401361268126, new PdfLine(130.16122201931546, 122.34940926745108, 160.1257456780558, 226.6234631432521).Length, 6); + Assert.Equal(332.92958441429414, new PdfLine(624.4803534254545, 835.4038007603154, 308.56356596547295, 730.3386229931815).Length, 6); + Assert.Equal(313.6871247728144, new PdfLine(722.9405250548465, 877.4613798590146, 665.3511605499749, 569.105939913227).Length, 6); + Assert.Equal(420.2790978069219, new PdfLine(465.3756579114451, 799.1386897491068, 67.53688693018323, 663.6439210031537).Length, 6); + Assert.Equal(224.58265631699635, new PdfLine(396.2261088696275, 284.94189008652484, 397.2472372180608, 509.5222249675961).Length, 6); + Assert.Equal(546.2969535991175, new PdfLine(102.78375092188985, 255.7553940937095, 133.5734017636524, 801.1839952072913).Length, 6); + Assert.Equal(794.7078364823532, new PdfLine(111.94122935338835, 788.2291106884405, 508.0945016563786, 99.30013741899279).Length, 6); + Assert.Equal(998.8189318369291, new PdfLine(628.3444948128207, 83.25407399875961, 38.35646117071645, 889.2028184949511).Length, 6); + Assert.Equal(617.8507518957027, new PdfLine(77.25538035870305, 22.650767363475378, 288.5177679310035, 603.2605805903414).Length, 6); + Assert.Equal(482.66131839887277, new PdfLine(955.9618825215007, 72.15467516243501, 944.4992863029239, 554.6798632785249).Length, 6); + Assert.Equal(615.4888829566713, new PdfLine(175.146198074012, 42.09447096948937, 608.4188104182263, 479.2459417844361).Length, 6); + Assert.Equal(286.98135517124325, new PdfLine(85.62005924289029, 985.3372525527625, 203.01524950706207, 723.4657811803311).Length, 6); + Assert.Equal(173.53293381279474, new PdfLine(496.1865573157063, 116.78214835100276, 644.9528684575984, 206.12566399097275).Length, 6); + Assert.Equal(434.9983015853231, new PdfLine(309.38158754418134, 540.7725293677845, 207.83557769372996, 963.7523518954882).Length, 6); + Assert.Equal(802.4785492127834, new PdfLine(431.71527190726766, 927.74356440042, 22.81036259755065, 237.25924373190134).Length, 6); + Assert.Equal(425.4191249809588, new PdfLine(796.4539729718109, 377.620962145811, 558.9437010302202, 730.5661457392357).Length, 6); + Assert.Equal(206.44000756255193, new PdfLine(908.0660285601697, 421.9422452496654, 793.536713896605, 593.6993784497764).Length, 6); + Assert.Equal(672.6343270485902, new PdfLine(93.86765378479423, 914.1712581378716, 764.6725799494799, 963.7463421751229).Length, 6); + Assert.Equal(332.2471802658839, new PdfLine(652.4365302659129, 324.1216594893792, 399.37133961411575, 539.4033309688629).Length, 6); + Assert.Equal(354.8834402095283, new PdfLine(782.1174340979168, 395.1549050103912, 429.77806425877804, 437.5721726770914).Length, 6); + Assert.Equal(874.9270988885283, new PdfLine(847.7196315611067, 913.3652729563843, 801.924841591113, 39.63747462265788).Length, 6); + Assert.Equal(200.00222948210663, new PdfLine(84.23216175716408, 201.15516601977046, 78.370199762001, 401.07147150629277).Length, 6); + Assert.Equal(555.4855348140425, new PdfLine(156.21894875551823, 466.94074837313525, 672.5690131332882, 262.1315923162938).Length, 6); + Assert.Equal(504.67386696619155, new PdfLine(281.3795209463261, 714.5244079956979, 483.22421175287656, 251.97232142401538).Length, 6); + Assert.Equal(471.28478764279447, new PdfLine(541.9836245369752, 823.1949808736383, 124.57681900242889, 604.3779117109257).Length, 6); + Assert.Equal(629.9344802246635, new PdfLine(725.1703955054331, 766.087125111111, 266.71083392776865, 334.07754214571816).Length, 6); + Assert.Equal(292.87445361844755, new PdfLine(702.9749087231916, 291.8611419226987, 661.2923953901591, 581.7542492304541).Length, 6); + Assert.Equal(176.71264203102157, new PdfLine(504.4594141560583, 194.80715729877573, 679.6758800916137, 171.8605253315718).Length, 6); + Assert.Equal(509.0936334402613, new PdfLine(338.7644380016143, 665.2418501556981, 744.305576700243, 972.9961554585269).Length, 6); + Assert.Equal(255.3072596182049, new PdfLine(798.2400393699398, 712.5969771963856, 914.1879018257719, 485.1373324114679).Length, 6); + Assert.Equal(345.33696236486287, new PdfLine(842.5227485309422, 694.7947514432409, 524.017234159065, 828.2558127686201).Length, 6); + Assert.Equal(844.3114397457932, new PdfLine(854.8032787312055, 84.72840198197706, 959.8906670383329, 922.4744542582032).Length, 6); + Assert.Equal(261.7020895346001, new PdfLine(289.3758465823646, 429.9895329919011, 409.8233195515163, 197.6527302203185).Length, 6); + Assert.Equal(420.95290185364973, new PdfLine(174.46153260893738, 969.1944624043416, 497.03706849473093, 698.7386079160323).Length, 6); + Assert.Equal(816.7966080629856, new PdfLine(493.8853111490309, 821.3864245626643, 375.63069058390084, 13.195516634224692).Length, 6); + Assert.Equal(499.7883400835412, new PdfLine(303.6925554060339, 989.7772689097685, 562.8246292140069, 562.4144801606302).Length, 6); + Assert.Equal(271.67203756842264, new PdfLine(548.6759858516891, 680.7956555439721, 820.3140550883649, 676.4996879056648).Length, 6); + Assert.Equal(574.9956519860169, new PdfLine(215.14603778829832, 795.6290747221913, 618.7275259767575, 386.0659145204792).Length, 6); + Assert.Equal(468.1514755259495, new PdfLine(308.66815831290614, 505.71622208405574, 68.5429186260651, 103.83853757985139).Length, 6); + Assert.Equal(194.8029179374746, new PdfLine(344.32825537199807, 757.6694627845026, 364.65886036244075, 563.9303512512939).Length, 6); + Assert.Equal(369.70795467168074, new PdfLine(247.07072749014225, 525.3553467911364, 35.383794612098505, 222.25039104774348).Length, 6); + Assert.Equal(257.51788770996, new PdfLine(422.4996085489343, 181.13157079175346, 180.74056893286726, 269.83348314158104).Length, 6); + Assert.Equal(812.0298650772706, new PdfLine(152.4340119885752, 800.8632949326653, 937.3290234991165, 592.6991225031195).Length, 6); + Assert.Equal(733.9524283377598, new PdfLine(808.0963889252962, 655.4329788754956, 203.15307065024845, 239.82529278527332).Length, 6); + Assert.Equal(668.9932161696333, new PdfLine(310.0616983396838, 831.3719641509196, 262.14239943737203, 164.09716013558685).Length, 6); + Assert.Equal(404.2455962075436, new PdfLine(388.2333868212858, 329.0023354190755, 26.016360491071033, 508.4830527390397).Length, 6); + Assert.Equal(584.3119738083733, new PdfLine(401.97475918597394, 842.8388999158244, 135.67820681318398, 322.7363822039766).Length, 6); + Assert.Equal(43.13166410765147, new PdfLine(284.27024011096034, 921.4275603760951, 242.9204840904382, 909.1581624390094).Length, 6); + Assert.Equal(311.1072938750192, new PdfLine(607.4771013518919, 531.3669010398204, 324.66544812600694, 661.0022245511202).Length, 6); + Assert.Equal(825.5401618185322, new PdfLine(776.1605680000215, 170.6836737542553, 595.8633696252181, 976.2949193520095).Length, 6); + Assert.Equal(256.49335572522824, new PdfLine(525.5392532332307, 914.294163251886, 272.99634075153676, 959.1374334989579).Length, 6); + Assert.Equal(523.4534310145216, new PdfLine(746.8609782139025, 620.4345380443198, 276.4878534067551, 850.113918703608).Length, 6); + Assert.Equal(450.27948266944475, new PdfLine(635.6967014966066, 839.6786299421856, 434.18473904587006, 437.00687590135925).Length, 6); + Assert.Equal(353.86322783943876, new PdfLine(580.4476281486512, 323.7261115921669, 888.9047468607108, 150.3093017587458).Length, 6); + Assert.Equal(373.5680565932186, new PdfLine(687.6117906871925, 182.85418698039524, 731.575985861892, 553.8262107901094).Length, 6); + Assert.Equal(602.3144965980068, new PdfLine(739.9965216015366, 164.05285107616163, 954.0596577820996, 727.0446152616177).Length, 6); + Assert.Equal(577.6509862708537, new PdfLine(78.29728590071838, 772.6059529810002, 271.16062719547017, 228.10214810511687).Length, 6); + Assert.Equal(870.8674265571132, new PdfLine(671.1912139589124, 378.5165174837426, 50.69352689765605, 989.575184871613).Length, 6); + Assert.Equal(750.2395936893962, new PdfLine(983.0621291942329, 317.7681790880521, 329.28371189757166, 685.7807182987465).Length, 6); + Assert.Equal(823.4380073065946, new PdfLine(786.6006014947625, 878.7158536740357, 170.13072221151072, 332.8083158862265).Length, 6); + Assert.Equal(412.61059058569464, new PdfLine(600.3667413490577, 514.1089485852575, 731.5877076160489, 122.92030166994516).Length, 6); + Assert.Equal(620.0359289489824, new PdfLine(505.86925166248074, 388.2624502842045, 670.7525439617368, 985.9731270402631).Length, 6); + Assert.Equal(765.1634994203943, new PdfLine(622.1148121263606, 84.16274688333215, 68.03381061781889, 611.8647694420438).Length, 6); + Assert.Equal(639.0761770184668, new PdfLine(973.3652080117488, 309.6533063561663, 762.0218813408244, 912.7721655408261).Length, 6); + Assert.Equal(323.78727039408955, new PdfLine(864.5922509054712, 640.4447192700089, 975.4472466741516, 944.6639927922188).Length, 6); } [Fact] - public void Insersect() + public void IntersectsWith() { - var line0 = new PdfLine(2, 1, 6, 4); - Assert.True(line0.IntersectsWith(new PdfLine(2, 4, 6, 1))); + var line0 = new PdfLine(121.86510182798493, 888.4331734682064, 652.3590264996429, 215.71124378171268); + Assert.True(line0.IntersectsWith(new PdfLine(106.65797430435452, 781.5461110296875, 326.0018708946326, 676.5076331266905))); + Assert.True(line0.IntersectsWith(new PdfLine(927.6711751799288, 873.1518228789744, 327.1403240297347, 298.8425616048351))); + Assert.False(line0.IntersectsWith(new PdfLine(211.9532880003101, 282.54578961873966, 14.482078986957859, 119.11793687938943))); + Assert.True(line0.IntersectsWith(new PdfLine(416.0489181616301, 4.001648778816591, 711.7342176949097, 440.6361129773777))); + Assert.True(line0.IntersectsWith(new PdfLine(320.63631388750366, 772.3086330223011, 92.88199211021964, 681.3956123187934))); + Assert.False(line0.IntersectsWith(new PdfLine(254.7767980985196, 256.3384234847108, 385.68931222160694, 389.43422949622897))); + Assert.False(line0.IntersectsWith(new PdfLine(951.4563739435887, 467.65604606055575, 653.8781391251528, 330.82804696496936))); + Assert.True(line0.IntersectsWith(new PdfLine(296.0054853334325, 984.372738928386, 150.12649899782483, 92.16578426069843))); + Assert.True(line0.IntersectsWith(new PdfLine(964.7472263072898, 634.0424073558066, 567.5072243659068, 255.47493029039703))); + Assert.False(line0.IntersectsWith(new PdfLine(445.5025817002651, 131.29026949481448, 779.5514467908611, 201.47431205695997))); + Assert.False(line0.IntersectsWith(new PdfLine(201.2807313528062, 237.61025561255232, 215.0277285149166, 100.69451038417643))); + Assert.True(line0.IntersectsWith(new PdfLine(728.5303333716589, 726.9052646877594, 228.65893311939024, 29.32611297221699))); + Assert.True(line0.IntersectsWith(new PdfLine(455.46023761228304, 198.14875399499465, 668.8777001944637, 560.649284529973))); + Assert.True(line0.IntersectsWith(new PdfLine(563.0307388324268, 66.91438078762113, 311.23309771082927, 737.5039979478947))); + Assert.False(line0.IntersectsWith(new PdfLine(520.2270215007433, 797.6233699545237, 844.9763820377324, 760.5192298844604))); + Assert.False(line0.IntersectsWith(new PdfLine(824.7288837428943, 317.22986494251506, 715.0600514651438, 205.90900383591136))); + Assert.False(line0.IntersectsWith(new PdfLine(714.7505932563387, 481.32602310525806, 375.2639238845693, 852.123333646955))); + Assert.False(line0.IntersectsWith(new PdfLine(718.2104428613408, 34.78596645816956, 600.7243078547273, 758.1526953246846))); + Assert.False(line0.IntersectsWith(new PdfLine(391.24785605781096, 97.56089354575825, 469.5609873277011, 340.0328643308137))); + Assert.False(line0.IntersectsWith(new PdfLine(368.49334095796695, 226.32460143513077, 30.741151126828626, 401.9728944817836))); + + var line1 = new PdfLine(230.1613967714955, 571.9141862195625, 765.2750076104808, 759.8623896684209); + Assert.False(line1.IntersectsWith(new PdfLine(48.55330445373185, 692.373078382745, 618.37588330216, 756.3225821516706))); + Assert.True(line1.IntersectsWith(new PdfLine(470.71765997788674, 976.5288583893856, 526.9800207024798, 411.2520206798771))); + Assert.False(line1.IntersectsWith(new PdfLine(476.2420312237511, 416.5940458027618, 204.77726076785797, 48.66536672928656))); + Assert.True(line1.IntersectsWith(new PdfLine(63.225473541715374, 852.0212054417979, 881.2596583573943, 405.4531812223144))); + Assert.False(line1.IntersectsWith(new PdfLine(907.0840709475256, 822.8209919226243, 89.6289756181683, 28.401408316098276))); + Assert.True(line1.IntersectsWith(new PdfLine(76.52440856595777, 8.908836052087254, 489.86263411047014, 911.648357552188))); + Assert.False(line1.IntersectsWith(new PdfLine(152.02843812778022, 363.9278260142812, 200.55748468145595, 153.34633257686147))); + Assert.True(line1.IntersectsWith(new PdfLine(648.8312592190831, 253.75260683872935, 104.6178178299546, 684.290289784323))); + Assert.False(line1.IntersectsWith(new PdfLine(324.7728948822769, 127.8027827855882, 866.7617466921944, 159.92551946755106))); + Assert.False(line1.IntersectsWith(new PdfLine(923.1081675320636, 643.4133624974421, 991.0788584564561, 576.4612567972256))); + Assert.False(line1.IntersectsWith(new PdfLine(264.4838653053745, 931.9786375793665, 126.15518508823797, 4.854818839558406))); + Assert.True(line1.IntersectsWith(new PdfLine(244.29731916996533, 966.1362233214004, 427.7016563174992, 28.12732081559577))); + Assert.True(line1.IntersectsWith(new PdfLine(29.899172798940278, 34.25368837165621, 822.394091757524, 862.0156881421128))); + Assert.False(line1.IntersectsWith(new PdfLine(379.3932867137456, 220.52740720135134, 342.8336650819017, 543.4172816638109))); + Assert.False(line1.IntersectsWith(new PdfLine(232.26338814197211, 398.1687360470165, 125.66445493231515, 648.9870954063125))); + Assert.False(line1.IntersectsWith(new PdfLine(439.76617664403005, 952.0541074984246, 269.4857118223336, 954.2300643651912))); + Assert.False(line1.IntersectsWith(new PdfLine(789.0561191608085, 710.4943749657895, 452.2588378717548, 583.5378533878925))); + Assert.True(line1.IntersectsWith(new PdfLine(347.9674376042718, 979.5843512282195, 286.83327692972415, 127.99359519299148))); + Assert.False(line1.IntersectsWith(new PdfLine(92.6586455785089, 716.1404938324638, 930.212110470076, 856.7417505867188))); + Assert.True(line1.IntersectsWith(new PdfLine(511.23371308545376, 613.4031648082713, 525.2595738527813, 780.5180733589225))); + + var line2 = new PdfLine(681.5712163856193, 278.35235332501765, 89.49622598121265, 478.3840577494369); + Assert.True(line2.IntersectsWith(new PdfLine(555.2938054571583, 230.13430671701042, 9.06607133775994, 661.2477207373171))); + Assert.False(line2.IntersectsWith(new PdfLine(312.5464531007375, 984.4946838425462, 647.0214816594255, 458.99042198014837))); + Assert.False(line2.IntersectsWith(new PdfLine(706.1891601946595, 503.2414560660433, 213.7012086107456, 915.7203529205516))); + Assert.True(line2.IntersectsWith(new PdfLine(516.4756289674448, 889.3568188056395, 443.3857730187525, 255.59551206643084))); + Assert.True(line2.IntersectsWith(new PdfLine(677.5899464550561, 251.9005995625827, 599.270045697367, 468.69554574840976))); + Assert.False(line2.IntersectsWith(new PdfLine(58.43822236258844, 353.2434776831316, 647.9568960966803, 238.57040552480657))); + Assert.True(line2.IntersectsWith(new PdfLine(594.6221013456028, 225.47517126242312, 555.8807024248091, 500.4558087778439))); + Assert.False(line2.IntersectsWith(new PdfLine(505.6211423680611, 506.9508670082764, 613.7622943331244, 916.7167888995149))); + Assert.False(line2.IntersectsWith(new PdfLine(181.9554901955809, 103.83812004071791, 813.1769117343163, 212.04094248210603))); + Assert.True(line2.IntersectsWith(new PdfLine(192.61397356916143, 481.26412544334676, 26.122156248780115, 351.6737388640251))); + Assert.False(line2.IntersectsWith(new PdfLine(533.3687585268123, 546.7063577336295, 734.0604636009198, 838.0593535791487))); + Assert.True(line2.IntersectsWith(new PdfLine(588.3377898992059, 428.3529117459819, 352.99245312217397, 201.32338004379702))); + Assert.True(line2.IntersectsWith(new PdfLine(100.81207169769357, 365.0059614538326, 836.9501610532827, 366.7908217495531))); + Assert.True(line2.IntersectsWith(new PdfLine(309.4732191006059, 804.4079310346496, 762.7871093749146, 104.20384008540606))); + Assert.True(line2.IntersectsWith(new PdfLine(620.165228658672, 322.5263479723731, 81.50075678495594, 74.82137781430187))); + Assert.False(line2.IntersectsWith(new PdfLine(344.98668099255235, 622.5514652630136, 890.9739242303083, 263.609734207957))); + Assert.False(line2.IntersectsWith(new PdfLine(721.9759476200207, 523.0331463894595, 175.6087614668952, 649.4213466430018))); + Assert.False(line2.IntersectsWith(new PdfLine(362.4717856570472, 428.221058553378, 830.865572391957, 991.4999990297424))); + Assert.True(line2.IntersectsWith(new PdfLine(201.07050402458904, 321.6603091186927, 353.0923523905989, 810.098585345059))); + Assert.False(line2.IntersectsWith(new PdfLine(910.4242667185558, 867.020021033747, 727.9222866131214, 238.83365486799025))); + + var line3 = new PdfLine(489.9738439176227, 374.7705883284915, 871.9262353024634, 787.1884433786219); + Assert.True(line3.IntersectsWith(new PdfLine(872.2571816146803, 693.2629489344569, 529.4023976313039, 719.6053848962767))); + Assert.False(line3.IntersectsWith(new PdfLine(248.3921952905872, 88.01302715059778, 436.5483136582463, 777.2837893075484))); + Assert.False(line3.IntersectsWith(new PdfLine(187.58364612449697, 476.00727611009387, 990.8170482716721, 15.255547734205566))); + Assert.True(line3.IntersectsWith(new PdfLine(885.1600918595342, 46.96177614048614, 571.9541129775752, 891.548614279429))); + Assert.True(line3.IntersectsWith(new PdfLine(576.6649882556012, 948.349869038489, 475.08985709179694, 236.2758426400251))); + Assert.False(line3.IntersectsWith(new PdfLine(531.27245928842, 142.7480615799812, 154.3213111834032, 526.2538388506061))); + Assert.True(line3.IntersectsWith(new PdfLine(881.8175274853771, 25.505202210701626, 480.9005042827823, 938.2873246721758))); + Assert.False(line3.IntersectsWith(new PdfLine(958.3250663923161, 136.8670470723481, 855.0411792021424, 246.15422872537695))); + Assert.False(line3.IntersectsWith(new PdfLine(467.6290179503615, 288.9112633707194, 766.6099159094962, 322.0833946500801))); + Assert.True(line3.IntersectsWith(new PdfLine(719.0037402256572, 956.4575112925683, 749.8148788878581, 511.0524574005092))); + Assert.False(line3.IntersectsWith(new PdfLine(995.3187839813876, 407.9303435596747, 377.91347458616985, 130.41305570255912))); + Assert.True(line3.IntersectsWith(new PdfLine(492.61631436354236, 72.28411781217147, 784.1433830572357, 704.105888102883))); + Assert.False(line3.IntersectsWith(new PdfLine(759.5238447083048, 600.6985614904459, 638.3886373294663, 372.19990350407573))); + Assert.True(line3.IntersectsWith(new PdfLine(459.48103366324966, 791.8975852411837, 931.1526059112057, 253.58273628850537))); + Assert.False(line3.IntersectsWith(new PdfLine(478.668011178824, 664.3818183262641, 116.69704115448553, 235.6187842060108))); + Assert.True(line3.IntersectsWith(new PdfLine(817.7287058030429, 245.110091787617, 95.46523288576525, 867.3892357838256))); + Assert.False(line3.IntersectsWith(new PdfLine(421.43891095440244, 525.8551990428018, 579.8572760181712, 709.5786739338987))); + Assert.False(line3.IntersectsWith(new PdfLine(405.27270929845946, 971.7859948825261, 602.7643289679388, 739.42851185728))); + Assert.False(line3.IntersectsWith(new PdfLine(434.10889800574284, 687.405657562332, 36.105527129304036, 624.8043796411798))); + Assert.False(line3.IntersectsWith(new PdfLine(407.9918033780493, 802.7120698257748, 402.61114362680604, 384.52039281951886))); + + + // need to test for paralell lines + } + + [Fact] + public void Intersect() + { + var line0 = new PdfLine(249.47344290572826, 472.01986485320856, 732.3041089590511, 429.3963331123053); + Assert.Null(line0.Intersect(new PdfLine(946.6713787409598, 272.5842702079052, 661.7722741634802, 974.9718084951525))); + Assert.Null(line0.Intersect(new PdfLine(540.4951330023757, 753.1874086128184, 250.27172041519742, 756.784934462783))); + Assert.Null(line0.Intersect(new PdfLine(440.3205368146664, 686.771688803123, 264.48095370451307, 709.8806000661422))); + Assert.Null(line0.Intersect(new PdfLine(915.966158342946, 744.9474270511445, 907.3260222851492, 916.7379211798299))); + Assert.Equal(new PdfPoint(545.1958434534847, 445.9139564831479), line0.Intersect(new PdfLine(966.1400668322528, 624.6061065235268, 464.3184791386117, 411.58126032919796)).Value, PointComparer); + Assert.Null(line0.Intersect(new PdfLine(457.4644688402201, 185.19895606329106, 343.63470036832234, 282.14725095181524))); + Assert.Null(line0.Intersect(new PdfLine(596.9425234714071, 520.3972980490058, 692.1504181909977, 934.6239207853833))); + Assert.Null(line0.Intersect(new PdfLine(702.5419877932891, 837.7055233563706, 184.57014732227984, 800.6947347151163))); + Assert.Null(line0.Intersect(new PdfLine(63.908287486616366, 782.5482324259448, 202.12075097336734, 29.037905727768543))); + Assert.Null(line0.Intersect(new PdfLine(699.4526266285355, 50.701692920919086, 534.9561205104103, 169.54750689077014))); + Assert.Equal(new PdfPoint(283.0516233385167, 469.05563590069073), line0.Intersect(new PdfLine(397.3618485639624, 200.48430056180177, 75.12971757838005, 957.5671733950289)).Value, PointComparer); + Assert.Equal(new PdfPoint(628.1904128326876, 438.5873264429044), line0.Intersect(new PdfLine(757.2017859731089, 333.55932267413647, 215.67641327028286, 774.4144775387421)).Value, PointComparer); + Assert.Null(line0.Intersect(new PdfLine(748.8435297643139, 533.358109476909, 701.6428580416452, 965.0203164274997))); + Assert.Null(line0.Intersect(new PdfLine(886.6338412881437, 887.5633472524614, 926.764649490491, 745.6663789247506))); + Assert.Null(line0.Intersect(new PdfLine(189.94143293608457, 353.71515852502154, 909.3320243673451, 97.14417015446696))); + Assert.Equal(new PdfPoint(686.5657923909976, 433.4340397558154), line0.Intersect(new PdfLine(608.2719064253038, 682.5753778353077, 819.0344381208887, 11.901563194561149)).Value, PointComparer); + Assert.Null(line0.Intersect(new PdfLine(863.3451705140101, 300.1007553258847, 628.1702242310403, 80.05558039310745))); + Assert.Null(line0.Intersect(new PdfLine(379.7182020231311, 122.98607964547048, 220.82462806140157, 476.07465576210694))); + Assert.Null(line0.Intersect(new PdfLine(150.73757132117638, 163.9876267539735, 621.5387365388307, 381.1628197815454))); + Assert.Null(line0.Intersect(new PdfLine(963.7401653691339, 468.9864141433272, 461.5718937734926, 779.1925326010052))); + + var line1 = new PdfLine(805.4090239582979, 476.7889933681887, 302.5142884961789, 922.3598084329763); + Assert.Null(line1.Intersect(new PdfLine(80.74661583851028, 554.2004208890269, 306.3044585249692, 556.0641175375137))); + Assert.Equal(new PdfPoint(718.3651178016886, 553.9109464882264), line1.Intersect(new PdfLine(539.5797804350717, 240.02552755940033, 795.4817008903701, 689.3010816396012)).Value, PointComparer); + Assert.Equal(new PdfPoint(638.7746772923516, 624.4290389405624), line1.Intersect(new PdfLine(401.4499813337844, 988.372895132168, 934.5386188831096, 170.867015021393)).Value, PointComparer); + Assert.Null(line1.Intersect(new PdfLine(682.4346840735002, 480.42134915306866, 546.3740655370983, 200.3751001600633))); + Assert.Null(line1.Intersect(new PdfLine(77.22352473898819, 134.48869782489516, 301.68939728706033, 668.8824171991777))); + Assert.Equal(new PdfPoint(701.4665906959101, 568.8832457885902), line1.Intersect(new PdfLine(901.5364640930612, 586.1527360560148, 94.94810134320741, 516.5302104662884)).Value, PointComparer); + Assert.Equal(new PdfPoint(624.8437642002289, 636.7719963496312), line1.Intersect(new PdfLine(840.591970170414, 12.655883306032845, 553.4055248375457, 843.4284367738792)).Value, PointComparer); + Assert.Null(line1.Intersect(new PdfLine(148.64581345463833, 614.6633416576203, 193.89988447854657, 65.24704638798428))); + Assert.Null(line1.Intersect(new PdfLine(248.17672727909124, 891.7248029485047, 150.20115866146634, 362.0108883103159))); + Assert.Null(line1.Intersect(new PdfLine(261.11174590176444, 8.934250136457877, 924.8114439905122, 273.9827355090374))); + Assert.Equal(new PdfPoint(796.646895313841, 484.55234527169506), line1.Intersect(new PdfLine(935.1255965324843, 690.4339511947389, 727.4037544886379, 381.60590725058927)).Value, PointComparer); + Assert.Null(line1.Intersect(new PdfLine(899.6539927432093, 32.69185179851786, 28.66371660343381, 375.4911653840769))); + Assert.Equal(new PdfPoint(557.7316315260696, 696.2341572109548), line1.Intersect(new PdfLine(843.9300053666182, 993.122304586576, 397.96518279311954, 530.5002840914163)).Value, PointComparer); + Assert.Null(line1.Intersect(new PdfLine(718.0150386321341, 302.8719368347982, 861.1312410432275, 309.755416028656))); + Assert.Equal(new PdfPoint(724.341934010291, 548.6154150742572), line1.Intersect(new PdfLine(745.031278693096, 782.0724737481689, 717.3560143336663, 469.7868062013797)).Value, PointComparer); + Assert.Null(line1.Intersect(new PdfLine(372.19847499219725, 159.9777911218353, 660.279657385527, 473.34232882665015))); + Assert.Null(line1.Intersect(new PdfLine(326.82581111546784, 596.085767017573, 691.8167132271086, 264.08147843750476))); + Assert.Null(line1.Intersect(new PdfLine(818.7414803241093, 714.0401709084665, 891.4704199532238, 194.71346086805286))); + Assert.Equal(new PdfPoint(671.619900613135, 595.3277740156508), line1.Intersect(new PdfLine(21.57423583123652, 743.2422768237401, 975.9390184260003, 526.0815449505143)).Value, PointComparer); + Assert.Equal(new PdfPoint(326.51596528777543, 901.0940326416946), line1.Intersect(new PdfLine(798.5429757903388, 976.1047510758361, 119.10382972392742, 868.1337719904257)).Value, PointComparer); + + var line2 = new PdfLine(351.9016935216292, 145.0516312659712, 413.95324499342, 933.7768132428268); + Assert.Null(line2.Intersect(new PdfLine(555.7326061092734, 596.7660607553403, 555.3101952087702, 748.2021066579533))); + Assert.Null(line2.Intersect(new PdfLine(434.9452762531486, 266.16267918746826, 370.0804431002427, 293.10181949672887))); + Assert.Equal(new PdfPoint(363.27088568715527, 289.5632179546771), line2.Intersect(new PdfLine(433.28817736655236, 454.27297566039005, 254.32586010490942, 33.27925814286659)).Value, PointComparer); + Assert.Null(line2.Intersect(new PdfLine(981.7946771114343, 355.3735941891449, 427.31815314210144, 553.1409091609705))); + Assert.Equal(new PdfPoint(405.2231610165113, 822.8104165929249), line2.Intersect(new PdfLine(450.35036850462194, 876.8904008574443, 43.2875505405439, 389.0704798936191)).Value, PointComparer); + Assert.Equal(new PdfPoint(352.3148117086049, 150.30269605761396), line2.Intersect(new PdfLine(335.6394222133249, 147.76920224797973, 754.5140780889623, 211.4088760303502)).Value, PointComparer); + Assert.Equal(new PdfPoint(407.35672177955183, 849.9296917951381), line2.Intersect(new PdfLine(886.3510678872603, 743.3709651600388, 258.90820026432357, 882.9540595190294)).Value, PointComparer); + Assert.Null(line2.Intersect(new PdfLine(699.5324602634907, 30.62029266548705, 438.1893757032378, 185.62504049463791))); + Assert.Null(line2.Intersect(new PdfLine(25.392070482058937, 783.0010773726668, 331.9724385523838, 410.8713863706629))); + Assert.Equal(new PdfPoint(387.1180119551795, 592.6793940896622), line2.Intersect(new PdfLine(127.50002828430095, 559.5543804276867, 608.7796689026322, 620.9615081054184)).Value, PointComparer); + Assert.Null(line2.Intersect(new PdfLine(700.4333788583542, 985.722567203589, 454.4815607496074, 537.8582103267325))); + Assert.Null(line2.Intersect(new PdfLine(695.3025157099005, 973.528879326945, 732.6364581909932, 948.3480610182024))); + Assert.Null(line2.Intersect(new PdfLine(999.8609414071335, 31.331087823489256, 469.11558751712425, 747.562026159229))); + Assert.Equal(new PdfPoint(378.07370772948497, 477.71900507331543), line2.Intersect(new PdfLine(210.82749903103237, 689.8013256401747, 704.8609969023787, 63.325117409769206)).Value, PointComparer); + Assert.Equal(new PdfPoint(392.2360372608596, 657.7336166648996), line2.Intersect(new PdfLine(828.8916324416031, 399.5799314744642, 115.61389583398585, 821.2744621338746)).Value, PointComparer); + Assert.Null(line2.Intersect(new PdfLine(312.9420577971906, 575.7851918030872, 369.1653129430475, 848.5174999834271))); + Assert.Null(line2.Intersect(new PdfLine(994.7168400302381, 218.52516980719062, 602.8188689748915, 547.2309026974548))); + Assert.Equal(new PdfPoint(381.0876815694553, 516.0290393757577), line2.Intersect(new PdfLine(193.64565487779996, 426.0413081120783, 409.2579997731871, 529.5531303647003)).Value, PointComparer); + Assert.Equal(new PdfPoint(375.6334131353115, 446.70089590095074), line2.Intersect(new PdfLine(116.42702680625928, 926.1997854715715, 539.6478715859665, 143.29498135384767)).Value, PointComparer); + Assert.Null(line2.Intersect(new PdfLine(289.3283008851727, 887.2935775850133, 128.73988804916837, 581.7849414489705))); + + var line3 = new PdfLine(827.9764299357637, 571.2599298061439, 269.42809761310895, 747.1120897673287); + Assert.Equal(new PdfPoint(426.2405090926502, 697.7416061912185), line3.Intersect(new PdfLine(597.9759520434611, 362.8430619098778, 288.87665225221747, 965.6126693934004)).Value, PointComparer); + Assert.Equal(new PdfPoint(725.181697687369, 603.623603014767), line3.Intersect(new PdfLine(960.6983788833169, 39.57574300958655, 678.827990569521, 714.637859713536)).Value, PointComparer); + Assert.Null(line3.Intersect(new PdfLine(848.441753184245, 226.01218557979473, 961.7117377516282, 851.4119110049194))); + Assert.Equal(new PdfPoint(513.7162314550305, 670.2009372259705), line3.Intersect(new PdfLine(538.0192981290608, 860.805671505451, 442.6036462826098, 112.47727286224085)).Value, PointComparer); + Assert.Equal(new PdfPoint(436.4187668433915, 694.5371053333131), line3.Intersect(new PdfLine(278.34070591188333, 559.6994093955936, 570.5767490067819, 808.9714113968411)).Value, PointComparer); + Assert.Equal(new PdfPoint(669.0652870832304, 621.2911734700773), line3.Intersect(new PdfLine(741.6848820025791, 656.9267581190151, 118.21235604462099, 350.9789213163321)).Value, PointComparer); + Assert.Null(line3.Intersect(new PdfLine(104.85791214848173, 875.2509567504092, 759.847180539203, 335.0494402032118))); + Assert.Equal(new PdfPoint(560.7507267197575, 655.3926975332802), line3.Intersect(new PdfLine(613.226053217281, 723.1667222798209, 194.830706992121, 182.79206543533866)).Value, PointComparer); + Assert.Equal(new PdfPoint(537.6738790396856, 662.6581628024372), line3.Intersect(new PdfLine(764.8077992586041, 874.9326555599533, 146.55306292710125, 297.1250325364575)).Value, PointComparer); + Assert.Null(line3.Intersect(new PdfLine(979.8554501847415, 682.366125605611, 58.09639347344508, 973.545841809009))); + Assert.Equal(new PdfPoint(532.8647782579247, 664.1722497865072), line3.Intersect(new PdfLine(530.470399838713, 701.712690490121, 569.911968144255, 83.32511885506533)).Value, PointComparer); + Assert.Null(line3.Intersect(new PdfLine(949.940584568515, 911.3281295765591, 612.6414591835184, 906.1725686874659))); + Assert.Null(line3.Intersect(new PdfLine(920.5210946321213, 322.83749577186416, 150.72810140776494, 263.9548604802149))); + Assert.Null(line3.Intersect(new PdfLine(867.3018668231659, 414.9718567669486, 704.6924632399761, 257.718843555619))); + Assert.Null(line3.Intersect(new PdfLine(939.1653932965677, 123.18553916850628, 255.76050234694125, 247.3931578409386))); + Assert.Null(line3.Intersect(new PdfLine(529.7116927877873, 228.07257844105567, 827.9533012755296, 362.2625343904694))); + Assert.Equal(new PdfPoint(533.3919167903337, 664.0062866206387), line3.Intersect(new PdfLine(3.5378422551779476, 721.4478307017417, 849.4639656918123, 629.7408777045952)).Value, PointComparer); + Assert.Null(line3.Intersect(new PdfLine(856.5972144347583, 660.3897499193599, 304.52476823723094, 841.7661947036165))); + Assert.Equal(new PdfPoint(580.7069188830294, 649.1097327593658), line3.Intersect(new PdfLine(35.708738123459206, 183.13206057626485, 844.7969349272522, 874.9087450635437)).Value, PointComparer); + Assert.Null(line3.Intersect(new PdfLine(122.32223965238276, 154.29377064067808, 300.48831503480864, 633.3423829686435))); + + var line4 = new PdfLine(272.2500658865409, 944.030904393777, 325.8150603620954, 229.21108602064965); + Assert.Null(line4.Intersect(new PdfLine(94.23759482120676, 50.728551251037814, 838.9961530818438, 521.7125228321976))); + Assert.Null(line4.Intersect(new PdfLine(106.17950496328655, 696.4005443937872, 8.81336619921358, 41.91149367532776))); + Assert.Null(line4.Intersect(new PdfLine(500.36065344929915, 882.9094171578315, 961.1482275308962, 318.49086681599016))); + Assert.Equal(new PdfPoint(312.4491435568774, 407.5779899406437), line4.Intersect(new PdfLine(618.2238088991691, 911.4108638928444, 303.3231477545474, 392.5408491506552)).Value, PointComparer); + Assert.Null(line4.Intersect(new PdfLine(281.4082184540746, 979.6282858096472, 913.5382692319204, 311.148978749047))); + Assert.Null(line4.Intersect(new PdfLine(607.4452789874105, 762.9411783229958, 630.3948270457036, 232.28318910839863))); + Assert.Equal(new PdfPoint(312.93418988326107, 401.10509228942965), line4.Intersect(new PdfLine(646.9628855796868, 557.0746028019612, 264.28988842381506, 378.3913913840766)).Value, PointComparer); + Assert.Equal(new PdfPoint(299.828540789867, 575.9987480495781), line4.Intersect(new PdfLine(833.7610711733155, 959.0533757539872, 180.4547011098937, 490.3573999125411)).Value, PointComparer); + Assert.Equal(new PdfPoint(301.0292352778426, 559.9755929233689), line4.Intersect(new PdfLine(449.1882948826826, 259.4417252863588, 291.156845084154, 580.0012843366668)).Value, PointComparer); + Assert.Null(line4.Intersect(new PdfLine(806.7141902274523, 92.17984347609298, 262.0311941394193, 229.6055538350278))); + Assert.Equal(new PdfPoint(312.7290679628452, 403.84242504436787), line4.Intersect(new PdfLine(682.2466893745454, 367.58972005929877, 94.70399650815587, 425.232470164812)).Value, PointComparer); + Assert.Equal(new PdfPoint(311.23767814165194, 423.7448820789783), line4.Intersect(new PdfLine(515.039723816646, 688.7989508153315, 218.33672413501725, 302.9228571214113)).Value, PointComparer); + Assert.Null(line4.Intersect(new PdfLine(969.4708245146627, 900.3068255154104, 918.9397260374807, 773.5705288854887))); + Assert.Null(line4.Intersect(new PdfLine(496.0627957884615, 447.9146019160819, 924.7464937982644, 959.2278477114312))); + Assert.Equal(new PdfPoint(315.0270907385083, 373.1755270226291), line4.Intersect(new PdfLine(137.57066459141853, 407.0475468535213, 370.2785910543763, 362.6293892747946)).Value, PointComparer); + Assert.Equal(new PdfPoint(294.48887005559953, 647.2561522892688), line4.Intersect(new PdfLine(715.6460182852811, 510.63316418211866, 20.73076328900536, 736.0630281127584)).Value, PointComparer); + Assert.Null(line4.Intersect(new PdfLine(835.8448024941904, 894.8049031463455, 556.4752433721656, 882.0142333548768))); + Assert.Null(line4.Intersect(new PdfLine(498.53009731811073, 811.6398668764998, 951.9489332295883, 866.4765330457532))); + Assert.Null(line4.Intersect(new PdfLine(533.9099241171612, 448.8008734479916, 986.9261524363459, 494.81790954688154))); + Assert.Equal(new PdfPoint(288.97653073783704, 720.8178025519654), line4.Intersect(new PdfLine(795.5467166283111, 728.7305691796133, 258.36414851796087, 720.3396286618134)).Value, PointComparer); } [Fact] @@ -62,11 +363,75 @@ Assert.False(horizontalLine.Contains(new PdfPoint(10, 12))); Assert.True(horizontalLine.Contains(new PdfPoint(20, 7.5))); Assert.True(horizontalLine.Contains(new PdfPoint(26.3d, 7.5d))); + + var line0 = new PdfLine(438.3865838366066, 609.9902140686454, 944.7309188382663, 876.9348433851213); + Assert.False(line0.Contains(new PdfPoint(634.4822834192046, 309.61126869152156))); + Assert.False(line0.Contains(new PdfPoint(513.4378559485833, 941.0769848202681))); + Assert.False(line0.Contains(new PdfPoint(965.1109446991279, 329.09245612574057))); + Assert.False(line0.Contains(new PdfPoint(820.7095483378824, 525.358685774857))); + Assert.False(line0.Contains(new PdfPoint(990.8069634112412, 663.5926568025885))); + + var line1 = new PdfLine(934.3159494382417, 759.5192678221186, 260.0914555582554, 754.853110870852); + Assert.False(line1.Contains(new PdfPoint(214.39243948924536, 717.9615854355675))); + Assert.False(line1.Contains(new PdfPoint(602.2787326566985, 662.8585244372948))); + Assert.False(line1.Contains(new PdfPoint(658.3394804169986, 73.36850324595979))); + Assert.False(line1.Contains(new PdfPoint(166.68617796663375, 846.1564716027135))); + Assert.False(line1.Contains(new PdfPoint(477.03683917749896, 725.7599348603968))); + + var line2 = new PdfLine(459.5236361336371, 225.75275035034105, 243.6017422524196, 577.1870942626915); + Assert.False(line2.Contains(new PdfPoint(138.46035708111393, 612.611020472454))); + Assert.True(line2.Contains(new PdfPoint(372.028832661666, 368.15923957290966))); + Assert.False(line2.Contains(new PdfPoint(174.3062607282675, 246.10768796449022))); + Assert.True(line2.Contains(new PdfPoint(407.3009997733987, 310.75028171105555))); + Assert.False(line2.Contains(new PdfPoint(413.5520960352509, 831.4966615594723))); + Assert.True(line2.Contains(new PdfPoint(331.59996792967223, 433.96123536322784))); + Assert.False(line2.Contains(new PdfPoint(507.311911158632, 878.2547755917843))); + Assert.False(line2.Contains(new PdfPoint(30.25648083800045, 924.4277127301796))); + Assert.False(line2.Contains(new PdfPoint(494.504688420875, 372.0094967104417))); + Assert.True(line2.Contains(new PdfPoint(403.89590870792, 316.2924058701583))); + + var line3 = new PdfLine(509.21323774187186, 888.044377235812, 47.37833742875941, 849.8318450594651); + Assert.False(line3.Contains(new PdfPoint(678.2233054557167, 546.1624886001035))); + Assert.True(line3.Contains(new PdfPoint(480.660341056732, 885.6818912422912))); + Assert.False(line3.Contains(new PdfPoint(506.5932134253688, 110.51265720847093))); + Assert.True(line3.Contains(new PdfPoint(437.25072258310075, 882.0901500344223))); + Assert.False(line3.Contains(new PdfPoint(109.31452757193073, 294.37842228373967))); + Assert.True(line3.Contains(new PdfPoint(476.2526479351329, 885.3171957307168))); + Assert.False(line3.Contains(new PdfPoint(740.0033114463812, 982.64467663726))); + Assert.True(line3.Contains(new PdfPoint(88.91977913129028, 853.2690120570946))); + Assert.False(line3.Contains(new PdfPoint(681.4728580543268, 760.6227653385002))); + Assert.True(line3.Contains(new PdfPoint(490.18925240056944, 886.4703198151103))); + + var line4 = new PdfLine(405.58207740586516, 672.608614731411, 207.25519576774244, 970.4807982580487); + Assert.False(line4.Contains(new PdfPoint(392.6618359714601, 96.47934589520469))); + Assert.True(line4.Contains(new PdfPoint(309.7610862024394, 816.5245964767958))); + Assert.False(line4.Contains(new PdfPoint(845.9946332716904, 418.6201620546748))); + Assert.True(line4.Contains(new PdfPoint(280.8428236740471, 859.9576698553517))); + Assert.False(line4.Contains(new PdfPoint(889.4514788109458, 672.0134606355084))); + Assert.True(line4.Contains(new PdfPoint(320.58916899950924, 800.2616237545421))); + Assert.False(line4.Contains(new PdfPoint(985.5749855832817, 486.8777325651764))); + Assert.False(line4.Contains(new PdfPoint(172.58499465406223, 1022.5528543693492))); + Assert.False(line4.Contains(new PdfPoint(936.4162971004565, 620.0608255800965))); + Assert.True(line4.Contains(new PdfPoint(336.6230951431535, 776.1798626162473))); + + var line5 = new PdfLine(435.3238347493861, 795.9122229139125, 732.5503121192992, 908.7240131737507); + Assert.False(line5.Contains(new PdfPoint(547.4436930896966, 559.6385871353319))); + Assert.False(line5.Contains(new PdfPoint(13.874097858707657, 635.9517115089891))); + Assert.False(line5.Contains(new PdfPoint(826.9938262230233, 989.6274928609377))); + Assert.False(line5.Contains(new PdfPoint(118.97238757058906, 675.8415837725852))); + Assert.False(line5.Contains(new PdfPoint(232.50406167656078, 813.870814739484))); + Assert.False(line5.Contains(new PdfPoint(29.09641985978787, 641.7293172325747))); + Assert.False(line5.Contains(new PdfPoint(312.0559485367265, 438.7925410762308))); + Assert.False(line5.Contains(new PdfPoint(109.7704752533478, 672.3490140274272))); + Assert.False(line5.Contains(new PdfPoint(82.60913058985109, 752.1433892591745))); + Assert.False(line5.Contains(new PdfPoint(50.992247634205874, 650.0398404144613))); } [Fact] public void ParallelTo() { + // TO DO: IMPROVE + var verticalLine1 = new PdfLine(10, 7.5d, 10, 15); var verticalLine2 = new PdfLine(200, 0, 200, 551.5467d); var horizontalLine1 = new PdfLine(10, 7.5d, 26.3d, 7.5d); diff --git a/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs b/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs index 4db7fc34..8e325821 100644 --- a/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs +++ b/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs @@ -13,6 +13,17 @@ { private const double epsilon = 1e-5; + /// + /// Return true if the points are in counter-clockwise order. + /// + /// The first point. + /// The second point. + /// The third point. + private static bool ccw(PdfPoint point1, PdfPoint point2, PdfPoint point3) + { + return (point2.X - point1.X) * (point3.Y - point1.Y) > (point2.Y - point1.Y) * (point3.X - point1.X); + } + #region PdfPoint /// /// Get the dot product of both points. @@ -226,12 +237,6 @@ if (points.Count() < 3) return points; - Func ccw = (PdfPoint p1, PdfPoint p2, PdfPoint p3) => - { - // TO DO, check rounding here - return Math.Round((p2.X - p1.X) * (p3.Y - p1.Y) - (p2.Y - p1.Y) * (p3.X - p1.X), 6); - }; - Func polarAngle = (PdfPoint point1, PdfPoint point2) => { return Math.Atan2(point2.Y - point1.Y, point2.X - point1.X) % Math.PI; @@ -274,7 +279,7 @@ for (int i = 2; i < sortedPoints.Count; i++) { var point = sortedPoints[i]; - while (ccw(stack.ElementAt(1), stack.Peek(), point) < 0) + while (!ccw(stack.ElementAt(1), stack.Peek(), point)) { stack.Pop(); } @@ -485,8 +490,8 @@ /// public static bool IntersectsWith(this PdfLine line, PdfLine other) { - // to improve - return Intersect(line, other) != null; + return (ccw(line.Point1, line.Point2, other.Point1) != ccw(line.Point1, line.Point2, other.Point2)) && + (ccw(other.Point1, other.Point2, line.Point1) != ccw(other.Point1, other.Point2, line.Point2)); } /// @@ -494,44 +499,29 @@ /// public static PdfPoint? Intersect(this PdfLine line, PdfLine other) { - // if the bounding boxes do not intersect, the lines cannot intersect - if (!line.GetBoundingRectangle().IntersectsWith(other.GetBoundingRectangle())) - { - return null; - } + if (!line.IntersectsWith(other)) return null; var eq1 = GetSlopeIntercept(line.Point1, line.Point2); var eq2 = GetSlopeIntercept(other.Point1, other.Point2); - if (double.IsNaN(eq1.Slope) && double.IsNaN(eq2.Slope)) return null; // both lines are vertical (hence parallel) - if (Math.Abs(eq1.Slope - eq2.Slope) < epsilon) return null; // both lines are parallel - - PdfPoint? intersection = null; - if (double.IsNaN(eq1.Slope)) { var x = eq1.Intercept; var y = eq2.Slope * x + eq2.Intercept; - intersection = new PdfPoint(x, y); + return new PdfPoint(x, y); } else if (double.IsNaN(eq2.Slope)) { var x = eq2.Intercept; var y = eq1.Slope * x + eq1.Intercept; - intersection = new PdfPoint(x, y); + return new PdfPoint(x, y); } else { var x = (eq2.Intercept - eq1.Intercept) / (eq1.Slope - eq2.Slope); var y = eq1.Slope * x + eq1.Intercept; - intersection = new PdfPoint(x, y); + return new PdfPoint(x, y); } - - // check if the intersection point belongs to both segments - // (for the moment we only know it belongs to both lines) - if (!line.Contains(intersection.Value)) return null; - if (!other.Contains(intersection.Value)) return null; - return intersection; } /// @@ -811,7 +801,7 @@ private static (double Slope, double Intercept) GetSlopeIntercept(PdfPoint point1, PdfPoint point2) { - if (Math.Abs(point1.X - point2.X) > epsilon) // vertical line special case + if (Math.Abs(point1.X - point2.X) > epsilon) { var slope = (point2.Y - point1.Y) / (point2.X - point1.X); var intercept = point2.Y - slope * point2.X; @@ -819,6 +809,7 @@ } else { + // vertical line special case return (double.NaN, point1.X); } }