PDA

View Full Version : 400 byte Mandelbrot


jsonchiu
04-21-2007, 04:50 AM
The challenge: create an image of the Mandelbrot Set by actionscript with only 400 bytes
Rules:
1. no "script is running slow" message, meaning that it must be either fast enough, or it must be a gradual animation like the one shown here

2. Must be good quality - 100 iterations

3. Must be colored by a gradient, not just 2 colors

Mine... 442 bytes (this is the best I can do, download it to view)
http://jsonchiu.prophp.org/flash/Mandelbrot2.swf

The code:
import flash.display.BitmapData;
var m:Number = 120;
var w:Number = 300;
var h:Number = 300;
var b:BitmapData = new BitmapData(w, h);
var i:Number = 0;
var id:Number = setInterval(u, 1);
function f(r:Number, i:Number):Number
{
var X:Number = 0;
var Y:Number = 0;
var X2:Number = X * X;
var Y2:Number = Y * Y;
var t:Number = 0;
while (t < m && (X2 + Y2) < 100000)
{
var p:Number = X;
X = X2 - Y2 + r;
Y = 2 * p * Y + i;

X2 = X * X;
Y2 = Y * Y;
t++;
}
return m - t;
}
function u():Void
{
for (var j:Number = 0; j <= w; j++)
{
//in order to input the correct values for calculating
//we need to scale the x (equals j), y (equals i) values
//j -> (j - w / 2) / w * 2.7
//i -> (i - h / 2) / h * 2.7
var r:Number = f((j - w / 2) / w * 2.7 - 0.7, (i - h / 2) / h * 2.7);
//set grey pixels according to the number of iterations left
b.setPixel(j, i, r + r * 0x100 + r * 0x10000);
}
if (i < h)
{
i++;
}
else
{
clearInterval(id);
}
}
attachBitmap(b, 1);

Note: I strongly type every variable and declare return types because they won't effect the file size anyways. Also, comments don't add to file size either.

jsonchiu
04-21-2007, 06:16 PM
394 bytes...
import flash.display.BitmapData;
m = 150;
w = 300;
h = 300;
b = new BitmapData(w, h);
i = 0;
function f(r:Number, i:Number):Number
{
var X:Number = 0;
var Y:Number = 0;
var X2:Number = 0;
var Y2:Number = 0;
var t:Number = 0;
while (t < m && (X2 + Y2) < 4)
{
Y = 2 * X * Y + i;
X = X2 - Y2 + r;

X2 = X * X;
Y2 = Y * Y;
t++;
}
return m - t;
}
onEnterFrame = function()
{
for (var j:Number = 0; j <= w; j++)
{
//in order to input the correct values for calculating
//we need to scale the x (equals j), y (equals i) values
//j -> (j - w / 2) / w * 3
//i -> (i - h / 2) / h * 3
//set green pixels according to the number of iterations left
b.setPixel(j, i, f((j - w / 2) / w * 3 - 1, (i - h / 2) / h * 3) * 256);
}
if (i < h)
{
i++;
}
else
{
onEnterFrame = null;
}
}
attachBitmap(b, 1);

Anyway to optimize that even more?