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

Processing風Flash「Frocessing」でフラクタルを描こう! サンプルコード

/**
 * Copyright ll_koba_ll ( http://wonderfl.net/user/ll_koba_ll )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/3WCJ
 */

package  
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import frocessing.display.F5MovieClip2DBmp


    public class C3_1 extends F5MovieClip2DBmp
    {
        //120度
        private const ANGLE_1:Number = Math.PI * 2 / 3;
        //240度
        private const ANGLE_2:Number = Math.PI * 4 / 3;
        
        //フラクタルの回転角度
        private var _theta:Number;
        //線分の長さ
        private var _length:Number;
        //線分の増加量
        private var _vl:Number;
        public function C3_1() {
        }
        
        public function setup():void {
            _theta = 0;
            _length = 10;
            _vl = 1;
            background( 0 );
        }
        
        public function draw():void {
            //fadeout
            noStroke();
            fill(0,0,0,.03);
            rect(0, 0, width, height);
            
            if (!isMousePressed){
                //マウスがクリックされていなければ初期値に戻す
                _length = 10;
                _vl = 1
            }else {
                //マウス位置から生成する
                translate(mouseX,mouseY );
                //毎フレームごとに線色を変える
                stroke(random(0xFFFFFF, 0xAAAAAA), 0.5);
                //フラクタル生成
                branch(_length, _theta);
                //回転
                _theta  += .1;
                //長さに加算
                _length += _vl;
            }
            
            //線分の増減
            if (_length > 110) _vl = -1
            else if (_length < 10) _vl = 1;
        }
        
        
        public function branch( h:Number,theta:Number ):void {
            h *= 0.66;
             
            if (h > 4) {
                //一本目の枝
                pushMatrix();
                rotate(theta);
                line(0,0,0,-h);
                translate(0,-h); 
                branch(h,theta);       
                popMatrix();
        
                //二本目の枝
                pushMatrix();
                rotate(theta + ANGLE_1);
                line(0, 0, 0, -h);
                translate( 0, -h);
                branch(h, theta);   
                popMatrix();

                //三本目の枝
                pushMatrix();
                rotate(theta + ANGLE_2);
                line(0,0,0,-h);
                translate(0,-h);
                branch(h,theta);
                popMatrix();
            }
        }
        
    }
}