{"id":277,"date":"2009-05-07T18:53:26","date_gmt":"2009-05-08T01:53:26","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1301"},"modified":"2009-05-07T18:53:26","modified_gmt":"2009-05-08T01:53:26","slug":"point-in-polygon","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=277","title":{"rendered":"Point in Polygon"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ function found here:\r\n\/\/ http:\/\/www.alienryderflex.com\/polygon\/\r\nfunction pointInPolygon(x:Number, y:Number, polyX:Array, polyY:Array):Boolean{\r\n\tvar j:int = polyX.length - 1;\r\n\tvar oddNodes:Boolean = false;\r\n\tfor (var i:int=0; i &lt; polyX.length; i++) {\r\n    if (polyY&#x5B;i] &lt; y &amp;&amp; polyY&#x5B;j] &gt;= y ||  polyY&#x5B;j] &lt; y &amp;&amp; polyY&#x5B;i] &gt;= y) {\r\n\r\n      if (polyX&#x5B;i] + (y - polyY&#x5B;i]) \/ (polyY&#x5B;j] - polyY&#x5B;i]) * (polyX&#x5B;j] - polyX&#x5B;i]) &lt; x) {\r\n        oddNodes = !oddNodes; \r\n      }\r\n    }\r\n    j = i; \r\n  }\r\n  return  oddNodes;\r\n}\r\n\r\n\/\/ draw an overly complex poly and store all  x y coords\r\nvar pX:Array= new Array();\r\nvar pY:Array = new Array();\r\ngraphics.beginFill(0xCC0000);\r\nfor (var i:int = 0; i&lt;60; i++){\r\n\tpX&#x5B;i] = Math.random()*stage.stageWidth;\r\n\tpY&#x5B;i] =Math.random()*stage.stageHeight;\r\n\tif (i == 0){\r\n\t\tgraphics.moveTo(pX&#x5B;i], pY&#x5B;i]);\r\n\t}else{\r\n\t\tgraphics.lineTo(pX&#x5B;i], pY&#x5B;i]);\r\n\t}\r\n}\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\t   alpha = 1;\r\n       if (pointInPolygon(mouseX, mouseY, pX, pY)){\r\n\t\t    alpha = .5;\r\n\t   }\r\n}\r\n<\/pre>\n<p>This snippet shows how to test to see if a point is inside a polygon. This may not really be useful as hitTestPoint() is likely faster &#8211; however this could be modified for other purposes such as polygon to polygon collision. This algorithm works nicely on all kinds of polygons &#8230; convex, concave etc&#8230;<\/p>\n<p>I found this algorithm in an article by Darel Rex Finley &#8230; <a href=\"http:\/\/www.alienryderflex.com\/polygon\/\" target=\"blank\">here.<\/a><\/p>\n<p>I&#8217;ve used this function in Processing a few times&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ function found here: \/\/ http:\/\/www.alienryderflex.com\/polygon\/ function pointInPolygon(x:Number, y:Number, polyX:Array, polyY:Array):Boolean{ var j:int = polyX.length &#8211; 1; var oddNodes:Boolean = false; for (var i:int=0; i &lt; polyX.length; i++) { if (polyY&#x5B;i] &lt; y &amp;&amp; polyY&#x5B;j] &gt;= y || polyY&#x5B;j] &lt; y &amp;&amp; polyY&#x5B;i] &gt;= y) { if (polyX&#x5B;i] + (y &#8211; polyY&#x5B;i]) \/ (polyY&#x5B;j] &#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114,94],"tags":[3,4],"class_list":["post-277","post","type-post","status-publish","format-standard","hentry","category-graphics-algorithms","category-misc","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=277"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/277\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}