2
$\begingroup$

I have got an array of points, which can be represented as a ListLinePlot. Then I am trying to find the number of intersections of this ListLinePlot with a certain line. Obviously, in the demonstrated case the number of intersections should be two, but I get 5 points as a result. RegionIntersection gives the same result. How can I fix that? I can't create and solve the system of equations, representing an intersection condition, because there is a large number of arrays to be investigated and their general appearance is unknown. The problem is that I should know the exact number of intersections. Thanks in advance.

My code is (sol is an above-mentioned array of points):

lst1 = Sort[sol];

lst2 = Table[{x, Max[Sort[sol[[All, 2]]]] - (Max[Sort[sol[[All, 2]]]] - Min[Sort[sol[[All, 2]]]])/5}, 
{x, Min[Sort[sol[[All, 1]]]], Max[Sort[sol[[All, 1]]]], 0.5}];

plot = ListLinePlot[{lst1, lst2}, PlotRange -> All]

l = Graphics`Mesh`FindIntersections@plot

example

The concrete example of the "sol" array:

{{7.05547, -2.46723}, {1.86965, 
  1.1145}, {5.99004, -0.731189}, {3.12849, 
  1.61209}, {7.16644, -2.67133}, {1.74237, 
  0.984025}, {7.02438, -2.41082}, {1.90557, 1.1482}, {5.23746, 
  0.238673}, {3.98266, 1.32496}, {6.38239, -1.32259}, {2.66365, 
  1.57629}, {7.25692, -2.84091}, {1.63982, 
  0.865836}, {7.27641, -2.87781}, {1.61788, 
  0.838986}, {7.27742, -2.87971}, {1.61675, 
  0.83759}, {7.27695, -2.87882}, {1.61728, 
  0.838239}, {7.27717, -2.87925}, {1.61703, 
  0.837928}, {7.27707, -2.87905}, {1.61714, 
  0.838075}, {7.27712, -2.87914}, {1.61709, 
  0.838006}, {7.27709, -2.8791}, {1.61711, 
  0.838039}, {7.2771, -2.87912}, {1.6171, 
  0.838023}, {7.2771, -2.87911}, {1.61711, 
  0.83803}, {7.2771, -2.87911}, {1.6171, 
  0.838027}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 
  0.838028}, {7.2771, -2.87911}, {1.61711, 0.838028}}
$\endgroup$
1
  • $\begingroup$ Provide a set of points that demonstrates this problem. $\endgroup$ – Bob Hanlon Dec 24 '20 at 16:48
4
$\begingroup$

Use the option Graphics`Mesh`AllPoints -> False

Graphics`Mesh`FindIntersections[plot, Graphics`Mesh`AllPoints -> False]
{{4.68871, 0.71373}}

Where do the extra points come from?

Repeated consecutive points count as intersections. There are two such points in lst1:

Select[#[[2]] > 1 &]@Tally[lst1]
{{{1.61711, 0.838028}, 145}, {{7.2771, -2.87911}, 147}}

If you remove duplicates from lst1, we get get a single intersection without having to use the option Graphics`Mesh`AllPoints -> False:

Graphics`Mesh`FindIntersections @
 ListLinePlot[{DeleteDuplicates@lst1, lst2}, PlotRange -> All]
 {{4.68871, 0.71373}}

By the way, RegionIntersection does not include self-intersections and gives a single point:

RegionIntersection[Line@lst1, Line@lst2]
  Point[{{4.68871, 0.71373}}]
$\endgroup$
3
  • $\begingroup$ Thank you for your answer. I've forgotten to mention that I should know the exact number of intersections. $\endgroup$ – Tanya Dec 24 '20 at 17:36
  • $\begingroup$ @Tanya, where should the second intersection be? $\endgroup$ – kglr Dec 24 '20 at 17:43
  • $\begingroup$ Thank you once again! After your addition all became clear. After deleting duplicates everything is alright :) $\endgroup$ – Tanya Dec 24 '20 at 17:43
3
$\begingroup$
line1 = Line[lst1];
line2 = Line[lst2];
RegionIntersection[line1, line2]

(* Point[{{4.68871, 0.71373}}]*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.