{"id":300,"date":"2009-05-29T14:54:25","date_gmt":"2009-05-29T21:54:25","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1462"},"modified":"2009-05-29T14:54:25","modified_gmt":"2009-05-29T21:54:25","slug":"polygon-triangulation","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=300","title":{"rendered":"Polygon Triangulation"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar triangulate:Triangulate = new Triangulate();\r\n\r\nvar poly:Array=&#x5B;];\r\n\r\nstage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);\r\nfunction onDown(evt:MouseEvent):void {\r\n\tpoly.push(new Pnt(mouseX, mouseY));\r\n}\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tvar i:int;\r\n\tgraphics.clear();\r\n\tvar verts:Array=triangulate.process(poly);\r\n\r\n\tif (verts==null) {\r\n\t\t\/\/ draw a red polygon if there is some kind of error,\r\n\t\t\/\/ or if there are too few points on the poly\r\n\t\tif (poly.length&gt;1) {\r\n\t\t\tgraphics.lineStyle(0,0xFF0000);\r\n\t\t\tgraphics.moveTo(poly&#x5B;0].x, poly&#x5B;0].y);\r\n\t\t\tfor (i = 1; i&lt;poly.length; i++) {\r\n\t\t\t\tgraphics.lineTo(poly&#x5B;i].x, poly&#x5B;i].y);\r\n\t\t\t}\r\n\t\t}\r\n\t}else{\r\n\t\t\/\/ draw the triangulated polygon\r\n\t\tvar tcount:int = verts.length \/ 3;\r\n\t\tgraphics.lineStyle(0,0x000000);\r\n\t\tfor (i = 0; i&lt;tcount; i++) {\r\n\t\t\tvar index:int = i * 3;\r\n\t\t\tvar p1:Pnt=verts&#x5B;index];\r\n\t\t\tvar p2:Pnt=verts&#x5B;index+1];\r\n\t\t\tvar p3:Pnt=verts&#x5B;index+2];\r\n\t\t\tgraphics.moveTo(p1.x,p1.y);\r\n\t\t\tgraphics.lineTo(p2.x,p2.y);\r\n\t\t\tgraphics.lineTo(p3.x,p3.y);\r\n\t\t\tgraphics.lineTo(p1.x,p1.y);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>The above demo&#8217;s a class that triangulates a polygon given a list of points. To test this snippet you&#8217;ll need the Pnt and Triangulate Classes, which you can download or copy from below.<\/p>\n<p>Polygon triangulation is useful for lots of things&#8230; I first found that I needed it back in my Director days. I wanted to be able to draw a polygon and then extrude it into 3D space &#8211; in order to do this I needed to triangulate the polygon that was drawn. Luckily there was an undocumented feature in Lingo that did the triangulation. I&#8217;ll probably do an extrusion demo soon. I&#8217;ve been looking into this to make the <a href=\"https:\/\/actionsnippet.com\/?page_id=1391\" target=\"blank\">QuickBox2D<\/a> polygon stuff a little easier.<\/p>\n<p><a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/polygon_triangulation.html\" target=\"blank\"><br \/>\nTake a look at the swf here<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/polygon_triangulated.gif\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/polygon_triangulation.swf&quot;,&quot;title&quot;:&quot;polygon triangulation&quot;,&quot;width&quot;:800,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#ffffff&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: polygon triangulation<\/span><\/button><\/span><\/p>\n<p><a href=\"https:\/\/actionsnippet.com\/source\/polygon_triangulation.zip\">Download all source and fla here&#8230;.<\/a><\/p>\n<p>Here is the Triangulate class:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/**\r\nThis code is a quick port of code written in C++ which was submitted to \r\nflipcode.com by John W. Ratcliff  \/\/ July 22, 2000 \r\nSee original code and more information here:\r\nhttp:\/\/www.flipcode.com\/archives\/Efficient_Polygon_Triangulation.shtml\r\n\r\nported to actionscript by Zevan Rosser\r\nwww.actionsnippet.com\r\n*\/\r\n\r\npackage {\r\n\t\r\n\tpublic class Triangulate {\r\n\t\t\r\n\t\tprivate const EPSILON:Number = 0.0000000001;\r\n\t\t\r\n\t\tpublic function Triangulate(){}\r\n\t\t\r\n\t\tpublic function process(contour:Array):Array{\r\n\t\t\tvar result:Array = &#x5B;];\r\n\t\t\tvar n:int = contour.length\r\n\t\t\tif ( n &lt; 3 ) return null\r\n\t\t\t\r\n\t\t\tvar verts:Array = &#x5B;];\r\n\t\t\t\r\n\t\t\t  \/* we want a counter-clockwise polygon in verts *\/\r\n\t\t\tvar v:int\r\n\t\t\t\r\n\t\t\t  if ( 0.0 &lt; area(contour) ){\r\n\t\t\t\tfor (v=0; v&lt;n; v++) verts&#x5B;v] = v;\r\n\t\t\t  }else{\r\n\t\t\t\tfor(v=0; v&lt;n; v++) verts&#x5B;v] = (n-1)-v;\r\n\t\t\t  }\r\n\t\t\t\r\n\t\t\t  var nv:int = n;\r\n\t\t\t\r\n\t\t\t  \/*  remove nv-2 vertsertices, creating 1 triangle every time *\/\r\n\t\t\t  var count:int = 2*nv;   \/* error detection *\/\r\n\t\t\t var m:int;\r\n\t\t\t  for(m=0, v=nv-1; nv&gt;2; )\r\n\t\t\t  {\r\n\t\t\t\t\/* if we loop, it is probably a non-simple polygon *\/\r\n\t\t\t\tif (0 &gt;= (count--)){\r\n\t\t\t\t  \/\/** Triangulate: ERROR - probable bad polygon!\r\n\t\t\t\t \/\/ trace(&quot;bad poly&quot;);\r\n\t\t\t\t  return null;\r\n\t\t\t\t}\r\n\t\t\t\r\n\t\t\t\t\/* three consecutive vertices in current polygon, &lt;u,v,w&gt; *\/\r\n\t\t\t\tvar u:int = v; if (nv &lt;= u) u = 0;     \/* previous *\/\r\n\t\t\t\tv = u+1; if (nv &lt;= v) v = 0;     \/* new v    *\/\r\n\t\t\t\tvar w:int = v+1; if (nv &lt;= w) w = 0;     \/* next     *\/\r\n\t\t\t\r\n\t\t\t\tif ( snip(contour,u,v,w,nv,verts)){\r\n\t\t\t\t  var a:int,b:int,c:int,s:int,t:int;\r\n\t\t\t\r\n\t\t\t\t  \/* true names of the vertices *\/\r\n\t\t\t\t  a = verts&#x5B;u]; b = verts&#x5B;v]; c = verts&#x5B;w];\r\n\t\t\t\r\n\t\t\t\t  \/* output Triangle *\/\r\n\t\t\t\t  result.push( contour&#x5B;a] );\r\n\t\t\t\t  result.push( contour&#x5B;b] );\r\n\t\t\t\t  result.push( contour&#x5B;c] );\r\n\t\t\t\r\n\t\t\t\t  m++;\r\n\t\t\t\r\n\t\t\t\t  \/* remove v from remaining polygon *\/\r\n\t\t\t\t  for(s=v,t=v+1;t&lt;nv;s++,t++) verts&#x5B;s] = verts&#x5B;t]; nv--;\r\n\t\t\t\r\n\t\t\t\t  \/* resest error detection counter *\/\r\n\t\t\t\t  count = 2 * nv;\r\n\t\t\t\t}\r\n\t\t\t  }\r\n\t\t\t\r\n\t\t\t  return result;\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ calculate area of the contour polygon\r\n\t\tpublic function area(contour:Array):Number{\r\n\t\t\tvar n:int = contour.length;\r\n\t\t\tvar a:Number  = 0.0;\r\n\t\t\t\r\n\t\t\tfor(var p:int=n-1, q:int=0; q&lt;n; p=q++){\r\n\t\t\t\ta += contour&#x5B;p].x * contour&#x5B;q].y - contour&#x5B;q].x * contour&#x5B;p].y;\r\n\t\t\t}\r\n\t\t\treturn a * 0.5;\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ see if p is inside triangle abc\r\n\t\tpublic function insideTriangle(ax:Number, ay:Number, bx:Number, by:Number, cx:Number, cy:Number,px:Number,py:Number):Boolean{\r\n\t\t\t\t\t\t\t\t\t\t\t               \r\n\t\t\t  var aX:Number, aY:Number, bX:Number, bY:Number\r\n\t\t\t  var cX:Number, cY:Number, apx:Number, apy:Number;\r\n\t\t\t  var bpx:Number, bpy:Number, cpx:Number, cpy:Number;\r\n\t\t\t  var cCROSSap:Number, bCROSScp:Number, aCROSSbp:Number;\r\n\t\t\t\r\n\t\t\t  aX = cx - bx;  aY = cy - by;\r\n\t\t\t  bX = ax - cx;  bY = ay - cy;\r\n\t\t\t  cX = bx - ax;  cY = by - ay;\r\n\t\t\t  apx= px  -ax;  apy= py - ay;\r\n\t\t\t  bpx= px - bx;  bpy= py - by;\r\n\t\t\t  cpx= px - cx;  cpy= py - cy;\r\n\t\t\t\r\n\t\t\t  aCROSSbp = aX*bpy - aY*bpx;\r\n\t\t\t  cCROSSap = cX*apy - cY*apx;\r\n\t\t\t  bCROSScp = bX*cpy - bY*cpx;\r\n\t\t\t\r\n\t\t\t  return ((aCROSSbp &gt;= 0.0) &amp;&amp; (bCROSScp &gt;= 0.0) &amp;&amp; (cCROSSap &gt;= 0.0));\r\n\t\t}\r\n\t\t\r\n\t\tprivate function snip(contour:Array, u:int, v:int, w:int, n:int, verts:Array):Boolean{\r\n\t\t\t  var p:int;\r\n\t\t\t  var ax:Number, ay:Number, bx:Number, by:Number;\r\n\t\t\t  var cx:Number, cy:Number, px:Number, py:Number;\r\n\t\t\t\r\n\t\t\t\t  ax = contour&#x5B;verts&#x5B;u]].x;\r\n\t\t\t\t  ay = contour&#x5B;verts&#x5B;u]].y;\r\n\t\t\t\t\r\n\t\t\t\t  bx = contour&#x5B;verts&#x5B;v]].x;\r\n\t\t\t\t  by = contour&#x5B;verts&#x5B;v]].y;\r\n\t\t\t\t\r\n\t\t\t\t  cx = contour&#x5B;verts&#x5B;w]].x;\r\n\t\t\t\t  cy = contour&#x5B;verts&#x5B;w]].y;\r\n\t\t\t\r\n\t      if ( EPSILON &gt; (((bx-ax)*(cy-ay)) - ((by-ay)*(cx-ax))) ) return false;\r\n\t\t\t\r\n\t\t\t  for (p=0;p&lt;n;p++){\r\n\t\t\t\t\tif( (p == u) || (p == v) || (p == w) ) continue;\r\n\t\t\t\t\tpx = contour&#x5B;verts&#x5B;p]].x\r\n\t\t\t\t\tpy = contour&#x5B;verts&#x5B;p]].y\r\n\t\t\t\t\tif (insideTriangle(ax,ay,bx,by,cx,cy,px,py)) return false;\r\n\t\t\t  }\r\n\t\t\t  return true;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>&#8230; and the Pnt class:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ Point class, no reason to use AS3&#039;s build in Point\r\npackage{\r\n\tpublic class Pnt {\r\n\t\tpublic var x:Number;\r\n\t\tpublic var y:Number;\r\n\t\tpublic function Pnt(x:Number, y:Number){\r\n\t\t\tthis.x = x;\r\n\t\t\tthis.y = y;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>I also found a nice <a href=\"http:\/\/www.siggraph.org\/education\/materials\/HyperGraph\/scanline\/outprims\/polygon1.htm\" target=\"blank\">description of the basic technique being employed here<\/a>.<\/p>\n<p>UPDATE:<br \/>\n<em>This should be obvious, but I was targeting fp9 with this&#8230; thus the use of Array instead of Vector.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>var triangulate:Triangulate = new Triangulate(); var poly:Array=&#x5B;]; stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); function onDown(evt:MouseEvent):void { poly.push(new Pnt(mouseX, mouseY)); } addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { var i:int; graphics.clear(); var verts:Array=triangulate.process(poly); if (verts==null) { \/\/ draw a red polygon if there is some kind of error, \/\/ or if there are too few points on the poly if (poly.length&gt;1) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,114,121,94],"tags":[3,4],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-3d","category-graphics-algorithms","category-math","category-misc","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/300","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=300"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}