{"id":2927,"date":"2011-09-09T12:47:24","date_gmt":"2011-09-09T12:47:24","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/09\/09\/detect-curve-intersections-quickly-and-easily\/"},"modified":"2022-05-29T20:12:57","modified_gmt":"2022-05-30T00:12:57","slug":"detect-curve-intersections-quickly-and-easily","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/09\/09\/detect-curve-intersections-quickly-and-easily\/","title":{"rendered":"Detect Curve Intersections, Quickly and Easily"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/11837-fast-and-robust-curve-intersections\">\"Fast and Robust Curve Intersections,\"<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22680\">Douglas Schwarz<\/a>.\r\n   <\/p>\r\n   <p>If you've ever needed to find the intersections between (possibly complicated) curves, this file is for you. We've come to\r\n      expect great things from Doug, and this file is no exception. I really like the interface to this function; it's very easy\r\n      to use, and well written and documented. I also like Doug's responsiveness to his reviewers' comments; he modified the file\r\n      to accommodate use cases suggested by a few different authors.\r\n   <\/p>\r\n   <p>Suppose you had two sinusoids of different phase and amplitude, one of which is very noisy:<\/p><pre>t= 0:pi\/64:3*pi;\r\ny = sin(t);\r\ny2 = 0.6*sin(t-0.7)+0.1*randn(size(y));<\/pre>\r\n<ul>\r\n\r\n<ul>\r\n<p><tt>intersections<\/tt> makes finding the intersection points very easy.\r\n   <\/p><pre>[xout,yout] = intersections(t,y,t,y2,1);\r\nplot(t,y,'linewidth',2)\r\nset(gca,'xlim',[min(t) max(t)],'ylim',[-1.1 1.1])\r\nhold on\r\nplot(t,y2,'g','linewidth',2)\r\nplot(xout,yout,'r.','markersize',18)<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/curveintersectionsexample.png\"> <\/p>\r\n   <p>I should note that there are other detectors of curve intersections on the File Exchange. I didn't have time to look at all\r\n      of them, but you can browse them <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange?q=curve+intersections+-polymorph+-gear\">here<\/a>. Also, Doug's submission inspired or was used by several other File Exchange submissions.\r\n   <\/p>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/9344\">Loren<\/a> and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/1858\">Lucio<\/a> recently provided a thoughtful discussion of methods used for the detection of line-segment intersections in <a href=\"https:\/\/blogs.mathworks.com\/loren\/\">Loren on the Art of MATLAB<\/a>. You can find those posts <a href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/08\/29\/intersecting-lines\/\">here<\/a> and <a href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/09\/08\/intersecting-lines-part-2\/\">here<\/a>.\r\n   <\/p>\r\n   <p>Oh, and that cool on-figure magnifier? I used a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26007-on-figure-magnifier\">previous Pick-of-the-Week submission<\/a> for that!\r\n   <\/p>\r\n   <p>As always, <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2927#respond\">comments to this blog post<\/a> are welcome. Or leave a comment for Doug <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/11837-fast-and-robust-curve-intersections#comments\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_469b39f8356448bca8a5b13298715aa0() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='469b39f8356448bca8a5b13298715aa0 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 469b39f8356448bca8a5b13298715aa0';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Robert Bemis';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_469b39f8356448bca8a5b13298715aa0()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.12<br><\/p>\r\n<\/div>\r\n<!--\r\n469b39f8356448bca8a5b13298715aa0 ##### SOURCE BEGIN #####\r\n%% Detect Curve Intersections, Quickly and Easily\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/11837-fast-and-robust-curve-intersections \"Fast and Robust Curve Intersections,\"> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22680 Douglas Schwarz>.\r\n\r\n%%\r\n% If you've ever needed to find the intersections between (possibly\r\n% complicated) curves, this file is for you. We've come to expect great\r\n% things from Doug, and this file is no exception. I really like the\r\n% interface to this function; it's very easy to use, and well written and\r\n% documented. I also like Doug's responsiveness to his reviewers' comments;\r\n% he modified the file to accommodate use cases suggested by a few\r\n% different authors.\r\n\r\n%%\r\n% Suppose you had two sinusoids of different phase and amplitude, one of\r\n% which is very noisy:\r\n\r\n%% \r\n%%\r\n% \r\n%  t= 0:pi\/64:3*pi;\r\n%  y = sin(t);\r\n%  y2 = 0.6*sin(t-0.7)+0.1*randn(size(y));\r\n%\r\n%%\r\n\r\n%%\r\n% |intersections| makes finding the intersection points very easy.\r\n%%\r\n\r\n%%\r\n% \r\n%  [xout,yout] = intersections(t,y,t,y2,1);\r\n%  plot(t,y,'linewidth',2)\r\n%  set(gca,'xlim',[min(t) max(t)],'ylim',[-1.1 1.1])\r\n%  hold on\r\n%  plot(t,y2,'g','linewidth',2)\r\n%  plot(xout,yout,'r.','markersize',18)\r\n% \r\n% \r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/curveintersectionsexample.png>>\r\n% \r\n%%\r\n% I should note that there are other detectors of curve intersections on\r\n% the File Exchange. I didn't have time to look at all of them, but you can\r\n% browse them\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?=curve+intersections+-polymorph+-gear&term=curve+intersections+-polymorph+-gear here>. \r\n% Also, Doug's submission inspired or was used by several other File Exchange submissions.\r\n\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/9344 Loren>\r\n% and <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/1858 Lucio>\r\n% recently provided a thoughtful discussion of methods used for the detection of line-segment intersections in \r\n% <https:\/\/blogs.mathworks.com\/loren\/ Loren on the Art of MATLAB>. You can\r\n% find those posts <https:\/\/blogs.mathworks.com\/loren\/2011\/08\/29\/intersecting-lines\/ here> and <https:\/\/blogs.mathworks.com\/loren\/2011\/09\/08\/intersecting-lines-part-2\/ here>.\r\n\r\n%%\r\n% Oh, and that cool on-figure magnifier? I used a \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26007-on-figure-magnifier previous Pick-of-the-Week submission> \r\n% for that!\r\n\r\n%% \r\n% As always, <https:\/\/blogs.mathworks.com\/pick\/?p=2927#respond comments to this blog post> are welcome. Or leave a\r\n% comment for Doug <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/11837-fast-and-robust-curve-intersections#comments here>.\r\n##### SOURCE END ##### 469b39f8356448bca8a5b13298715aa0\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is \"Fast and Robust Curve Intersections,\" by Douglas Schwarz.\r\n   \r\n   If you've ever needed to find the intersections between (possibly complicated) curves, this... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/09\/09\/detect-curve-intersections-quickly-and-easily\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2927"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2927"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2927\/revisions"}],"predecessor-version":[{"id":15962,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2927\/revisions\/15962"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}