4
$\begingroup$

I have a curve (not a function) as a list of points and I would like to find the intersection of the curve with another curve (another set of points). To be more concrete about the problem I am solving :

The curve is a spiral, whose points are https://drive.google.com/file/d/1TtkKPgIxSKmdeWZDYQ1-Dd2aUvxqGDyF/view?usp=sharing. It looks like :

enter image description here

and I need to find intersection with another curve, say a straight line $y=x$. Is there a way to do this in Mathematica.

A canonical procedure is to find the interpolating function from the points (if this were a function) say $f(x)$ and solve $f-g=0$ to find intersection with the the curve $g(x)$.

How does one do the same for a curve where an interpolating function makes no sense? Other alternatives?

Thanks for reading and any help is appreciated.

Edit: I think one way is to check for intersection of all possible line segments, but the number of checks is $O(n^2)$. Maybe someone in the community could suggest a clever way to remove some of these checks, or suggest another method.

$\endgroup$
2

2 Answers 2

8
$\begingroup$
ListLinePlot[data, 
 MeshFunctions -> {Function[{x, y}, x - y]}, 
 Mesh -> {{0}}, 
 MeshStyle -> Directive[PointSize[Medium], Red], 
 Prolog -> {Orange, InfiniteLine[{{0, 0}, {1, 1}}]},
 PlotRange -> All, ImageSize -> Large]

enter image description here

intersections = Cases[Normal @ llp, Point[x_] :> x, All]

enter image description here

$\endgroup$
3
  • $\begingroup$ Why are the double curly braces needed around zero in Mesh-> {{0}}? The intersections vanish if I remove one of them. $\endgroup$
    – Charlie
    Mar 28, 2022 at 22:08
  • $\begingroup$ @Shubham, See Mesh >> Details for details of the settings for Mesh. $\endgroup$
    – kglr
    Mar 28, 2022 at 22:13
  • $\begingroup$ I see. Thanks for the answer and comment! $\endgroup$
    – Charlie
    Mar 28, 2022 at 22:15
5
$\begingroup$

Another way which does not depend on the order of points.We select the points close to the region y==x.

pts = RandomSample[data];
reg = ImplicitRegion[x == y, {x, y}];
dist = RegionDistance[reg];
inters = Select[pts, dist[#] < .01 &]
ListPlot[{inters, pts}, 
 PlotStyle -> {Directive[PointSize[.02], Red], 
   Directive[AbsoluteThickness[1], Blue]}, AspectRatio -> Automatic]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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