Triangle in triangle
BitmapData使ったら何とか軽くなった...
/**
* BitmapData使ったら何とか軽くなった...
*/
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
[SWF(backgroundColor="0xffffff", frameRate="12")]
public class Main extends Sprite {
private var STAGE:Stage;
private var sWidth:Number;
private var sHeight:Number;
private var max:int = 4;
private var sp:Shape;
private var bm:Bitmap;
private var bmd:BitmapData;
private var cache:BitmapData;
private var g:Graphics;
public function Main() {
STAGE = this.stage;
STAGE.align = StageAlign.TOP_LEFT;
STAGE.scaleMode = StageScaleMode.SHOW_ALL;
sWidth = STAGE.stageWidth;
sHeight = STAGE.stageHeight;
bmd = new BitmapData(sWidth, sHeight, false, 0xffffff);
bm = new Bitmap(bmd);
addChild(bm);
init();
}
private function init():void {
sp = new Shape();
g = sp.graphics;
addEventListener(Event.ENTER_FRAME, draw);
}
private function draw(event:Event):void {
bmd.fillRect(bmd.rect, 0xffffff);
cache = bmd.clone();
drawTriangle(0, 0, sWidth, 0, 0, sHeight, 0);
drawTriangle(sWidth, sHeight, sWidth, 0, 0, sHeight, 0);
bmd.draw(cache);
}
private function drawTriangle(ax:Number, ay:Number, bx:Number, by:Number, cx:Number, cy:Number, level:int):void {
g.clear();
g.beginFill(0xffffff);
g.lineStyle(1, 0x000000);
g.moveTo(ax, ay);
g.lineTo(bx, by);
g.lineTo(cx, cy);
g.lineTo(ax, ay);
g.endFill();
cache.draw(sp);
if(level == max) return;
var ab:Number = Util.getRandomRange(0.1, 0.9);
var ac:Number = Util.getRandomRange(0.1, 0.9);
var bc:Number = Util.getRandomRange(0.1, 0.9);
drawTriangle(
ax, ay,
ax + (bx - ax) * ab, ay + (by - ay) * ab,
ax + (cx - ax) * ac, ay + (cy - ay) * ac,
level + 1);
drawTriangle(
bx, by,
ax + (bx - ax) * ab, ay + (by - ay) * ab,
bx + (cx - bx) * bc, by + (cy - by) * bc,
level + 1);
drawTriangle(
cx, cy,
ax + (cx - ax) * ac, ay + (cy - ay) * ac,
bx + (cx - bx) * bc, by + (cy - by) * bc,
level + 1);
}
}
}
// internal claases
// Util class that contains static methods
internal class Util {
public static function getRandomRange(min:Number, max:Number):Number {
return (max - min) * Math.random() + min;
}
}