Actionscript tree
Just finished an Actionscript tree. Its still not 100% right, there are some little details I want to sort out. It can be extremely processing intensive so it is possible to cause it to hang by making it iterate too much or have too many branches so be warned. I have set the time out period to 30 seconds so if you haven’t got a tree in 30 seconds, you broke it! The other point of note is that angle is in radians. Angles controls the angle at which the branches come off. Any who, enough talking, here it is.
When I get a chance to, I will neaten up the code and post it up. Those of you who are observant may notice the swf is actulay called leaf_02.swf, this is ‘cos it was I started out wanting to draw a leaf, but that is harder than I thought (Not given up yet though
), so hopefully the next post will be a leaf!
Sorcecode for those interested – Not object oriented and almost no comments
function bezierPoint(origin:Point, control:Point, end:Point, loc:Number):Point { var p1:Point = Point.interpolate(origin, control, loc); var p2:Point = Point.interpolate(control, end, loc); var p:Point = Point.interpolate(p1, p2, loc); return p; } function endPoint(angle:Number, hypontinuse:Number, origin:Point):Point { var ret:Point = new Point((Math.cos(angle)*hypontinuse)+ origin.x, (Math.sin(angle)*hypontinuse)+origin.y); return ret; } function controlPoint(origin:Point, hyp:Number, angle:Number):Point { var a:Number = 3.14159265 + (angle + 1.57079633); var end:Point = endPoint(a , hyp, origin); return end } function drawBranch(origin:Point, angle:Number, size:Number, color:Number, childNo:Number):Array { var returnData = new Array(); var branch:Shape = new Shape(); var end:Point = endPoint(angle, size, origin); var control:Point = controlPoint(Point.interpolate(origin,end,0.5), Point.distance(origin, end)/6, angle); branch.graphics.lineStyle(0, color); branch.graphics.beginFill(color,1) branch.graphics.moveTo(origin.x, origin.y); branch.graphics.curveTo(control.x, control.y, end.x, end.y); var thickEnd:Point = controlPoint(origin, ((size/100)*3)/childNo, angle); branch.graphics.curveTo(control.x, control.y, thickEnd.x, thickEnd.y); branch.graphics.lineTo(origin.x, origin.y); returnData.push(branch); returnData.push(control); returnData.push(end); return returnData; } function drawTree(origin, angle, size, depth, factor, fixedAngle, frequency, childNo):void { if(depth <= 0) return; //draw branch var branch:Array = drawBranch(new Point(origin.x, origin.y), angle, size, 0x514837, childNo); holder.addChild(branch[0]); //find start, end, control for(var i:Number = 1; i <= frequency; i++) { var randSeed:Number = (Math.random()/2)+0.75; var frequencySeed:Number = Math.round(Math.random()*2)-1 var newAngle:Number; if(i % 2 == 0) newAngle = (angle+fixedAngle)*randSeed; else newAngle = (angle-fixedAngle)*randSeed; var offset:Number = ((1/frequency)*i)-((1/frequency)/2); var newOrigin:Point = bezierPoint(origin, branch[1], branch[2], offset); drawTree(newOrigin, newAngle, size/factor, depth-1, factor, fixedAngle, frequency+frequencySeed, (frequency-i)+1); } } var p:Point = new Point(400,550); drawTree(p, -1.5, 200, 8, 1.5, 0.7, 3, 1); drawTreeBtn.addEventListener(MouseEvent.CLICK, createManager); function createManager(event:MouseEvent):void { var len = len.getLineText(0); var depth = dep.getLineText(0); var frequency = freq.getLineText(0); var factor = fac.getLineText(0); var angle = angle.getLineText(0); //fix for typing issues caused by input var length1:Number = new Number(len); var depth1:Number = new Number(depth); var frequency1:Number = new Number(frequency); var factor1:Number = new Number(factor); var angle1:Number = new Number(angle); for(var i:int = holder.numChildren-1; i >= 0; i--) holder.removeChildAt(i); drawTree(p, -1.5, length1, depth1, factor1, angle1, frequency1, 1); }
About this entry
You’re currently reading “Actionscript tree,” an entry on random()
- Published:
- 02.06.08 / 2pm
- Category:
- Actionscript
No comments
Jump to comment form | comments rss [?] | trackback uri [?]