{"id":25834,"date":"2017-10-25T20:58:56","date_gmt":"2017-10-25T15:28:56","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25834"},"modified":"2017-10-25T20:58:56","modified_gmt":"2017-10-25T15:28:56","slug":"how-to-check-if-two-given-line-segments-intersect","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/how-to-check-if-two-given-line-segments-intersect\/","title":{"rendered":"How to check if two given line segments intersect?"},"content":{"rendered":"<p>Given two line segments (p1, q1) and (p2, q2), find if the given line segments intersect with each other.<span id=\"more-120479\"><\/span><\/p>\n<p>Before we discuss solution, let us define notion of <a href=\"http:\/\/www.geeksforgeeks.org\/orientation-3-ordered-points\/\" target=\"_blank\" rel=\"noopener\"><strong>orientation<\/strong><\/a>. Orientation of an ordered triplet of points in the\u00a0plane can be<br \/>\n\u2013counterclockwise<br \/>\n\u2013clockwise<br \/>\n\u2013colinear<br \/>\nThe following diagram shows different possible orientations of (a, b, c)<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25852\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/orientation11.png\" alt=\"How to check if two given line segments intersect\" width=\"400\" height=\"122\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/orientation11.png 400w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/orientation11-300x92.png 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/p>\n<div id=\"practice\">\n<h4 id=\"ad-typebanner\">[ad type=&#8221;banner&#8221;]<\/h4>\n<h4 id=\"how-is-orientation-useful-here\"><strong>How is Orientation useful here?<\/strong><\/h4>\n<\/div>\n<p>Two segments (p1,q1) and (p2,q2) intersect if and only if\u00a0one of the following two conditions is verified<\/p>\n<p><strong>1.\u00a0<em>General Case:<\/em><\/strong><br \/>\n\u2013 (p1, q1, p2) and (p1, q1, q2) have different\u00a0orientations and<br \/>\n\u2013 (p2, q2,\u00a0p1) and (p2, q2,\u00a0q1) have different\u00a0orientations.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25856\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/GeneralCaseExamples1.png\" alt=\"How to check if two given line segments intersect\" width=\"500\" height=\"167\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/GeneralCaseExamples1.png 500w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/GeneralCaseExamples1-300x100.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25854\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/examplesGeneralCase211.png\" alt=\"How to check if two given line segments intersect\" width=\"500\" height=\"193\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/examplesGeneralCase211.png 500w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/examplesGeneralCase211-300x116.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>2. <em>Special Case <\/em><\/strong><br \/>\n\u2013 (p1, q1, p2), (p1, q1, q2), (p2, q2, p1), and (p2, q2, q1) are\u00a0all collinear and<br \/>\n\u2013\u00a0the x-projections of (p1, q1) and (p2, q2) intersect<br \/>\n\u2013 the y-projections of (p1, q1) and (p2, q2) intersect<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25860\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/examplesSpecialCase1.png\" alt=\"How to check if two given line segments intersect\" width=\"400\" height=\"128\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/examplesSpecialCase1.png 400w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/examplesSpecialCase1-300x96.png 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Following is C++ implementation based on above idea.<\/p>\n<div>\n<div id=\"highlighter_925795\" class=\"syntaxhighlighter nogutter c\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\">\n<p><code class=\"c comments\">\/\/ A C++ program to check if two given line segments intersect<\/code><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include &lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>struct Point<br\/>{<br\/>    int x;<br\/>    int y;<br\/>};<br\/> <br\/>\/\/ Given three colinear points p, q, r, the function checks if<br\/>\/\/ point q lies on line segment &#039;pr&#039;<br\/>bool onSegment(Point p, Point q, Point r)<br\/>{<br\/>    if (q.x &lt;= max(p.x, r.x) &amp;&amp; q.x &gt;= min(p.x, r.x) &amp;&amp;<br\/>        q.y &lt;= max(p.y, r.y) &amp;&amp; q.y &gt;= min(p.y, r.y))<br\/>       return true;<br\/> <br\/>    return false;<br\/>}<br\/> <br\/>\/\/ To find orientation of ordered triplet (p, q, r).<br\/>\/\/ The function returns following values<br\/>\/\/ 0 --&gt; p, q and r are colinear<br\/>\/\/ 1 --&gt; Clockwise<br\/>\/\/ 2 --&gt; Counterclockwise<br\/>int orientation(Point p, Point q, Point r)<br\/>{<br\/>    \/\/ See http:\/\/www.geeksforgeeks.org\/orientation-3-ordered-points\/<br\/>    \/\/ for details of below formula.<br\/>    int val = (q.y - p.y) * (r.x - q.x) -<br\/>              (q.x - p.x) * (r.y - q.y);<br\/> <br\/>    if (val == 0) return 0;  \/\/ colinear<br\/> <br\/>    return (val &gt; 0)? 1: 2; \/\/ clock or counterclock wise<br\/>}<br\/> <br\/>\/\/ The main function that returns true if line segment &#039;p1q1&#039;<br\/>\/\/ and &#039;p2q2&#039; intersect.<br\/>bool doIntersect(Point p1, Point q1, Point p2, Point q2)<br\/>{<br\/>    \/\/ Find the four orientations needed for general and<br\/>    \/\/ special cases<br\/>    int o1 = orientation(p1, q1, p2);<br\/>    int o2 = orientation(p1, q1, q2);<br\/>    int o3 = orientation(p2, q2, p1);<br\/>    int o4 = orientation(p2, q2, q1);<br\/> <br\/>    \/\/ General case<br\/>    if (o1 != o2 &amp;&amp; o3 != o4)<br\/>        return true;<br\/> <br\/>    \/\/ Special Cases<br\/>    \/\/ p1, q1 and p2 are colinear and p2 lies on segment p1q1<br\/>    if (o1 == 0 &amp;&amp; onSegment(p1, p2, q1)) return true;<br\/> <br\/>    \/\/ p1, q1 and p2 are colinear and q2 lies on segment p1q1<br\/>    if (o2 == 0 &amp;&amp; onSegment(p1, q2, q1)) return true;<br\/> <br\/>    \/\/ p2, q2 and p1 are colinear and p1 lies on segment p2q2<br\/>    if (o3 == 0 &amp;&amp; onSegment(p2, p1, q2)) return true;<br\/> <br\/>     \/\/ p2, q2 and q1 are colinear and q1 lies on segment p2q2<br\/>    if (o4 == 0 &amp;&amp; onSegment(p2, q1, q2)) return true;<br\/> <br\/>    return false; \/\/ Doesn&#039;t fall in any of the above cases<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    struct Point p1 = {1, 1}, q1 = {10, 1};<br\/>    struct Point p2 = {1, 2}, q2 = {10, 2};<br\/> <br\/>    doIntersect(p1, q1, p2, q2)? cout &lt;&lt; &quot;Yes\\n&quot;: cout &lt;&lt; &quot;No\\n&quot;;<br\/> <br\/>    p1 = {10, 0}, q1 = {0, 10};<br\/>    p2 = {0, 0}, q2 = {10, 10};<br\/>    doIntersect(p1, q1, p2, q2)? cout &lt;&lt; &quot;Yes\\n&quot;: cout &lt;&lt; &quot;No\\n&quot;;<br\/> <br\/>    p1 = {-5, -5}, q1 = {0, 0};<br\/>    p2 = {1, 1}, q2 = {10, 10};<br\/>    doIntersect(p1, q1, p2, q2)? cout &lt;&lt; &quot;Yes\\n&quot;: cout &lt;&lt; &quot;No\\n&quot;;<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Output:<\/p>\n<pre>No\r\nYes\r\nNo<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>How to check if two given line segments intersect? &#8211; Geometric Algorithms &#8211; Orientation of an ordered triplet of points in the plane.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,73905],"tags":[75746,75745,75751,75747,75748,75750,75752,75753,75749],"class_list":["post-25834","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-geometric-algorithms","tag-a-line-that-intersects-two-non-intersecting-planes","tag-a-line-that-intersects-two-or-more-lines","tag-intersecting-lines-definition","tag-intersection-of-two-lines-in-3d","tag-line-line-intersection","tag-line-segment-intersection","tag-parallel-lines-definition","tag-perpendicular-line","tag-what-is-the-intersection-of-two-planes"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25834"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25834\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}