{"id":270,"date":"2011-03-31T13:22:21","date_gmt":"2011-03-31T13:22:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/03\/31\/points-and-polygons\/"},"modified":"2016-08-04T09:23:26","modified_gmt":"2016-08-04T14:23:26","slug":"points-and-polygons","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/03\/31\/points-and-polygons\/","title":{"rendered":"Points and Polygons"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Ever wanted to know if some points of interest were inside a region?  You can answer that if the region is specified as a\r\n         polygon in MATLAB.  The key is the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/inpolygon.html\"><tt>inpolygon<\/tt><\/a> function.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Polygons<\/a><\/li>\r\n         <li><a href=\"#3\">Some Points of Interest<\/a><\/li>\r\n         <li><a href=\"#4\">Test the Points with the Polygon<\/a><\/li>\r\n         <li><a href=\"#7\">Multiply-connected Polygon<\/a><\/li>\r\n         <li><a href=\"#8\">Do You Work with Points and Polygons?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Polygons<a name=\"1\"><\/a><\/h3>\r\n   <p>Polygons in two dimensions are generally represented in MATLAB with two arrays, locations for the <tt>X<\/tt> vertices and <tt>Y<\/tt> vertices.  There is no need to have the final points in these match the initial points; that is, when arrays as described\r\n      are used in situations where they are interpreted as polygon vertices, the polygon is automatically closed.\r\n   <\/p>\r\n   <p>Let's define a very simple polygon to start.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">X = [0 0.5 1]';\r\nY = [0 0.5 0]';<\/pre><p>And look at it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">patch(X,Y, [0.2, 0.7, 0.8], <span style=\"color: #A020F0\">'edgecolor'<\/span>,<span style=\"color: #A020F0\">'r'<\/span>,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'facealpha'<\/span>, 0.2, <span style=\"color: #A020F0\">'linewidth'<\/span>,2);\r\naxis([0 1 -0.2 1])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/270\/insiderP_01.png\"> <h3>Some Points of Interest<a name=\"3\"><\/a><\/h3>\r\n   <p>Let's create some points of interest now.  Some inside, some outside, and some on the boundary.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xin = [0.3 0.75 0.82]';\r\nyin = [0.25 0.1, 0.05]';\r\nxout = [0.3 0.75 0.82]';\r\nyout = [-0.15 0.6, 0.3]';\r\nxedge = [0.3 0.75 0.82]';\r\nyedge = [0.3 0.25, 0.18]';\r\nhold <span style=\"color: #A020F0\">all<\/span>\r\nplot(xin, yin, <span style=\"color: #A020F0\">'g*'<\/span>, <span style=\"color: #A020F0\">'markersize'<\/span>,5)\r\nplot(xout, yout, <span style=\"color: #A020F0\">'mx'<\/span>, <span style=\"color: #A020F0\">'markersize'<\/span>,9)\r\nplot(xedge, yedge, <span style=\"color: #A020F0\">'bd'<\/span>, <span style=\"color: #A020F0\">'markersize'<\/span>,7)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/270\/insiderP_02.png\"> <h3>Test the Points with the Polygon<a name=\"4\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[inIN onIN] = inpolygon(xin,yin, X, Y)<\/pre><pre style=\"font-style:oblique\">inIN =\r\n     1\r\n     1\r\n     1\r\nonIN =\r\n     0\r\n     0\r\n     0\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[inOUT onOUT] = inpolygon(xout,yout, X, Y)<\/pre><pre style=\"font-style:oblique\">inOUT =\r\n     0\r\n     0\r\n     0\r\nonOUT =\r\n     0\r\n     0\r\n     0\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[inEDGE onEDGE] = inpolygon(xedge,yedge, X, Y)<\/pre><pre style=\"font-style:oblique\">inEDGE =\r\n     1\r\n     1\r\n     1\r\nonEDGE =\r\n     1\r\n     1\r\n     1\r\n<\/pre><h3>Multiply-connected Polygon<a name=\"7\"><\/a><\/h3>\r\n   <p>Here's an example from the help for <tt>inpolygon<\/tt>, with a square containing a square hole.  The outer loop is counterclockwise, the inner clockwise.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xv = [0 3 3 0 0 NaN 1 1 2 2 1];\r\nyv = [0 0 3 3 0 NaN 1 2 2 1 1];\r\nx = rand(1000,1)*3; y = rand(1000,1)*3;\r\nin = inpolygon(x,y,xv,yv);\r\nplot(xv,yv,x(in),y(in),<span style=\"color: #A020F0\">'.r'<\/span>,x(~in),y(~in),<span style=\"color: #A020F0\">'.b'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/270\/insiderP_03.png\"> <h3>Do You Work with Points and Polygons?<a name=\"8\"><\/a><\/h3>\r\n   <p>Have you used <tt>inpolygon<\/tt>?  I'd love to hear the contexts of the problems you solve with it.  \r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_e9623edb48ba46949472d6264742f95e() {\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='e9623edb48ba46949472d6264742f95e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e9623edb48ba46949472d6264742f95e';\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 = 'Loren Shure';\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_e9623edb48ba46949472d6264742f95e()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\ne9623edb48ba46949472d6264742f95e ##### SOURCE BEGIN #####\r\n%% Points and Polygons\r\n% Ever wanted to know if some points of interest were inside a region?  You\r\n% can answer that if the region is specified as a polygon in MATLAB.  The\r\n% key is the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/inpolygon.html\r\n% |inpolygon|> function.\r\n%% Polygons\r\n% Polygons in two dimensions are generally represented in MATLAB with two\r\n% arrays, locations for the |X| vertices and |Y| vertices.  There is no\r\n% need to have the final points in these match the initial points; that is,\r\n% when arrays as described are used in situations where they are\r\n% interpreted as polygon vertices, the polygon is automatically closed.\r\n%\r\n% Let's define a very simple polygon to start.\r\nX = [0 0.5 1]';\r\nY = [0 0.5 0]';\r\n%%\r\n% And look at it.\r\npatch(X,Y, [0.2, 0.7, 0.8], 'edgecolor','r',...\r\n    'facealpha', 0.2, 'linewidth',2);\r\naxis([0 1 -0.2 1])\r\n%% Some Points of Interest\r\n% Let's create some points of interest now.  Some inside, some outside, and\r\n% some on the boundary.\r\nxin = [0.3 0.75 0.82]';\r\nyin = [0.25 0.1, 0.05]';\r\nxout = [0.3 0.75 0.82]';\r\nyout = [-0.15 0.6, 0.3]';\r\nxedge = [0.3 0.75 0.82]';\r\nyedge = [0.3 0.25, 0.18]';\r\nhold all\r\nplot(xin, yin, 'g*', 'markersize',5)\r\nplot(xout, yout, 'mx', 'markersize',9)\r\nplot(xedge, yedge, 'bd', 'markersize',7)\r\nhold off\r\n%% Test the Points with the Polygon\r\n[inIN onIN] = inpolygon(xin,yin, X, Y)\r\n%%\r\n[inOUT onOUT] = inpolygon(xout,yout, X, Y)\r\n%%\r\n[inEDGE onEDGE] = inpolygon(xedge,yedge, X, Y)\r\n%% Multiply-connected Polygon\r\n% Here's an example from the help for |inpolygon|, with a square containing\r\n% a square hole.  The outer loop is counterclockwise, the inner clockwise.\r\nxv = [0 3 3 0 0 NaN 1 1 2 2 1];\r\nyv = [0 0 3 3 0 NaN 1 2 2 1 1];\r\nx = rand(1000,1)*3; y = rand(1000,1)*3;\r\nin = inpolygon(x,y,xv,yv);\r\nplot(xv,yv,x(in),y(in),'.r',x(~in),y(~in),'.b')\r\n%% Do You Work with Points and Polygons?\r\n% Have you used |inpolygon|?  I'd love to hear the contexts of the problems\r\n% you solve with it.  Let me know\r\n% <https:\/\/blogs.mathworks.com\/?p=270#respond here>.\r\n\r\n\r\n    \r\n\r\n\r\n\r\n##### SOURCE END ##### e9623edb48ba46949472d6264742f95e\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Ever wanted to know if some points of interest were inside a region?  You can answer that if the region is specified as a\r\n         polygon in MATLAB.  The key is... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/03\/31\/points-and-polygons\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/270"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=270"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/270\/revisions"}],"predecessor-version":[{"id":1979,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/270\/revisions\/1979"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}