Code covered by the BSD License  

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Fast and Robust Curve Intersections

4.9 | 78 ratings Rate this file 451 Downloads (last 30 days) File Size: 4.66 KB File ID: #11837 Version: 1.2
image thumbnail

Fast and Robust Curve Intersections



31 Jul 2006 (Updated )

Computes intersection points of two curves.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

This function computes the (x,y) locations where two curves intersect. The curves can be broken with NaNs or have vertical segments. It is also very fast (at least on data that represents what I think is a typical application).


Curve Intersect 2 inspired this file.

This file inspired Fast And Robust Self Intersections, Gear Shift Map For Automatic Transmission In S Mode, Antarctic Mapping Tools, Fast Line Segment Intersection, Polymorph, and Kirchhoff Vortex Contour Dynamics Simulation.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (112)
28 Apr 2016 Jaime Paredes

Works perfectly

20 Apr 2016 Etpalmer

Working great, finds the intersection correctly and quickly even for a limited number of spaced out points; just what I needed.

Thanks so much!

18 Apr 2016 Els Crijns

Great function! It's easy to work with and very well documented.
I've used it in Matlab R2016a and I got the message bellow:
" Warning: NARGCHK will be removed in a future release. Use NARGINCHK or NARGOUTCHK instead. "

It works when you just apply the suggested fix, but maybe it would be relevant to already update the script as well

16 Apr 2016 oraib alketan

oh man this is crazy good

thanks a lot

06 Apr 2016 Win.Ev

Win.Ev (view profile)

01 Apr 2016 Nipun

Nipun (view profile)

Excellent! Thank you very much!

29 Mar 2016 Arpit Goyal

Very helpful in my coursework! Thank you!!

11 Mar 2016 David deLeon  
15 Feb 2016 Alon Tuchner

Great work. Just what I needed

31 Aug 2015 AVINASH

Hello Douglas, great job! I was desperately looking for a function like this. However, I have to use this function in another language.

Can you please provide supporting information or documents that explain the concept of the function? This would help me in writing the code in other language.


20 Aug 2015 Mark

Mark (view profile)

works like a charm!

11 Aug 2015 Khalid Ali

Amazing work, 5*

05 Aug 2015 harisk87  
10 Jul 2015 Chad Greene

Chad Greene (view profile)

Perfect function. Thanks for sharing.

19 Jun 2015 Farhad Sedaghati  
01 Nov 2014 Douglas Schwarz

Pedro, thanks for the suggestion. I tried bsxfun when I first wrote this and it was not faster at that time. Perhaps things have changed so i will revisit it.

Comment only
31 Oct 2014 Pedro

Pedro (view profile)

Thanks for this functions. It was very helpfull.
I would just remocomend the use of bsxfun instead of repmat, simpy because it's faster. The equivalent would be I believe:
[i, j] = find(bsxfun(@le, min(x1(1:end-1),x1(2:end)), max(x2(1:end-1),x2(2:end)).') & ...
bsxfun(@ge, max(x1(1:end-1),x1(2:end)), min(x2(1:end-1),x2(2:end)).') & ...
bsxfun(@le, min(y1(1:end-1),y1(2:end)), max(y2(1:end-1),y2(2:end)).') & ...
bsxfun(@ge, max(y1(1:end-1),y1(2:end)), min(y2(1:end-1),y2(2:end)).'));

Thanks again, Pedro

27 Oct 2014 Kevin

Kevin (view profile)

11 Jul 2014 Douglas Schwarz

Ilya and Jan, because of floating point arithmetic, it's impossible to find intersections perfectly in all cases. Jan, your example has two curves that touch at a single point; some people might define this as an intersection. Your assertion that (0,0) isn't an intersection is debatable.

Comment only
11 Jul 2014 Jan

Jan (view profile)

It also erroneously finds contact points (no real intersections).
Example: [x0, y0] = intersections([-1,0,-1], [-1,0,1], [1, 0, 1], [-1,0,1], 1);
returns point (0, 0) as intersection point although it isn't.

04 Jul 2014 ENSTA

ENSTA (view profile)

17 Jun 2014 Ilya

Ilya (view profile)

An excellent function! However, if both intersecting curves already include the intersection point, weird results are possible (see the provided example). This problem was already touched by John Mahoney (the results were different).

My test case: several lines pass through one point and every line must eventually include this point. However, it's not known in advance that we have this situation, it simply may appear.

x1=[-0.49313932739246, -0.02127781500161, 0.450583697389237];
y1=[-10.01, 0, 10.01];
x2=[-1.22073877122679, -0.02127781500161, 1.17818314122357];
y2=[10.01, 0, -10.01];
[x,y,seg1,seg2] = intersections(x1, y1, x2, y2)

The case is published because it may be useful to know about. Otherwise I agree that the function works fine in 99,99999999% of cases.

17 Jun 2014 p kung

p kung (view profile)

exactly what i'm looking for.

14 May 2014 deepak gogade

thanks to douglas sir. it is running .

14 May 2014 deepak gogade

where to find intersection.m file

Comment only
13 May 2014 deepak gogade

hello sir i asked a question on mathwork ask the question please help me out this question.
i want to intersection point of curve by the .fig file
</matlabcentral/answers/uploaded_files/12588/Unti.jpg> this is my graph fig. i want to intersection point of efficiency & unit discharge at 100 unit speed & other unit speed. so how we use matlab code finding intersection of this.

Comment only
12 May 2014 Douglas Schwarz

Deepak, you must install intersections.m into a folder that is on your MATLAB path. The error message is simply informing you that MATLAB cannot find the function.

Comment only
12 May 2014 deepak gogade

when i run the code
t= 0:pi/64:3*pi;
y = sin(t);
>> y2=cos(t);
>> [xout,yout] = intersections(t,y,t,y2,1);
set(gca,'xlim',[min(t) max(t)],'ylim',[-1.1 1.1])
hold on

that problem is occured:-

"Undefined function 'intersections' for input arguments of type 'double'."

05 May 2014 Elizabeth Jones

Elizabeth Jones (view profile)

  • 1 file
  • 4.53846
27 Feb 2014 Eric Sampson  
02 Feb 2014 Faraz Oloumi

Thank you!

08 Oct 2013 Ted Shultz

Just what i was looking for! thanks

28 Aug 2013 François Beauducel


23 Apr 2013 Naime

Naime (view profile)

15 Feb 2013 Matthias Hunstig

Does what it should, no problems so far.

20 Jan 2013 Douglas Schwarz


Welcome to the fun of floating-point arithmetic. First, the duplicate points *are* removed, but this is not obvious unless you display all the digits of the result. For your third example, the four "duplicates" are actually all different, but only in the final bit (you can see it with "format hex"). I don't think there's anything I can do to make this work correctly in all cases.

Second, why should something fail if an intersection occurs at a segment boundary? Some people would like to consider this a proper intersection and others not (I know this from several discussions on the topic). As a user, you will have to evaluate the results you get for your particular application.

In many real world applications, where the two curves are formed from real data, this issue never comes up at all. It's (mostly) only from textbook type problems that you'll see this behavior.


Comment only
17 Jan 2013 John Mahoney


Thanks for the nice function.
Could you please clarify the behavior depending on the ROBUST flag?

From the function doc:

% ROBUST (optional) set to 1 or true means to use a slight variation of the
% algorithm that might return duplicates of some intersection points, and
% then remove those duplicates. The default is true, but since the
% algorithm is slightly slower you can set it to false if you know that
% your curves don't intersect at any segment boundaries. Also, the robust
% version properly handles parallel and overlapping segments.

First, based on the first sentence, it would seem that the removal of duplicates is handled by intersections.m. This is not the case.

Second, it would seem that something should fail if the curves *do* intersect at segment boundaries (points specifying the line) and the ROBUST = 0/false is used.
At least for the example below, this is not the case.

Thanks for your help!

% Test intersections function
% Depending on whether ROBUST is true or false, and depending on how points
% defining each line do or don't lay (exactly = machine prec?) on the other
% line, the algorithm returns a different number of intersections.

clear all

% % two lines where no point is exactly on other line
% x1 = [0.1, 0.4, 0.5, 0.8];
% y1 = [0.2, 0.5, 0.7, 0.9];
% x2 = [0.1, 0.4, 0.8];
% y2 = [0.9, 0.3, 0.1];

% % first line has point on the other, but not vice versa
% x1 = [0.1, 0.25, 0.5, 0.8];
% y1 = [0.2, 0.6, 0.7, 0.9];
% x2 = [0.1, 0.4, 0.8];
% y2 = [0.9, 0.3, 0.1];

% % two lines that share a common point
% x1 = [0.1, 0.4, 0.5, 0.8];
% y1 = [0.2, 0.3, 0.7, 0.9];
% x2 = [0.1, 0.4, 0.8];
% y2 = [0.9, 0.3, 0.1];

robust = 0;
[xinter, yinter, iinter, jinter] = intersections(x1, y1, x2, y2, robust);

hold on
scatter(xinter, yinter, 200, 'r','filled')
plot(x1, y1, '.b-','markersize',30)
plot(x2, y2, '.g-','markersize',20)
text(0.5, 0.5, ['found ', num2str(numel(xinter)), ' intersection/s'], 'fontsize',14)

Comment only
27 Sep 2012 joo

joo (view profile)

did anyone used this code just for one curve? could you help me with the transformation? i am new in matlab, and i can't achieve a working code for just one curve. thank you so so much.

Comment only
26 Sep 2012 joo

joo (view profile)

30 Apr 2012 R

R (view profile)

16 Jan 2012 Murat Shagirov  
18 Dec 2011 Douglas Schwarz


Thank you for your comments. First of all, you should know that the definitions for I and J were specifically requested by users -- they were not included at all in early versions of the program. I have no opinion about them since I don't use them myself. Certainly, backwards compatibility is important so they are not going to change.

I would encourage you to write a simple wrapper function that runs intersections and then converts its outputs into whatever you require. I hesitate to do that for you since I'm not completely sure what it is you need, but feel free to contact me via email if you need help with it.


Comment only
16 Dec 2011 Paul

Paul (view profile)

This is an absolutely fantastic piece of code. 5* well deserved. Thank you!

That said I really regret writing this but...

To me the returned values for I and J feel awkward. Because X0 and Y0 return the intersection points in terms of the original space in which the curves lie (X,Y), I would expect the second set of returned values to give coordinates in terms of the curves themselves. So if a curve has (Euclidean) length 4.8, then I would expect 2.4 to be halfway the curve, not at 4 tenth of the second line segment.

This way we could also compute derivatives and integrals along a curve and interpolate these values simply by looking op the value at I (or J for that matter). Furthermore, the distance along the curve is a nice linear function, whereas the currently returned values for I and J are piecewise linear and non-differentiable it the points.

I understand that such a change of I and J would not be backwards compatible, and also it might be slightly (but hardly) more involved to compute, so I see the big arguments against. Nevertheless, I would be very happy if you would at least give it a thought :-)

17 Nov 2011 David

David (view profile)

Excellent work

08 Nov 2011 Björn

Björn (view profile)

Really good, faster and as useful as polyxpoly.

06 Oct 2011 Harrison  
06 Sep 2011 KYAW KYAW

Dear Sir/Madam,

I am looking for the intersection of dynamic waves and base line. SO far, I could manage to get few of the intersection points using ratio (Y data of dynamic wave/ Y data of base line) for whole of samples. Pls advise me how should I continue since I was stuck in here so long.

I used your function but mine is dynamic wave form and base straing line so the X -axis is not constant line. So the array is only (1900x1) for wave form and base line. The Y values of wave form is changing but the base line is seems like constant. Let say, there is 64 intersection points visuality but what i can get is only 47 but some are not accutare positions.

Thanks and best regards

Comment only
19 Aug 2011 Floris

Floris (view profile)

Very nice work. Thanks for sharing.

I agree that the index identification is a very useful extra feature.

16 Aug 2011 Brock

Brock (view profile)

Very nice work. The option to identify index pairs between which the intersection points lie is a nice touch. This saves me several hours of work, and the code has worked great on my test cases so far. Thanks so much!

11 Aug 2011 Douglas Schwarz


It just occurred to me that you can use the 3rd and 4th output arguments to tell you in which line segments the intersections lie. See the help for more info.


Comment only
09 Aug 2011 Douglas Schwarz


No, it's not random.

For the NON-ROBUST option:
Number the line segments of your polygon from 1 to N, as defined by the order of the x- and y-coordinates. For each P1P2 line segment, the intersections are found in order of the polygon segments. In other words, if P1P2 crosses polygon segments 12 and 25, the intersection with segment 12 will be first followed by the intersection with segment 25, because 12 < 25.

For the ROBUST option:
Because unique() is run on the intersections, they end up being sorted in increasing order of the x-coordinate (with any tie broken by sorting y).


Comment only
08 Aug 2011 Jack

Jack (view profile)


I've got a pollygon that encloses the origin and I have 2 points P1 and P2 outside the pollygon such that the line P1P2 intersects the pollygon in 2 points, I1 and I2, that I determine using the 'intersections' function. Then I rotate my line P1P2 by 10 degrees 36 times and I determine the intersection points with the pollygon for each angle. The results are stored in 2 vectors, x0 and y0, each with 2 components. Now,what I am trying to figure out here is how does the function choose to allocate the numerical values of the coordinates of the 2 points to x0(1) and x0(2) respectively?More explicitly, what does x0(1) represent, the x coordinate of which one of the 2 intersection points? Is it random? Cheers!

Comment only
13 Jul 2011 Daniel

Daniel (view profile)

Using in combination with polymorph(), thanks!

10 Jun 2011 Tom

Tom (view profile)

Thank you so much for sharing this wonderful code with us.

One small issue though, I found a case where instead of detecting 3 intersections it finds 5. Perhaps it is pathological but let me know if you would like more info.

Comment only
10 Jun 2011 Vooi Leng Tan

Thanks a million!

02 Jun 2011 Jaime

Jaime (view profile)

Thanks a lot bruder !!!

09 Mar 2011 Douglas Schwarz

NURUL, please send me an example.

Comment only
09 Mar 2011 NURUL

NURUL (view profile)

Hi, I have used this code to find the coordinates intersection between 2 vectors, where X1, Y1 is my latitude and longitude for vector 1, and X2,Y2 is my latitude and longitude for vector 2.
Previously this function works very well in my data, but now, I am not so sure why it is sometimes cannot find the intersections, even it is implemented on the same datasets. I am really wondering why?

Comment only
20 Dec 2010 Douglas Schwarz


You have defined two curves consisting of just one line segment each. These two segments do not intersect so that is why my function returns empty vectors.

In order to find the intersection of the two infinite straight lines defined by the segments you can set

x1=[627 619];
y1=[338 288];
x2=[621 627];
y2=[351 339];

and then follow the calculations in the comments in intersections.m. Set up the A and B matrices and then solve with T = A\B. Since you are looking for an intersection outside the endpoints you can ignore t1 and t2 and just look at x0 and y0.


Comment only
18 Dec 2010 Laura

Laura (view profile)

Dear Douglas,

My question: is it possible to obtain with you code the intersection of 2 curves if their intersection is outside the endpoints given?
thank you!

Comment only
18 Dec 2010 Laura

Laura (view profile)

I have asked my question wrongly. The intersection is outside the interval of the selected points:

C1=(627,338),(619,288 )
C2= (621,351), (627,339)

because this gives empty for me and it should intersect

Comment only
30 Nov 2010 Diarmuid

Excellent code ... well written, fast and above all it works!!! Has saved me a huge amount of coding time!

28 Nov 2010 Manuel

Manuel (view profile)

Thanks for the file. Saved me lines of code and hours of computing time.

11 Aug 2010 Rebecca

The function seems great but I get an error about the try/catch loop syntax for my version of matlab.
changing line 218 to "catch, err" avoids the error.

04 Aug 2010 David

David (view profile)

May I suggest an improvement to your code.

Currently the code checks for intersecting domains and then for intersecting domain checks if the lines actually cross. This op takes 16 flops (that I count) per pair of lines, with then the LU decomp and back substitution for a 4 by 4 system of equations where the domains do cross.

However if the intersection condition is stated in terms of L1 = O1 + k1*D1 and L2 = O2 + k2*D2 where L1 == L2, you are left with an equation in 2 unknowns (k1 and k2). This involves ~7 operations to find k1 and k2, with four more to find x0 and y0.

N.B. Your implementation of this was far faster than my one using matlab classes (of the order of 3000-4000 times faster) :).



08 Jul 2010 Evgeny Pr

Evgeny Pr (view profile)

2Douglas Schwarz
Thank you! Thank you! Thank you!

2Truc Phan
1. Not all have the Mapping Toolbox.
2. Polyxpoly does not work when you want to find the intersection curve itself (loops in curve).

10 Jun 2010 Andrey

Andrey (view profile)

Reliable and robust, but it takes 6.6 seconds for two 10,000 point-functions with ~20 intersections, with the robust flag off. May be I can figure out how to make it less robust, but a bit faster.

19 Apr 2010 Truc Phan

Thanks for sharing.

However, can you please tell us the major difference and/or advantage between this function and the polyxpoly function in Mapping Toolbox. Thanks

Comment only
09 Apr 2010 U. Murat Erdem

Excellent work!

I have submitted a code doing similar task but faster. You can find it here:

I would appreciate your feedback.

11 Mar 2010 Rui Xavier

Ya thanks Doug. Didnt have your email address so contacted you with a private message on matlab central itself. If you can give me you email i can mail you our code so far & ear images as an attachment so you can get a better idea. Thanks

Comment only
08 Mar 2010 Douglas Schwarz

Rui, please email me directly with details of your problem and I will see if I can help.

Comment only
08 Mar 2010 Rui Xavier

Hi douglas!! i am currently doing a final year project on ear recognition.the idea of finding the feature vector to be trained involves finding the distances of a centroid from the intersection points(lines have been drawn at different angles to intersect a edge detected ear image)..but we are unable to find the coordinates of the intersections.hoping u can help us out asap

thanking u in anticipation

Comment only
01 Mar 2010 Shadab Khan

Excellent! It does exactly what is needed and saved so much of time. I am using this function heavily, it saved 300 lines of my code which was inefficient in finding the intersection points.

A suggestion to all: Ignore the post by M MA, this function works perfect, and is fast as stated by Douglas.

Thanks! And i hope you will help us all with such great functions in the future.


09 Feb 2010 Joshua Arnott  
27 Jan 2010 Douglas Schwarz

Arrgh! I actually did exactly that and then removed it thinking (mistakenly) that it wasn't needed. I ended up using reshape(i,[],1) rather than i(:) because it is documented to force the second dimension to be 1 even if i starts out as 0-by-0. i(:) does the right thing today, but maybe not in the future. Thanks again, Rodrigo! (New version just uploaded.)

Comment only
27 Jan 2010 Rodrigo Portugal

there is still a little bug related to mismatching dimensions of empty matrices. When we try, for instance,
[xi,yi,i,j] = intersections([0,1],[0,0,],[1,0],[1,1])
that's OK. However, if we try
[xi,yi,i,j] = intersections([0,1],[0,0,],[1,0.5,0],[1,0.5,1])
there is the error "Matrix dimensions should agree", because empty matrices should have compatible dimensions, even though they are empty entities.

So I have look at the code, and I have inserted the following code
i = i(:); j = j(:);
just after "i" and "j" definitions at line 165. In this way it seems that this little annoying bug was removed. Could you check it please?

Personally (and strangely) I am very pleased about this bug, because reading the help on empty matrices I've learned that they have a strong operational structure underneath. As a matter of fact, they behave like non-empty matrices, when we try to make math operations on them!

Congratulations and thanks for the prompt response.


25 Jan 2010 Douglas Schwarz

Rodrigo, I agree. I just fixed it and uploaded a new version -- it should appear soon. Thanks for the report.

Comment only
12 Jan 2010 Rodrigo Portugal

A really really excellent function. I'm using all the time. However I have a suggestion. When the curves do not interecept, like in this example
[xi,yi,i,j] = intersections([0,1],[0,0,],[1,0],[1,1])
instead to produce an error, it would be better, in my opinion, if it produces empty matrices as an output. This is partially done when the function is called with two outputs, such as,
[xi,yi] = intersections([0,1],[0,0,],[1,0],[1,1])
but not with four. I give you five points anyway!

05 Dec 2009 Philips

An excellent function. Met my needs 100%. Thanks for making life easier for me.

24 Nov 2009 Sören Sieberling


Comment only
17 Nov 2009 Olga Avila

Found the link for this on the following thread <>
It was just what I needed :)

29 Sep 2009 Nik F

Nik F (view profile)

28 Aug 2009 Theresa

Excellent file. It works perfectly, and extremely quickly. Just what I was looking for!

28 Jul 2009 Douglas Schwarz

Yuri, no problem! I'm glad it's resolved and that you're still following this thread. -Doug

Comment only
28 Jul 2009 Yuri K

Yuri K (view profile)

Doug, thanks and sorry for my stupid mistake and your time. I didn't test it yet, but believe that was the problem.

28 Jul 2009 Douglas Schwarz

It bothered me that I never resolved Yuri's issue so I just took another look at it and realized that his code has a bug. When he gets X2 and Y2 he is using the handle H1 instead of H2. After fixing that there is no problem.

Comment only
08 Apr 2009 Douglas Schwarz


Please email me a mat-file and m-file that demonstrate what you are saying and I will investigate further.


Comment only
08 Apr 2009 Yuri K

Yuri K (view profile)

Douglas, thank you for comment.
That was exactly my point. At smaller N the polygons don't touch, but the function does find an intersection. If I use robust as true, much more intersection points on the 1st polygon are found.
At high N many points from two circles are actually very close, almost ideal touch. (Although I confirm there is a small gap at very close zoom in.) But at let's say N=1000, nothing found at robust=0, and 923 points all around the 1st circle at robust=1. Why is it?

Comment only
08 Apr 2009 Douglas Schwarz


If you zoom in on the supposed intersection point I think you'll find that there is no intersection at all. Remember, those circles are made up of line segments and so are actually polygons with N sides. For most values of N the two polygons won't even touch. In fact, the closest they can ever come is to touch at just one point and because of floating point round-off effects, my routine may or may not find that point.


Comment only
07 Apr 2009 Yuri K

Yuri K (view profile)

Great function, works perfectly in almost all cases. However I found situation when it gives unpredictable result.

I draw 2 circles (using famous CIRCLE function from FEX), which touch each other.
hold on
H1=circle([0 0],5,N);
H2=circle([0 10],5,N);
hold off
axis equal

It doesn't find intersection even with larger N and returns many warnings like
Warning: Matrix is singular to working precision.
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 1.559090e-016.

If I use N=10 or N=20 it actually finds intersection but in a weird place, specially at 20.

Thanks a lot anyway. 4 points.

23 Feb 2009 Jeremy Bruch


Fanstastic job on this. The inclusion of indices is a great convenience as well. Thank you for sharing this code and continuing to improve on your work.


04 Dec 2008 Thomas

Thomas (view profile)

Great code, works perfectly, and fast.

21 Feb 2008 W. H. Brave

Excellent, thank you.

03 Jan 2008 Levi Kilcher


Thanks so much Doug.

FYI: the reason for wanting a index is for the case where you have a set of spatial vectors (x1,y1) and (x2,y2) but you also have a third set of data (i.e. t1) which is in one-to-one correspondence with (x1,y1). The indices which you have now included make it trivial to obtain the value of t1 where (x1,y1) and (x2,y2) intersect.

Hopefully this make sense.

Thanks again,

18 Dec 2007 Douglas Schwarz


I'm not sure what use that would be, but it was trivial to implement and doesn't hurt the execution time so I have no objection to adding it. I have just uploaded a new version.


Comment only
17 Dec 2007 Levi Kilcher

This is a great function. Best intersection routine out there so far. No doubt.

One thing is missing though:
Returning the indices of the intersection. I think that Tim Meyers got this piece right with "intxy" ID:15239. He used floating point indices to indicate the index (read his description). However, his routine does not return all intersections (only the first), and I would guess it is not as fast/robust as this one.

I haven't looked through this function long enough to figure out how difficult such an addition would be. However, I will say that this routine would (IMHO) become MUCH more powerful/useful if some sort of index returning were included.

I'd be happy to work with you on implementing this if you gave me an idea where to start. I've done a bit of thinking about the "intersection problem", but clearly not as much as you.


20 Sep 2007 Heikki Huttunen

Works perfectly.

03 Sep 2007 Scott Miller

This routine works well and quickly.

One general comment for users: "intersections" is a general curve intersection routine, not a polygon itersection routine, although it can be used for this as well. When testing polygons, add the first point of the boundary to the end of the boundary list - "intersections" does not add the last point automatically like "fill", nor is it intended to.


24 Aug 2007 Douglas Schwarz

Gabriel, thanks for the good example. The definition of intersection is debatable and your example only touches at the end points. Nevertheless, I have reverted to using \ in my algorithm instead of linsolve as it seems to be more accurate (at least in this case). This particular failure didn't occur in any of my test cases. The new version should be available shortly.

Comment only
24 Aug 2007 Gabriel Iniesta

The function was working well, but i found no intersection for [X,Y]=intersections([1 6],[1 2],[6 8],[2 2]) when [6 2] actually is an intersection point. Other intersection tools do find this point....

15 Jul 2007 Eduardo GR

The best intersection tool on FC, thanks.

02 May 2007 Ken Garrard

A very nicely written and well documented function that is also efficient. I appreciate the error checking and handling of special cases. The comments make it straightforward to understand how the function works. I need to know which line segments contain intersection points and it was easy to add a couple of statements to return this information ( i(in_range) and j(in_range) ). I can confirm that the function is slower if A is changed from sparse to full. Does anyone know why?

This is exactly the sort of function that I hope to find whenever I browse the FEX.

15 Feb 2007 Andy Dixon

Does exactly what it says on the tin. Excellent!

14 Feb 2007 Chris Barichievy

you're my like a dream; thanks very much

28 Aug 2006 Rickey Chen

Excellent! Just what I am looking for. Thanks a lot!

24 Aug 2006 Brian K

Works very fine.

12 Aug 2006 Peter Navé


11 Aug 2006 Jos vdG

Good job. Well coded and well documented.

09 Aug 2006 John D'Errico

Doug has done a very nice job here, writing a code that is both fast and carefully constructed, watching out for degeneracies. Excellent explanations internally, in case you wish to know how he did it. Ignore the sour grapes review by M Ma. Or, if you prefer, consider the following comparison:

n = 100;
x1 = rand(1,n);
x2 = rand(1,n);
y1 = rand(1,n);
y2 = rand(1,n);

Elapsed time is 0.282314 seconds.

Elapsed time is 2.219815 seconds.

Both codes found the same points of intersections (2183 of them in total.) But intersections was considerably faster.

02 Aug 2006 Douglas Schwarz

I'm sorry you felt you needed to give my function such a low rating, but it is not doing quite the same thing as yours (meetpoint). Comparing test suites is a little unfair under these circumstances. Nevertheless, I have attempted to make my function behave better when presented with pathological data. Speed and accuracy with real data are my priorities. I have just uploaded a new version -- it should be available soon.

Comment only
01 Aug 2006 M MA

Well, the function meetpoint ( already works with vertical lines... but the rate 2 is because I found one error at the first test!! using the example in meetpoint:
x1=[0 1.5 3 5 5];
y1=[0 1.5 1.5 2 1];
x2=[.2 .5 .5 1 1 2 2 5 5];
y2=[.2 .5 .8 1 1.3 1.3 1.8 1.8 .5];

The output of meetpoint is:
>> [x' y']
ans =
0.5000 0.5000
1.0000 1.0000
1.3000 1.3000
2.0000 1.5000
4.2000 1.8000
5.0000 1.8000

While the output of intersections is:
>> [x0 y0]
ans =
0.5000 0.5000
1.0000 1.0000
1.3000 1.3000
2.0000 1.5000
4.2000 1.8000

So, it looks like its not handling vertical lines (both) correctly as meetpoint does!
Moreover, meetpoint also returns the slopes at the intersections!

01 Aug 2006 A. Silverstone

Finally! A code that will work with vertical lines. Perfect timing on posting this too! Thanks!!!!

02 Aug 2006

Based on comment from M MA I added code to handle difficult situations such as colinear line segments. Also, I updated the documentation.

04 Aug 2006

Added screenshot.

07 Aug 2006

Fixed a minor bug related to handling NaNs in the curves.

09 Aug 2006

Forgot to reset warning state when no error occurred -- now fixed.

01 Apr 2007

Fixed bug when more than one anomaly present in data.

01 May 2007

Added robustness option at the expense of a little speed -- on by default, but can be turned off.

25 Jun 2007

Fixed bug where parallel segments were incorrectly identified as overlapping. Increased speed of non-robust algorithm. Other internal changes.

27 Aug 2007

Reverted to using \ instead of linsolve as it seems to be more accurate.

18 Dec 2007

Added additional outputs which indicate in which segment pairs the intersections lie.

25 Jan 2010 1.1

Fixed bug identified by Rodrigo Portugal. No longer fails when there are no intersections and you need 4 outputs.

27 Jan 2010 1.2

Fixed bug that my previous "bug fix" failed to fix.

Contact us