forked from: マンデルブロ高速化
/**
* Copyright makc3d ( http://wonderfl.net/user/makc3d )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/yGjy
*/
// forked from toyoshim's マンデルブロ高速化
// forked from toyoshim's マンデルブロ @ Frocessing
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import net.hires.debug.Stats;
[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 = 0.0;
private var offset_y:Number = 0.0;
private var zoom:Number = 0.5//1.0;
public function Mandelbrot() {
// width x height, no transparent, ARGB=0x00000000
img = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0);
bmp = new Bitmap(img);
stage.addChild(bmp);
// draw initial image
draw();
// add mouse event listener
stage.addEventListener(MouseEvent.CLICK, onClick);
// add debug status window
stage.addChild(new Stats());
}
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 *= 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;
// REMAP_CARDIOD code from http://linas.org/art-gallery/self/self.html
var re:Number = 0.25 - cx;
var im:Number = +cy;
var u:Number = Math.sqrt (0.5*(re + Math.sqrt (re*re + im*im)));
var v:Number = 0.5 * im / u;
u = 0.5 - u;
var R:Number = Math.sqrt (u*u + v*v);
var phi:Number = Math.atan2 (v,u);
if (0.0 > phi) phi += 2.0*Math.PI;
phi /= 2.0*Math.PI;
/* phi runs from 0 to 1 */
phi = phi / (1.0+phi);
R = 0.5 *Math.sqrt(2.0*R);
phi *= 2.0*Math.PI;
/*re_c*/cx = R * (Math.cos (phi) - R * Math.cos (2.0*phi));
/*im_c*/cy = R * (Math.sin (phi) - R * Math.sin (2.0*phi));
// ------------------
var r:Number = 0.0;
var i:Number = 0.0;
var n:int, N:int = 37;
for (n = 0; n < N; n++) {
var nr:Number = r * r - i * i + cx;
var ni:Number = 2.0 * r * i + cy;
r = nr;
i = ni;
if ((r * r + i * i) > 4) break;
}
if (n == N) return 0
// http://linas.org/art-gallery/escape/smooth.html
return 255 * (n - Math.log (Math.log (Math.sqrt (r * r + i * i))) / Math.log (2)) / N;
}
private function calcColor(n:int):int {
return 0x10202 * int(n/2);
}
private function draw():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();
}
}
}