シェルピンスキーのギャスケット
package
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.geom.Point;
[SWF(frameRate=60, backgroundColor=0x000000)]
public class SierpinskiGasket extends Sprite
{
private const W:int = stage.stageWidth;
private const H:int = stage.stageHeight;
private var lineLength:Number = W;
private var list:Array = [];
public function SierpinskiGasket()
{
var topP:Point = new Point(lineLength * Math.cos( -Math.PI / 3),lineLength * Math.sin( -Math.PI / 3) + H - 20);
var leftP:Point = new Point(0, H-20);
var rightP:Point = new Point(W, H-20);
addTriangle(lineLength/2, topP, leftP, rightP);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
}
private function onEnterFrameHandler(event:Event):void
{
var t:Triangle = list[0];
list.splice(0, 1);
removeChild(t);
var leftHalfP:Point = new Point(t.leftP.x + t.lineLength * Math.cos( -Math.PI / 3),
t.leftP.y + t.lineLength * Math.sin( -Math.PI / 3));
var rightHalfP:Point = new Point(t.topP.x + t.lineLength * Math.cos(Math.PI / 3),
t.topP.y + t.lineLength * Math.sin(Math.PI / 3));
var bottomHalfP:Point = new Point(t.leftP.x + t.lineLength, t.leftP.y);
addTriangle(t.lineLength/2, t.topP, leftHalfP, rightHalfP);
addTriangle(t.lineLength/2, leftHalfP, t.leftP, bottomHalfP);
addTriangle(t.lineLength/2, rightHalfP, bottomHalfP, t.rightP);
}
private function addTriangle(lineLength:Number, p1:Point, p2:Point, p3:Point):void{
var triangle:Triangle = new Triangle(lineLength, p1, p2, p3);
addChild(triangle);
list.push(triangle);
}
}
}
import flash.display.Shape;
import flash.geom.Point;
class Triangle extends Shape
{
public var topP:Point;
public var leftP:Point;
public var rightP:Point;
public var lineLength:Number;
public function Triangle(lineLength:Number,topP:Point, leftP:Point, rightP:Point)
{
this.lineLength = lineLength;
this.topP = topP;
this.leftP = leftP;
this.rightP = rightP;
this.graphics.lineStyle(0, 0xffffff);
this.graphics.moveTo(topP.x, topP.y);
this.graphics.lineTo(leftP.x, leftP.y);
this.graphics.lineTo(rightP.x, rightP.y);
this.graphics.lineTo(topP.x, topP.y);
this.graphics.endFill();
}
}