Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Step 1 is to find if the lines are on the same projection. This is true if the slope of the first line equals the slope of the constructed line segment from the second-last point of the first line to the second point of the second line.</p> <p>I don't have Mathematica on my work machine so I can't test this out (there might be syntax errors), but something like the following should work:</p> <pre><code>(( #2[[2,2]]-#1[[-2,2]])/(#2[[2,1]]-#1[[-2,1]])) == (( #1[[-1,2]]-#1[[-2,2]])/(#1[[-1,1]]-#1[[-2,1]])) &amp; @@@ (Transpose[{Most[lines],Rest[lines]}]) </code></pre> <p>Essentialy all this does is test that "rise over run" for the first line equals "rise over run" for the joined line segment.</p> <p>I am assuming that :lines: is not a list of JoinedCurve elements, but a simple list of n*2 lists of points. I am also assuming that the pairs of points defining each line segment are in a canonical order with the points in ascending order in x-direction. That is, the value of first element of the first point is lower than the first element of the second point. If not, sort them first.</p> <p>Step 2 is actually joining the points. This applies the test in Step 1 and then replaces the two lines with a single joined line. You could wrap this in FixedPoint to join all the lines that are in the same projection.</p> <pre><code>If[(( #2[[2,2]]-#1[[-2,2]])/(#2[[2,1]]-#1[[-2,1]])) == (( #1[[-1,2]]-#1[[-2,2]])/(#1[[-1,1]]-#1[[-2,1]])), {#1[[-2]],#2[[2]]}] &amp; @@@ (Transpose[{Most[lines],Rest[lines]}]) </code></pre> <p>This all assumes that the pairs of lines you want to compare are adjacent in the list. If they could be any of the lines in your collection, then you first need to generate a list of all possible pairs of lines to be compared, e.g. using Tuples[listOfLines, {2}], instead of the Transpose function above.</p> <p>Ok, putting this all together:</p> <pre><code>f = If[(( #2[[2,2]]-#1[[-2,2]])/(#2[[2,1]]-#1[[-2,1]])) == (( #1[[-1,2]]-#1[[-2,2]])/(#1[[-1,1]]-#1[[-2,1]])), {#1[[-2]],#2[[2]]}] &amp; ; FixedPoint[f @@@ #, Tuples[Sort[listOfLines],{2}] ] </code></pre> <p>I have broken out the Step 2 test-and-replace function into a named pure function so that the #s don't get confused. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload