Mandelbrot: lake of fire (linear mapping)
// forked from makc3d's Mandelbrot: lake of fire
// forked from rect's Mandelbrot
package
{
import flash.events.Event;
import flash.display.*;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.getTimer;
import flash.utils.ByteArray;
[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 target: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 = 30;
private var rot:Number = 0;
private var scale:Matrix;
private var image:Loader;
private var filter:DisplacementMapFilter;
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;
target = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
scale = new Matrix; scale.scale (stage.stageWidth / w, stage.stageHeight / h);
image = new Loader; addChild (image);
image.load (new URLRequest ("http://assets.wonderfl.net/images/related_images/4/42/420b/420b4115c4180c7220113d906523a309d2ed4e88"), new LoaderContext (true));
filter = new DisplacementMapFilter (target, target.rect.topLeft,
BitmapDataChannel.GREEN, BitmapDataChannel.BLUE, 2*128 * scale.a, 2*128 * scale.d,
DisplacementMapFilterMode.WRAP);
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void
{
var bytes:ByteArray = byte;
bytes.position = 0;
var pixels:uint = w * h;
rot += 1;
zoom = 10 + Math.sin(rot * 0.03) * 5;
mx = 0.7 * Math.sin(rot * 0.01) - 0.3;
my = 0.7 * Math.cos(rot * 0.01);
var magic:Number = 64;
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;
}
if (i == maxIteration) {
newRe *= magic;
newIm *= magic;
var g:int = int (newRe) % 256;
var b:int = int (newIm) % 256;
bytes.writeUnsignedInt(g * 256 + b);
} else {
bytes.writeUnsignedInt((128 - x) * 256 + (128 - y));
}
}
bytes.position = 0;
buffer.setPixels(buffer.rect, bytes);
target.draw (buffer, scale);
image.filters = [ filter ];
}
}
}