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).
Thanks for amazing code, do you have any solution for finding the intersection for very large size of arrays? my 2 curves consist of 90,000 elements and repmat gives a memory error ... I cannot reduce my time increment since it affects the accuracy of method
My function does not assume that the curves you specify are closed. To see what I mean, plot your two curves with
You'll see that the line only intersects the curve at one point. To get an intersection at the second point you will have to close the curve A by replicating the first point as the last point.
>> A2 = [A;A(1,:)];
>> [x0,y0] = intersections(A2(:,1),A2(:,2),B,C,1)
Now you have your two desired intersections.
When looking for the intersection between shape A, with vertical line that passes through BC, I get only one intersection point, whereas if you plot the data, it should be two. Can you please explain this?
when sections of both curves overlap, the output is the midpoint of the overlapping region. What's missing is the fractional index of that point (third and fourth function output). Somewhere between lines 227 and 236 T(1,overlap) and T(2,overlap) have to be set, otherwise they stay NaN.
Apart from that rather special case great function!
Also please check the tags on this file, there are some commas missing.
Looks like MATLAB is moving underneath, so time for an update.
[xnew,ynew] = intersections(x,y1,x,y2)
Warning: NARGCHK will be removed in a future release. Use NARGINCHK or NARGOUTCHK instead.
> In intersections (line 91)
straight forward and fast, thanks!
Working great, finds the intersection correctly and quickly even for a limited number of spaced out points; just what I needed.
Thanks so much!
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
oh man this is crazy good
thanks a lot
Excellent! Thank you very much!
Very helpful in my coursework! Thank you!!
Great work. Just what I needed
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.
works like a charm!
Amazing work, 5*
Perfect function. Thanks for sharing.
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.
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
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.
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.
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.
exactly what i'm looking for.
thanks to douglas sir. it is running .
where to find intersection.m file
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.
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.
when i run the code
y = sin(t);
>> [xout,yout] = intersections(t,y,t,y2,1);
set(gca,'xlim',[min(t) max(t)],'ylim',[-1.1 1.1])
that problem is occured:-
"Undefined function 'intersections' for input arguments of type 'double'."
Just what i was looking for! thanks
Does what it should, no problems so far.
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.
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.
% % 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);
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)
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.
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.
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 :-)
Really good, faster and as useful as polyxpoly.
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
Very nice work. Thanks for sharing.
I agree that the index identification is a very useful extra feature.
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!
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.
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).
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!
Using in combination with polymorph(), thanks!
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.
Thanks a million!
Thanks a lot bruder !!!
NURUL, please send me an example.
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?
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
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.
My question: is it possible to obtain with you code the intersection of 2 curves if their intersection is outside the endpoints given?
I have asked my question wrongly. The intersection is outside the interval of the selected points:
C2= (621,351), (627,339)
because this gives empty for me and it should intersect
Excellent code ... well written, fast and above all it works!!! Has saved me a huge amount of coding time!
Thanks for the file. Saved me lines of code and hours of computing time.
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.
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) :).
Thank you! Thank you! Thank you!
1. Not all have the Mapping Toolbox.
2. Polyxpoly does not work when you want to find the intersection curve itself (loops in curve).
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.
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
I have submitted a code doing similar task but faster. You can find it here:
I would appreciate your feedback.
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
Rui, please email me directly with details of your problem and I will see if I can help.
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
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.
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.)
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.
Rodrigo, I agree. I just fixed it and uploaded a new version -- it should appear soon. Thanks for the report.
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!
An excellent function. Met my needs 100%. Thanks for making life easier for me.
Found the link for this on the following thread <http://www.mathworks.de/matlabcentral/newsreader/view_thread/258260#672305>
It was just what I needed :)
Excellent file. It works perfectly, and extremely quickly. Just what I was looking for!
Yuri, no problem! I'm glad it's resolved and that you're still following this thread. -Doug
Doug, thanks and sorry for my stupid mistake and your time. I didn't test it yet, but believe that was the problem.
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.
Please email me a mat-file and m-file that demonstrate what you are saying and I will investigate further.
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?
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.
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.
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.
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.
Great code, works perfectly, and fast.
Excellent, thank you.
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.
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.
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.
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.
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.
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....
The best intersection tool on FC, thanks.
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.
Does exactly what it says on the tin. Excellent!
you're my hero.works like a dream; thanks very much
Excellent! Just what I am looking for. Thanks a lot!
Works very fine.
Good job. Well coded and well documented.
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.
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.
Well, the function meetpoint (http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8997&objectType=FILE) 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']
While the output of intersections is:
>> [x0 y0]
So, it looks like its not handling vertical lines (both) correctly as meetpoint does!
Moreover, meetpoint also returns the slopes at the intersections!
Finally! A code that will work with vertical lines. Perfect timing on posting this too! Thanks!!!!
Fixed bug that my previous "bug fix" failed to fix.
Fixed bug identified by Rodrigo Portugal. No longer fails when there are no intersections and you need 4 outputs.
Added additional outputs which indicate in which segment pairs the intersections lie.
Reverted to using \ instead of linsolve as it seems to be more accurate.
Fixed bug where parallel segments were incorrectly identified as overlapping. Increased speed of non-robust algorithm. Other internal changes.
Added robustness option at the expense of a little speed -- on by default, but can be turned off.
Fixed bug when more than one anomaly present in data.
Forgot to reset warning state when no error occurred -- now fixed.
Fixed a minor bug related to handling NaNs in the curves.
Based on comment from M MA I added code to handle difficult situations such as colinear line segments. Also, I updated the documentation.
Inspired by: Curve Intersect 2
Inspired: Fast and Robust Self-Intersections, Fast Line Segment Intersection, Gear shift map for Automatic Transmission in S mode, polymorph, Kirchhoff Vortex Contour Dynamics Simulation, RivMAP - River Morphodynamics from Analysis of Planforms, Antarctic Mapping Tools, anomaly
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.