{"id":353,"date":"2009-07-21T09:47:44","date_gmt":"2009-07-21T16:47:44","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1809"},"modified":"2009-07-21T09:47:44","modified_gmt":"2009-07-21T16:47:44","slug":"recursive-2d-structure","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=353","title":{"rendered":"Recursive 2D Structure"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width = 600, height = 700, frameRate=24)]\r\nvar canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0xFFFFFF);\r\naddChild(new Bitmap(canvas));\r\n\r\nvar maxBranches:int = 600;\r\nvar branches:int = 0;\r\nvar startX:Number = 300\r\nmakeBranch(startX,690,30,-60, 60);\r\n\r\nfunction makeBranch(xp:Number, yp:Number, step:Number, min:Number, max:Number):void {\r\n\tvar vectors:Shape = Shape(addChild(new Shape()));\r\n\tvar cX:Number, cY:Number, eX:Number, eY:Number\r\n\tvar dcX:Number=xp, dcY:Number=yp, deX:Number=xp, deY:Number=yp;\r\n\tvar theta:Number = (min + Math.random()*(max-min) - 90) * Math.PI \/ 180;\r\n\tcX = xp + step * Math.cos(theta);\r\n\tcY = yp + step * Math.sin(theta);\r\n\ttheta = (min + Math.random()*(max-min)-90) * Math.PI \/ 180;\r\n\teX = cX + step * Math.cos(theta);\r\n\teY = cY + step * Math.sin(theta);\r\n\tvar run:Function = function():void{\r\n\t     dcX +=  (cX - dcX) \/ 2;\r\n\t\t dcY +=  (cY - dcY) \/ 2;\r\n\t\t deX +=  (eX - deX) \/ 8;\r\n\t\t deY +=  (eY - deY) \/ 8;\r\n\t\t with(vectors.graphics){\r\n\t\t\t  clear();\r\n\t\t\t  beginFill(0xFFFFFF,0.8);\r\n\t\t\t  lineStyle(0,0x000000,0.8);\r\n\t\t\t  moveTo(startX, yp);\r\n\t\t\t  lineTo(xp, yp);\r\n\t\t\t  curveTo(dcX, dcY, deX, deY);\r\n\t\t\t  lineTo(startX, deY);\r\n\t\t }\r\n\t\t if (Math.abs(dcX - cX) &lt; 1 &amp;&amp; Math.abs(deX - eX) &lt; 1 &amp;&amp; Math.abs(dcY - cY) &lt; 1 &amp;&amp; Math.abs(deY - eY) &lt; 1){\r\n\t\t\t canvas.draw(vectors);\r\n\t\t\t removeChild(vectors);\r\n\t\t\t if (branches &lt; maxBranches){\r\n\t\t\t\t setTimeout(makeBranch, 10, deX, deY, step - Math.random(), -90, 90);\r\n\t\t\t\t branches++;\r\n\t\t\t\t if (int(Math.random()*2) == 1){\r\n\t\t\t\t    setTimeout(makeBranch, 10, deX, deY, step - Math.random()*3, -90, 90);\r\n\t\t\t\t\tbranches++;\r\n\t\t\t\t }\r\n\t\t\t }\r\n\t\t }else{\r\n\t\t     setTimeout(arguments.callee,  1000 \/ 24);\r\n\t\t }\r\n\t}();\r\n}\r\n<\/pre>\n<p>This snippet uses a technique similar to what you might use to create a <a href=\"https:\/\/actionsnippet.com\/?p=238\" target=\"blank\">recursive tree<\/a>. A bit of additional logic is added for bezier branches, filled shapes and animation. <\/p>\n<p><i>WARNING: may run slow on older machines<\/i><br \/>\n<a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/recursive_2d_structure.html\" target=\"blank\"><br \/>\nHave a look at the swf&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/recursive_curves.jpg\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/recursive_2d_structure.swf&quot;,&quot;title&quot;:&quot;recursive 2d structure&quot;,&quot;width&quot;:600,&quot;height&quot;:700,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#cccccc&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: recursive 2d structure<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width = 600, height = 700, frameRate=24)] var canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0xFFFFFF); addChild(new Bitmap(canvas)); var maxBranches:int = 600; var branches:int = 0; var startX:Number = 300 makeBranch(startX,690,30,-60, 60); function makeBranch(xp:Number, yp:Number, step:Number, min:Number, max:Number):void { var vectors:Shape = Shape(addChild(new Shape())); var cX:Number, cY:Number, eX:Number, eY:Number var dcX:Number=xp, dcY:Number=yp, deX:Number=xp, deY:Number=yp; var theta:Number = (min [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,24,17,94,13],"tags":[3,14,4],"class_list":["post-353","post","type-post","status-publish","format-standard","hentry","category-bezier","category-bitmapdata","category-functions","category-misc","category-motion","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/353","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=353"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}