FractalTree
2009/01/07 初AS3です。
2009/01/10 いろいろ修正
2009/06/28 間違いをみつけた
// 2009/01/07 初AS3です。
// 2009/01/10 いろいろ修正
// 2009/06/28 間違いをみつけた
package
{
import flash.display.Sprite;
public class FractalTree extends Sprite
{
private const Radian:Number =20;
private const Radian1:Number = Math.PI * Radian / 180;
private const Radian2:Number = -Math.PI * Radian / 180;
private const MaxDepth:Number = 10;
public function FractalTree():void {
DrawBranch(MaxDepth, -Math.PI / 2, 85, 235, 380);
}
private function DrawBranch(
Depth:uint,
MyRadian:Number,
Length:Number,
OffsetX:Number,
OffsetY:Number
):void
{
if (Depth < 2) return;
var MyVectorX:Number = 0.8 * Length * Math.cos(MyRadian);
var MyVectorY:Number = 0.8 * Length * Math.sin(MyRadian);
this.graphics.moveTo(OffsetX, OffsetY);
var Color:uint = 0xFFFFFF*Math.random();
this.graphics.lineStyle(Depth, Color);
this.graphics.lineTo(MyVectorX + OffsetX, MyVectorY + OffsetY);
DrawBranch(
Depth - 1,
MyRadian + Radian1,
Length*0.8,
MyVectorX + OffsetX,
MyVectorY + OffsetY
);
DrawBranch(
Depth - 1,
MyRadian + Radian2,
Length*0.8,
MyVectorX + OffsetX,
MyVectorY + OffsetY
);
}
}
}