/* * JULIA * * Draws an animated Julia-fractal. The used Julia-fractal here is of the form z(n+1) = z(n)^2+c where z & c are of the complex form a+bi and c is chosen at random for each mouse-click. Each frame the value of c is updated a little bit resulting in an animation. * * To recreate, you'll need to have the Flash9 ActionScript3 Preview installed, you can get it at http://labs.adobe.com/technologies/flash9as3preview/ * Create a new 400x400 FLA, and put 'Julia' (without quotes) as its Document Class. Test Movie et voila! * * @author Edwin Heijmen * @version 0.1 * * @see http://astronomy.swin.edu.au/~pbourke/fractals/juliaset/ */ package { import flash.display.*; import flash.events.*; import flash.utils.Timer; public class Julia extends MovieClip { private var cutoff:uint = 40; // cutoff-point, max 40 iterations for each pixel private var colorStep:Number = 255/cutoff; // scale for coloring pixel private var size:Number = 1; // size of each point (1 pixel by 1 pixel) private var w:uint = 400; // width private var h:uint = 400; // height private var rows:uint = uint(w/size); // number of rows private var cols:uint = uint(h/size); // number of columns private var jump:Number = 4/rows; // assuming fractal will be from (-2,-2) to (2,2) so scale is 4 // used for displaying fractal and interaction private var bmd:BitmapData; private var bm:Bitmap = null; private var sp:Sprite; // used to determine position in fractal and in bitmap private var pX:Number; private var pY:Number; private var fX:Number; private var fY:Number; private var col:uint; private var row:uint; // constant c private var cReal:Number; private var cImag:Number; private var cRealStep:Number; private var cImagStep:Number; function Julia () { // constructor // // draw sprite for interaction purposes only sp = new Sprite(); sp.graphics.beginFill(0xFFFFFF); sp.graphics.drawRect(0,0,w,h); sp.graphics.endFill(); addChild(sp); // listen to mouse-clicks to set new random value of c sp.addEventListener("mouseUp",setC); // // set random c setC(); // // start executing function var myTimer:Timer = new Timer(0); myTimer.addEventListener("timer",doNext); myTimer.start(); } function setC (event:Event=null):void { // create new random values for cReal & cImag // // random value for c cReal = Math.random()*2-1; cImag = Math.random()*2-1; // random step-value for updating c on each iteration cRealStep = 0.02*(Math.random()*2-1); cImagStep = 0.02*(Math.random()*2-1); } function doNext (event:TimerEvent=null):void { // create a new Julia fractal // // remove bitmap if (bm!=null) { removeChild(bm); bmd.dispose(); } // create new bitmapdata bmd = new BitmapData(400,400,false,0x000000); // update c cReal += cRealStep; cImag += cImagStep; // start the process bmd.lock(); // go from left to right, top to bottom col = 0; // fX represents horizontal position in fractal // pX the corrsponding horizontal position of pixel in the bitmap fX = -2; pX = 0; // got thru all points do { row = 0; // fY represents vertical position in fractal // pY the corrsponding vertical position of pixel in the bitmap fY = -2; pY = 0; do { // calculate value for point (fX,fY) and draw it at (pX,pY) drawPoint(calcPoint()); // update fY += jump; pY += size; } while (++row