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

Mandelbrot

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

package{
  import flash.display.*;
  import flash.utils.*;
  import flash.events.*;
  import flash.text.*;
  import flash.events.KeyboardEvent;
  import flash.ui.Keyboard;
  import mx.controls.Button;
  [SWF(backgroundColor="#000000",width="800",height="600",frameRate = "30")]
  public class Main extends Sprite{

    private var _minX:Number = -2;
    private var _maxX:Number = 1;
    private var _minY:Number = -1.5;
    private var _maxY:Number = 1.5;
    private var _maxN:uint = 512;
    private var _infty:Number = int.MAX_VALUE;
    private var minX:Number ;
    private var maxX:Number ;
    private var minY:Number ;
    private var maxY:Number ;
    private var maxN:uint = 512;
    private var infty:Number = int.MAX_VALUE;
    private var bmd:BitmapData;
    private var i:uint = 0;
    private var j:uint = 0;
    private var timer:Timer;

    private var mx:Number;
    private var my:Number;
    private var mousex:Number;
    private var mousey:Number;

    private var md:Boolean = false;

    private var dd:Shape;

    private var dim:int = 600
    private var dimx:int = 800;
    private var dimy:int = 600;
    private var mbn:uint = 2;

    private var minXtf:TextField;
    private var minYtf:TextField;
    private var maxXtf:TextField;
    private var maxYtf:TextField;
    private var inftytf:TextField;
    private var maxNtf:TextField;
    private var mbntf:TextField;

    private var tf_h:uint = 0
    private function addtf(param:String):TextField{
      with (addChild(new TextField())){
        x = dim + 10;
        y = 10+tf_h*20;
        text = param;
        selectable = false;
        textColor = 0xFFFFFF;
      }
      
      var tf:TextField;
      with (addChild(tf=new TextField())){
        x = dim + 60;
        y = 10+tf_h*20;
        width = dimx - dim -70;
        height = 20;
        //text = defaultvalue;
        selectable = true;
        border = true;
        type = TextFieldType.INPUT;
        borderColor = 0x808080;
        textColor = 0x000000;
        backgroundColor = 0xFFFFFF;
        background = true;
      }
      tf.addEventListener(KeyboardEvent.KEY_DOWN,tf_kdown); 
      tf_h++;
      return tf;
    }
    
    public function Main(){
    
      XYreset()

      var base:Sprite = new Sprite();
      bmd = new BitmapData(dimy,dimy, false, 0x000000);
      var bm:Bitmap = new Bitmap(bmd);
      base.addChild(bm);
      addChild(base);


      minXtf=addtf("left x");
      minYtf=addtf("bottom y");
      maxXtf=addtf("right x");
      maxYtf=addtf("top y");
      maxNtf=addtf("N");
      inftytf=addtf("Infinity");
      mbntf=addtf("n (z^n+c)");
      settf();
      
      var setvaluebtn:TextField;
      with (addChild(setvaluebtn=new TextField())){
        x = dim + 10;
        y = 10+tf_h*20;
        width = 50;
        height = 20;
        text = "Set Value";
        selectable = false;
        border = true;
        //type = TextFieldType.INPUT;
        borderColor = 0x808080;
        textColor = 0x000000;
        backgroundColor = 0xE0E0E0;
        background = true;
      }
      setvaluebtn.addEventListener(MouseEvent.MOUSE_DOWN,setvaluebtn_mdown);

      var resetbtn:TextField;
      with (addChild(resetbtn=new TextField())){
        x = dim + 60;
        y = 10+tf_h*20;
        width = 65;
        height = 20;
        text = "Reset Value";
        selectable = false;
        border = true;
        //type = TextFieldType.INPUT;
        borderColor = 0x808080;
        textColor = 0x000000;
        backgroundColor = 0xE0E0E0;
        background = true;
      }
      resetbtn.addEventListener(MouseEvent.MOUSE_DOWN,resetbtn_mdown);
      tf_h++;

/*
      with (addChild(new TextField())){
        x = dim + 10;
        y = 10+tf_h*20+20;
        width = dimx - dim -20;
        height = dimy - y;
        selectable = false;
        textColor = 0xFFFFFF;
        text="Keyboard\n\tHOME:初期化\n\tENTER:再描画\n\tESCAPE:描画中止\n\tUP:マウス付近拡大\n\tDOWN:マウス付近縮小\n\tPAGE_UP:画面中心拡大\n\tPAGE_DOWN:画面中心縮小\n\tRIGHT:マウス付近拡大2\n\tLEFT:マウス付近縮小2\nMouse\n\tDrag&Drop:範囲拡大";
      }
*/

      addEventListener(Event.ENTER_FRAME, draw);
      stage.addEventListener(KeyboardEvent.KEY_DOWN,kdown);
      base.addEventListener(MouseEvent.MOUSE_DOWN,mdown);
      base.addEventListener(MouseEvent.MOUSE_UP,mup);
      base.addEventListener(MouseEvent.MOUSE_MOVE,mmv);
      
    }

    private function settf():void{
      minXtf.text = String(minX);
      minYtf.text = String(minY);
      maxXtf.text = String(maxX);
      maxYtf.text = String(maxY);
      inftytf.text = String(infty);
      maxNtf.text = String(maxN);
      mbntf.text = String(mbn);
      i = j = 0;
    }

    private function setparam():void{
      minX = Number(minXtf.text) ;
      minY = Number(minYtf.text) ;
      maxX = Number(maxXtf.text) ;
      maxY = Number(maxYtf.text) ;
      var size:Number = Math.max(maxX-minX,maxY-minY);
      maxXtf.text = String(maxX=minX+size);
      maxYtf.text = String(maxY=minY+size);
      infty = Number(inftytf.text);
      maxN = int(maxNtf.text) ;
      mbn = int(mbntf.text);
      i=j=0;
    }

    private function setvaluebtn_mdown(event:MouseEvent):void{
      setparam();
    }
    private function resetbtn_mdown(event:MouseEvent):void{
      XYreset()
      settf();
      setparam();
    }

    public function tf_kdown(evt:KeyboardEvent):void{
      switch (evt.keyCode){
        case Keyboard.ENTER:
          setparam();
          break;
        default:
          break;
      }
    }
    private function XYreset():void{
      minX = _minX;
      maxX = _maxX;
      minY = _minY;
      maxY = _maxY;
      maxN = _maxN;
      infty = _infty;
    }
    private function mdown(event:MouseEvent):void{
      if (md)
        return;
      mousex = mouseX;
      mousey = mouseY;
      mx = minX+mouseX *(maxX-minX)/dim;
      my = minY+(dim-1-mouseY) *(maxY-minY)/dim;
      md = true;
    }

    private function mmv(event:MouseEvent):void{
      if(!md)
        return;
      try{
        removeChild(dd);
      }catch(e:Object){}
      var minmx:int = Math.min(mousex,mouseX);
      var minmy:int = Math.min(mousey,mouseY);
      var size:int = Math.max(Math.abs(mousex-mouseX),Math.abs(mousey-mouseY));
      dd = new Shape();
      dd.graphics.lineStyle(1,0xFF0000);
      dd.graphics.drawRect(minmx,minmy,size,size);
      addChild(dd);
    }

    private function mup(event:MouseEvent):void{
      if(!md)
        return;
      md = false;
      try{
        removeChild(dd);
      }catch(e:Object){}
      var _mx:Number = minX+mouseX *(maxX-minX)/dim;
      var _my:Number = minY+(dim-1-mouseY) *(maxY-minY)/dim;
      minX = Math.min(mx,_mx);
      minY = Math.min(my,_my);
      var size:Number = Math.max(Math.abs(mx-_mx),Math.abs(my-_my));
      maxX = minX + size;
      maxY = minY + size;
      i = j = 0;
      settf();
    }
    public function kdown(evt:KeyboardEvent):void{
      try{
        removeChild(dd);
      }catch(e:Object){}
      md = false;
      /*
      mx = minX+mouseX *(maxX-minX)/dim;
      my = minY+(dim-1-mouseY) *(maxY-minY)/dim;
      var t:Number ;
      */
      switch (evt.keyCode){
      /*
        case Keyboard.HOME:
          XYreset();
          break
        case Keyboard.ENTER:
          break;
      */
        case Keyboard.ESCAPE:
          i=width;
          return;
      /*
        case Keyboard.UP:
          XYenlarge1(Math.sqrt(0.5),mx,my);
          break
        case Keyboard.DOWN:
          XYenlarge1(Math.sqrt(2),mx,my);
          break;
        case Keyboard.PAGE_UP:
          XYenlarge1(Math.sqrt(0.5),(minX+maxX)/2,(minY+maxY)/2);
          break
        case Keyboard.PAGE_DOWN:
          XYenlarge1(Math.sqrt(2),(minX+maxX)/2,(minY+maxY)/2);
          break;
        case Keyboard.RIGHT:
          XYenlarge2(Math.sqrt(0.5),mx,my);
          break
        case Keyboard.LEFT:
          XYenlarge2(Math.sqrt(2),mx,my);
          break;
       */
        default:
          return;
      }
      /*
      dd = new Shape();
      dd.graphics.lineStyle(1,0xFF0000);
      dd.graphics.drawCircle(mouseX,mouseY,16);
      bmd.draw(dd);
      settf()
      */
    }
    public function XYenlarge1(t:Number,mx:Number,my:Number):void{
          minX = minX * t + (1-t) * mx;
          maxX = maxX * t + (1-t) * mx;
          minY = minY * t + (1-t) * my;
          maxY = maxY * t + (1-t) * my;
    }
    public function XYenlarge2(t:Number,mx:Number,my:Number):void{
          minX = mx - (maxX-minX)*t/2;
          maxX = 2*mx -minX;
          minY = my - (maxY-minY)*t/2;
          maxY = 2*my - minY;
    }
    public function draw(evt:Event):void{
      bmd.lock();
      for (var k:uint=dim*16;k>0;k--){
        if (i>=dim){
          bmd.unlock();
          return;
        }
        var n:int = Fractal.Mandelbrot(
            minX+i*(maxX-minX)/dim ,
            minY+j*(maxY-minY)/dim ,
            infty,
            maxN
            ,mbn);
        var color:int;
          var t:Number = n / (maxN);
          var bright:int = int ( 255 * (1-t) );
          color = bright*0x010101;
        bmd.setPixel(i, dim-j-1, color);
        j++;
        if (j==height){
          j=0;
          i++;
        }
      }
      bmd.unlock();
    }
  }
  
}

  class Fractal{
    public static function Mandelbrot(a:Number,b:Number,r:uint,n:uint,N:uint):int{
      var _x:Number = 0;
      var _y:Number = 0;
      var x:Number = 0;
      var y:Number = 0;
      var t:Number = 0;
      var i:int = 0
      for (i=0;i<n;i++){
        for (var j:int=0;j<N-1;j++){
        t = x * _x - y * _y ;
        y = x * _y + _x * y ;
        x = t;
        }
        x += a;
        y += b;
        _x = x;
        _y = y;
        if (x*x + y*y > r*r){
          break;
        }
      }
      return i;
    }
  }