Mandelbrot
Mandelbrot
by rect (http://blog.r3c7.net/)
//
// Mandelbrot
// by rect (http://blog.r3c7.net/)
//
package
{
import flash.events.Event;
import flash.display.*;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.getTimer;
import flash.utils.ByteArray;
import frocessing.color.ColorHSV;
[SWF(backgroundColor="#000000")]
public class main extends Sprite
{
private var w:uint = 128;
private var h:uint = 128;
private var cont:Bitmap;
private var buffer:BitmapData;
private var zero:Point = new Point(0,0);
private var byte:ByteArray;
private var zoom:Number = 1;
private var mx:Number = -0.5;
private var my:Number = 0;
private var maxIteration:int = 60;
private var rot:Number = 0;
public function main():void
{
stage.frameRate = 30;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.LOW;
buffer = new BitmapData(w, h, false, 0x000000);
byte = new ByteArray();
byte.position = 0;
var bmp:BitmapData = new BitmapData(w, h, false, 0x000000);
cont = new Bitmap(bmp);
addChild(cont);
cont.width = stage.stageWidth;
cont.height = stage.stageHeight;
cont.x = (stage.stageWidth - cont.width) / 2;
cont.y = (stage.stageHeight - cont.height) / 2;
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void
{
var targetBmd:BitmapData = cont.bitmapData;
var bytes:ByteArray = byte;
bytes.position = 0;
var pixels:uint = w * h;
rot += 1;
zoom = 21 + Math.sin(rot * Math.PI/180) * 20;
mx = (Math.sin(rot * Math.PI/180) * 0.5) -1.5;
my = Math.sin(rot * Math.PI/180) * 0.28;
for (var j:int = 0; j < pixels; j++)
{
var x:int = (j & 0x7F) >> 0;
var y:int = (j / w) >> 0;
var pr:Number = 1.5 * (x - 64) / (zoom * 64) + mx;
var pi:Number = (y - 64) / (zoom * 64) + my;
var newRe:Number = 0;
var newIm:Number = 0;
var oldRe:Number = 0;
var oldIm:Number = 0;
var i:int = 0;
for(i= 0; i < maxIteration; i++)
{
oldRe = newRe;
oldIm = newIm;
newRe = oldRe * oldRe - oldIm * oldIm + pr;
newIm = 2 * oldRe * oldIm + pi;
if((newRe * newRe + newIm * newIm) > 4) break;
}
var hsv:ColorHSV = new ColorHSV((i + 192) & 255, 0.9, (240 * Number(i < maxIteration))*0.01, 1);
var rgb:uint = hsv.value;
bytes.writeUnsignedInt(rgb);
}
bytes.position = 0;
buffer.setPixels(buffer.rect, bytes);
targetBmd.copyPixels(buffer, buffer.rect, zero);
}
}
}