flash on 2010-3-16
绘制一棵树
夏天的树人
/**
* Copyright summerTree ( http://wonderfl.net/user/summerTree )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4yYn
*/
package
{
//绘制一棵树
//夏天的树人
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.*;
[SWF(backgroundColor="0xffffff", frameRate="30")];
public class Main extends Sprite
{
private var shape:Sprite=new Sprite();
public function Main()
{
addChild(shape);
CreatTree(shape.graphics,300, 460, 90, 180,6);
CreatTree(shape.graphics,300, 400, 45, 130,6);
CreatTree(shape.graphics,300, 400, 135, 130,6);
}
//绘图对象,基本坐标px,py,角度,偏移长度,数量
private function CreatTree(g:Graphics,px:Number,py:Number,angle:Number,len:Number,n:int):void
{
if (n>0)
{
var x1:Number=px+0.1*len*Math.cos(angle*Math.PI/180);
var y1:Number=py-0.1*len*Math.sin(angle*Math.PI/180);
var x2:Number=px+len*Math.cos(angle*Math.PI/180);
var y2:Number=py-len*Math.sin(angle*Math.PI/180);
drawLine(g,n-1,px,py,x2,y2);
var a_l:Number = angle+30;
var a_r:Number = angle-30;
len = len*2/3;
CreatTree(g,x2, y2, angle-Math.random()*10,len, n-1);
CreatTree(g,x1, y1, a_l,len*2/3, n-1);
CreatTree(g,x1, y1, a_r, len*2/3,n-1);
CreatTree(g,x2, y2, a_l,len*2/3, n-1);
CreatTree(g,x2, y2, a_r,len*2/3, n-1);
}
}
//绘制线
private function drawLine(g:Graphics,n:int,x1:Number,y1:Number,x2:Number,y2:Number):void
{
g.lineStyle(n,0x000000);
g.moveTo(x1,y1);
g.lineTo(x2,y2);
}
}
}