# Detect Curve Intersections, Quickly and Easily

Brett's Pick this week is "Fast and Robust Curve Intersections," by Douglas Schwarz.

If you've ever needed to find the intersections between (possibly complicated) curves, this file is for you. We've come to expect great things from Doug, and this file is no exception. I really like the interface to this function; it's very easy to use, and well written and documented. I also like Doug's responsiveness to his reviewers' comments; he modified the file to accommodate use cases suggested by a few different authors.

Suppose you had two sinusoids of different phase and amplitude, one of which is very noisy:

t= 0:pi/64:3*pi;
y = sin(t);
y2 = 0.6*sin(t-0.7)+0.1*randn(size(y));

intersections makes finding the intersection points very easy.

[xout,yout] = intersections(t,y,t,y2,1);
plot(t,y,'linewidth',2)
set(gca,'xlim',[min(t) max(t)],'ylim',[-1.1 1.1])
hold on
plot(t,y2,'g','linewidth',2)
plot(xout,yout,'r.','markersize',18)

I should note that there are other detectors of curve intersections on the File Exchange. I didn't have time to look at all of them, but you can browse them here. Also, Doug's submission inspired or was used by several other File Exchange submissions.

Loren and Lucio recently provided a thoughtful discussion of methods used for the detection of line-segment intersections in Loren on the Art of MATLAB. You can find those posts here and here.

Oh, and that cool on-figure magnifier? I used a previous Pick-of-the-Week submission for that!