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

Random Mandelbrot-like set

Get Adobe Flash player
by makc3d 02 Feb 2010
/**
 * Copyright makc3d ( http://wonderfl.net/user/makc3d )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/a4UI
 */

// forked from makc3d's forked from: マンデルブロ高速化
// forked from toyoshim's マンデルブロ高速化
// forked from toyoshim's マンデルブロ @ Frocessing
package {
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    import frocessing.color.ColorHSV;
    import net.hires.debug.Stats;
    import com.bit101.components.*;

    [SWF(width="465", height="465", frameRate="30", backgroundColor="0x00000000")]
    public class Mandelbrot extends Sprite {
        private var bmp:Bitmap;
        private var img:BitmapData;
        private var txt:TextField;
        
        private var offset_x:Number = 100.0;
        private var offset_y:Number = 0.0;
        private var zoom:Number = 0.6;

        private var coefs:Array;

        public function Mandelbrot() {
            // width x height, no transparent, ARGB=0x00000000
            img = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0);
            bmp = new Bitmap(img);
            var bmph:Sprite = new Sprite;
            bmph.addChild (bmp);
            stage.addChild(bmph);
            
            // add mouse event listener
            bmph.addEventListener(MouseEvent.CLICK, onClick);

            // add debug status window
            stage.addChild(new Stats());

            // make sliders for 37 iteration coefs
            coefs = [];
            for (var n:int = 0; n <= 36; n++) {
                var slider:HUISlider = new HUISlider (null, 300, 12 * (n + 1), n.toString (), draw);
                slider.minimum = -1;
                slider.maximum = +1;
                slider.value = 1;
                slider.alpha = 0.5;
                coefs [n] = slider;
                stage.addChild (slider);
            }
            
            // draw initial image
            draw();
        }

        public function onClick(event:MouseEvent):void {
            offset_x = (offset_x + stage.mouseX - img.width / 2.0)
                        / zoom;
            offset_y = (offset_y + stage.mouseY - img.height / 2.0)
                        / zoom;
            zoom *= event.shiftKey ? 1 / 1.2 : 1.2;
            offset_x *= zoom;
            offset_y *= zoom;
            draw();
        }
        
        private function calcDepth(x:int, y:int):int {
            var cx:Number = (x + offset_x - img.width * 2.2 / 3.0)
                        / (img.width / 2.0) / zoom;
            var cy:Number = (y + offset_y - img.height / 2.0)
                        / (img.height / 2.0) / zoom;
            var r:Number = 0.0;
            var i:Number = 0.0;
            var n:int;
            for (n = 0; n <= 36; n++) {
                var cn:Number = HUISlider (coefs [n]).value;
                var nr:Number = r * r - i * i + cn * cx;
                var ni:Number = 2.0 * r * i + cn * cy;
                r = nr;
                i = ni;
                if ((r * r + i * i) > 4) break;
            }
            return 360 - n * 10;
        }
        
        private function calcColor(n:int):int {
            var hsv:ColorHSV = new ColorHSV(n, n, n);
            return hsv.value32;
        }

        private function draw(e:Event = null):void {
            img.lock();
            for (var y:int = 0; y < img.height; y++) {
                for (var x:int = 0; x < img.width; x++) {
                    img.setPixel32(x, y, calcColor(calcDepth(x, y)));
                }
            }
            img.unlock();
        }
    }
}