jsonchiu
04-21-2007, 05: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.
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.