{"id":292,"date":"2009-05-21T16:20:06","date_gmt":"2009-05-21T23:20:06","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1415"},"modified":"2009-05-21T16:20:06","modified_gmt":"2009-05-21T23:20:06","slug":"drawpath-boxes","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=292","title":{"rendered":"drawPath() Boxes"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nconst TWO_PI:Number = Math.PI * 2;\r\nvar boxNum:int = 3000;\r\nvar pointNum:int = boxNum * 10;\r\nvar rot:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar posX:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar posY:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar velX:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar velY:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar scale:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar rotSpeed:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar geometry:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar cmds:Vector.&lt;int&gt; = new Vector.&lt;int&gt;();\r\n\r\nvar stageWidth:Number = stage.stageWidth;\r\nvar stageHeight:Number = stage.stageHeight;\r\n\r\npopulateGeom();\r\nfunction populateGeom():void{\r\n\tfor (var i:int = 0; i&lt;pointNum; i+=10){\r\n\t\tposX.push(Math.random() * stageWidth);\r\n\t\tposY.push(Math.random() * stageHeight);\r\n\t\tvelX.push(Math.random()*4 - 2);\r\n\t\tvelY.push(Math.random()*4 - 2);\r\n\t\tscale.push(Math.random() * 1 + .2);\r\n\t\trot.push(Math.random() * TWO_PI);\r\n\t\trotSpeed.push(Math.random() * 0.2 - 0.1);\r\n\t\tgeometry&#x5B;i] = 0;\r\n\t\tgeometry&#x5B;i + 1] = 0;\r\n\t\tcmds.push(1);\r\n\t\tgeometry&#x5B;i + 2] =  0;\r\n\t\tgeometry&#x5B;i + 3] =  0;\r\n\t\tcmds.push(2);\r\n\t\tgeometry&#x5B;i + 4] =  0;\r\n\t\tgeometry&#x5B;i + 5] =  0;\r\n\t\tcmds.push(2);\r\n\t\tgeometry&#x5B;i + 6] = 0;\r\n\t\tgeometry&#x5B;i + 7] =  0;\r\n\t\tcmds.push(2);\r\n\t\tgeometry&#x5B;i + 8] =  0;\r\n\t\tgeometry&#x5B;i + 9] =  0;\r\n\t\tcmds.push(2);\r\n\t}\r\n}\r\n\r\nfunction run():void{\r\n\tvar inc:int = 0;\r\n\tvar xt:Number, yt:Number, s:Number, r:Number, rs:Number;\r\n\tvar cos:Number, sin:Number;\r\n\tvar cosN:Number, cosP:Number, sinN:Number, sinP:Number;\r\n\tfor (var i:int = 0; i&lt;pointNum; i+=10){\r\n\t\txt = posX&#x5B;inc] += velX&#x5B;inc];\r\n\t\tyt = posY&#x5B;inc] += velY&#x5B;inc];\r\n\t\t\r\n\t\tif (xt &lt; 0 || xt &gt; stageWidth){\r\n\t\t\tvelX&#x5B;inc] *= -1;\r\n\t\t}\r\n\t\tif (yt &lt; 0 || yt &gt; stageHeight){\r\n\t\t\tvelY&#x5B;inc] *= -1;\r\n\t\t}\r\n\t\t \r\n\t\ts = scale&#x5B;inc];\r\n\t\tr = rot&#x5B;inc] += rotSpeed&#x5B;inc];\r\n\t\tinc++;\r\n\t\t \r\n\t\tcos = Math.cos(r);\r\n\t\tsin = Math.sin(r);\r\n\t\t\r\n        cosN = -10  * cos;\r\n\t\tcosP = 10  * cos\r\n\t\tsinN = -10 * sin;\r\n\t\tsinP = 10 * sin;\r\n\t\tgeometry&#x5B;i] = xt + (cosN - sinN) * s;\r\n\t\tgeometry&#x5B;i + 1] = yt + (cosN + sinN) * s;\r\n\t\tgeometry&#x5B;i + 2] = xt + (cosP - sinN) * s;\r\n\t\tgeometry&#x5B;i + 3] = yt + (cosN + sinP) * s;\r\n\t\tgeometry&#x5B;i + 4] = xt + (cosP - sinP) * s;\r\n\t\tgeometry&#x5B;i + 5] = yt + (cosP + sinP) * s;\r\n\t\tgeometry&#x5B;i + 6] = xt + (cosN - sinP) * s;\r\n\t\tgeometry&#x5B;i + 7] = yt + (cosP + sinN) * s;\r\n\t\tgeometry&#x5B;i + 8]  = geometry&#x5B;i];\r\n\t\tgeometry&#x5B;i + 9]  = geometry&#x5B;i + 1];\r\n\t}\r\n}\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n     run();\r\n\t graphics.clear();\r\n\t graphics.beginFill(0x000000);\r\n\t graphics.drawPath(cmds, geometry,GraphicsPathWinding.NON_ZERO);\r\n\t graphics.endFill();\r\n}\r\n<\/pre>\n<p>I was messing with drawPath() today and did this snippet as a sort of performance test&#8230; the code isn&#8217;t fully optimized but it&#8217;s clear that drawPath() is rather fast. I wish that there were beginFill() and lineStyle() drawing commands that worked with it though&#8230; oh yeah, that&#8217;s what IGraphicsData is for&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>const TWO_PI:Number = Math.PI * 2; var boxNum:int = 3000; var pointNum:int = boxNum * 10; var rot:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var posX:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var posY:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var velX:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var velY:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var scale:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var rotSpeed:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var geometry:Vector.&lt;Number&gt; = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,13],"tags":[3,4],"class_list":["post-292","post","type-post","status-publish","format-standard","hentry","category-graphics","category-motion","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/292","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=292"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/292\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}