樹木曲線はフラクタルで木を再現したもので、コンピュータアートの分野では有名です
ある程度ランダムな要素を入れるとかなり現実の木に近くなります
※クリックでstart
Line.as
package {
import flash.display.Sprite;
import flash.geom.Point;
public class Line extends Sprite {
public var startP:Point;
public var endP:Point;
public var distance:Number;
public var angle:Number;
function Line(startP:Point, endP:Point) {
this.startP = startP;
this.endP = endP;
angle = Math.atan2(endP.y-startP.y, endP.x-startP.x);
distance = Point.distance(startP, endP);
graphics.lineStyle(0,0x000000);
graphics.moveTo(startP.x, startP.y);
graphics.lineTo(endP.x, endP.y);
graphics.endFill();
}
}
}
TreeCurve.as
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Point;
public class TreeCurve extends Sprite {
private var list:Array = [];
private const W:int = 300;
private const H:int = 300;
private const RAD:Number = Math.PI/8;
private const a:Number = 0.75;
function TreeCurve() {
var line:Line=new Line(new Point(W/2, H), new Point(W/2, H*4/5));
list.push(line);
stage.addChild(line);
stage.addEventListener(MouseEvent.CLICK,onClick);
function onClick(event:MouseEvent):void {
stage.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
stage.removeEventListener(MouseEvent.CLICK,onClick);
}
}
private function onEnterFrameHandler(event:Event):void {
var line:Line=list[0];
list.splice(0, 1);
if (line.distance <=
{
stage.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
return;
}
var p:Point = new Point(line.endP.x + line.distance * a * Math.cos(line.angle + RAD), line.endP.y + line.distance * a * Math.sin(line.angle+RAD));
var newLine:Line = new Line(line.endP, p);
list.push(newLine);
stage.addChild(newLine);
p = new Point(line.endP.x + line.distance * a * Math.cos(line.angle - RAD), line.endP.y + line.distance * a * Math.sin(line.angle - RAD));
newLine = new Line(line.endP, p);
list.push(newLine);
stage.addChild(newLine);
}
}
}





