In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

MagicSierpinskiGasket

http://www.jcu.edu/math/vignettes/chaosgame.htm
Get Adobe Flash player
by Kaede 23 Aug 2010
    Embed
/**
 * Copyright Kaede ( http://wonderfl.net/user/Kaede )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zJfN
 */

package {
    import flash.display.Sprite;
    import flash.geom.Point;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;
    
    //http://www.jcu.edu/math/vignettes/chaosgame.htm
    
    public class MagicSierpinskiGasket extends Sprite {
        private const L:int = 200;//三角形の一辺の長さ
        private const B:Point = new Point(stage.stageWidth/2,stage.stageHeight/2);//基準点
        private var t:Array;//基準となる正三角形の配列
        private var m:Point;//中点
        private var bitmap:Bitmap;

        public function MagicSierpinskiGasket() {
            init();            
        }
        
        private function init():void{
            var seed:Point = new Point(stage.stageWidth*Math.random(),stage.stageHeight*Math.random());//適当な乱数
            bitmap = new Bitmap(new BitmapData(stage.stageWidth,stage.stageHeight,false,0));
            addChild(bitmap);
            t = getTrianglePoints(B,L);
            m = getMidpoint(seed,getRandomBasePoint());
            addEventListener(Event.ENTER_FRAME,enterFrameHandler);
        }
        
        private function enterFrameHandler(e:Event):void{
            update(m);
            m = getMidpoint(m,getRandomBasePoint());
        }

        private function update(point:Point):void{
            var bd:BitmapData = bitmap.bitmapData;
            bd.setPixel(point.x,point.y,0xFF0000);
        }

        private function getRandomBasePoint():Point{
            return t[Math.floor(t.length*Math.random())];
        }

        private function getTrianglePoints(b:Point,l:Number):Array{
            return [
                new Point(b.x,b.y-(L/2)),
                new Point(b.x-(L/2),b.y+(L/2)),
                new Point(b.x+(L/2),b.y+(L/2))
            ]
        }

        private function getMidpoint(p1:Point,p2:Point):Point{
            return new Point((p1.x+p2.x)/2,(p1.y+p2.y)/2);
        }

    }
}