{"id":25878,"date":"2017-10-25T21:10:32","date_gmt":"2017-10-25T15:40:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25878"},"modified":"2017-10-25T21:10:32","modified_gmt":"2017-10-25T15:40:32","slug":"check-given-point-lies-inside-outside-polygon","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/check-given-point-lies-inside-outside-polygon\/","title":{"rendered":"How to check if a given point lies inside or outside a polygon?"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Given a polygon and a point \u2018p\u2019, find if \u2018p\u2019 lies inside the polygon or not. The points lying on the border are considered inside.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25879\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/GeneralCaseExamples1-1.png\" alt=\"How to check if a given point lies inside or outside a polygon?\" width=\"274\" height=\"125\" \/><\/p>\n<p>Following is a simple idea to check whether a point is inside or outside.<\/p>\n<pre><strong>1)<\/strong> Draw a horizontal line to the right of each point and extend it to infinity\r\n\r\n<strong>1)<\/strong> Count the number of times the line intersects with polygon edges.\r\n\r\n<strong>2)<\/strong> A point is inside the polygon if either count of intersections is odd or\r\n   point lies on an edge of polygon.  If none of the conditions is true, then \r\n   point lies outside.\r\n\r\n<\/pre>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25884\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/polygon31.png\" alt=\"How to check if a given point lies inside or outside a polygon?\" width=\"495\" height=\"279\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/polygon31.png 495w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/polygon31-300x168.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/polygon31-470x264.png 470w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/polygon31-215x120.png 215w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/polygon31-414x232.png 414w\" sizes=\"(max-width: 495px) 100vw, 495px\" \/><\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>How to handle point \u2018g\u2019 in the above figure?<\/strong><br \/>\nNote that we should returns true if the point lies on the line or same as one of the vertices of the given polygon.\u00a0To handle this, after checking if the line from \u2018p\u2019 to extreme intersects, we check whether \u2018p\u2019 is colinear with vertices of current line of polygon. If it is coliear, then we check if the point \u2018p\u2019 lies on current side of polygon, if it lies, we return true, else false.<\/p>\n<p>Following is C++ implementation of the above idea.<\/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\">\/\/ A C++ program to check if a given point lies inside a given polygon<br\/>\/\/ Refer http:\/\/www.geeksforgeeks.org\/check-if-two-given-line-segments-intersect\/<br\/>\/\/ for explanation of functions onSegment(), orientation() and doIntersect()<br\/>#include &lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Define Infinite (Using INT_MAX caused overflow problems)<br\/>#define INF 10000<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\/>    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\/>    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\/>    return (val &gt; 0)? 1: 2; \/\/ clock or counterclock wise<br\/>}<br\/> <br\/>\/\/ The 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\/>\/\/ Returns true if the point p lies inside the polygon[] with n vertices<br\/>bool isInside(Point polygon[], int n, Point p)<br\/>{<br\/>    \/\/ There must be at least 3 vertices in polygon[]<br\/>    if (n &lt; 3)  return false;<br\/> <br\/>    \/\/ Create a point for line segment from p to infinite<br\/>    Point extreme = {INF, p.y};<br\/> <br\/>    \/\/ Count intersections of the above line with sides of polygon<br\/>    int count = 0, i = 0;<br\/>    do<br\/>    {<br\/>        int next = (i+1)%n;<br\/> <br\/>        \/\/ Check if the line segment from &#039;p&#039; to &#039;extreme&#039; intersects<br\/>        \/\/ with the line segment from &#039;polygon[i]&#039; to &#039;polygon[next]&#039;<br\/>        if (doIntersect(polygon[i], polygon[next], p, extreme))<br\/>        {<br\/>            \/\/ If the point &#039;p&#039; is colinear with line segment &#039;i-next&#039;,<br\/>            \/\/ then check if it lies on segment. If it lies, return true,<br\/>            \/\/ otherwise false<br\/>            if (orientation(polygon[i], p, polygon[next]) == 0)<br\/>               return onSegment(polygon[i], p, polygon[next]);<br\/> <br\/>            count++;<br\/>        }<br\/>        i = next;<br\/>    } while (i != 0);<br\/> <br\/>    \/\/ Return true if count is odd, false otherwise<br\/>    return count&amp;1;  \/\/ Same as (count%2 == 1)<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    Point polygon1[] = {{0, 0}, {10, 0}, {10, 10}, {0, 10}};<br\/>    int n = sizeof(polygon1)\/sizeof(polygon1[0]);<br\/>    Point p = {20, 20};<br\/>    isInside(polygon1, n, p)? cout &lt;&lt; &quot;Yes \\n&quot;: cout &lt;&lt; &quot;No \\n&quot;;<br\/> <br\/>    p = {5, 5};<br\/>    isInside(polygon1, n, p)? cout &lt;&lt; &quot;Yes \\n&quot;: cout &lt;&lt; &quot;No \\n&quot;;<br\/> <br\/>    Point polygon2[] = {{0, 0}, {5, 5}, {5, 0}};<br\/>    p = {3, 3};<br\/>    n = sizeof(polygon2)\/sizeof(polygon2[0]);<br\/>    isInside(polygon2, n, p)? cout &lt;&lt; &quot;Yes \\n&quot;: cout &lt;&lt; &quot;No \\n&quot;;<br\/> <br\/>    p = {5, 1};<br\/>    isInside(polygon2, n, p)? cout &lt;&lt; &quot;Yes \\n&quot;: cout &lt;&lt; &quot;No \\n&quot;;<br\/> <br\/>    p = {8, 1};<br\/>    isInside(polygon2, n, p)? cout &lt;&lt; &quot;Yes \\n&quot;: cout &lt;&lt; &quot;No \\n&quot;;<br\/> <br\/>    Point polygon3[] =  {{0, 0}, {10, 0}, {10, 10}, {0, 10}};<br\/>    p = {-1,10};<br\/>    n = sizeof(polygon3)\/sizeof(polygon3[0]);<br\/>    isInside(polygon3, n, p)? cout &lt;&lt; &quot;Yes \\n&quot;: cout &lt;&lt; &quot;No \\n&quot;;<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<pre>No\r\nYes\r\nYes\r\nYes\r\nNo\r\nNo<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to check if a given point lies inside or outside a polygon &#8211; Geometric Algorithm &#8211;  To handle this, after checking if the line from \u2018p\u2019 to extreme.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,73905,83569],"tags":[75868,75872,75874,75875,75869,75873,75870,75871],"class_list":["post-25878","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-geometric-algorithms","category-polygon","tag-check-if-a-point-is-inside-a-rectangle","tag-check-if-a-point-is-inside-a-square","tag-check-if-point-is-inside-polygon-javascript","tag-check-if-point-is-inside-rectangle-java","tag-how-to-find-whether-a-point-lies-inside-a-triangle","tag-point-in-polygon-winding-number","tag-point-inside-convex-polygon","tag-point-inside-polygon-python"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25878","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=25878"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25878\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}