Groups | Blog | Home
all groups > flash actionscript > january 2005 >

flash actionscript : mandelbrot


smatsri
1/30/2005 11:00:30 PM
i tried to do the the mandelbrot picture. but it get to slow to soon. i made a
movie clip this a black square to be the 'pixel'.and for a 200x200 rez i need
to go throu all the points and for each point i need to do a loop of 10 to
decide if i will attach a the 'pixel' or not,and thats before adding color.so i
need to do alot of calculations to do a small picture.yet i see java applets
that dont have alot of problems with it. so i thought maybe theres a better way
to do it in flash.does any body have an idea? here`s the code:

_root.createEmptyMovieClip("mcImage",_root.getNextHighestDepth())
var x:Number = 0
var y:Number = 0
var a:Number;
var b:Number;
mcImage._x = Stage.width/2;
mcImage._y = Stage.height/2;
for(i=-100;i<=100;i++){
for(j=-100;j<=100;j++){
x=0
y=0
b=i/50
a=j/50
for(n=0;n<=10;n++){
xNew = x*x-y*y+a
y = 2*x*y+b
x = xNew
zAbs = x*x + y*y
}
if (zAbs<4) {

mcImage.attachMovie("square","mcSquare"+x+"_"+y,mcImage.getNextHighestDepth(
));
mcImage["mcSquare"+x+"_"+y]._x = i
mcImage["mcSquare"+x+"_"+y]._y = j
}
}
}
Rothrock
1/30/2005 11:11:30 PM
Your script gives me the dreaded 'A script in this movie is causing Flash
Player to run slowly' error. Flash is not known for its ability to handle a lot
of maths. You would do better to code something like this in Java. I'm
intrigued by this and will poke around a bit and see if I can figure anything
out, but I'm not very hopeful. I'll let you know.
Rothrock
1/31/2005 12:26:50 AM
Your script tries to draw the whole picture in one go which causes the slow
down error. It is kind of a built in remedy against creating infinite loops. I
played around a bit and was able to speed things up. Some of the changes I made
will help you. Others I made some assumptions and it depends upon what your
final goal is whether it will be helpful. I've also adjusted the range of
pixels that are being tested since the set isn't distributed symmetrically
around the origin.

stop();
startTime = getTimer();
this.createEmptyMovieClip("mcImage", 100);
mcImage._x = Stage.width/2;
mcImage._y = Stage.height/2;
var i = -60;
this.onEnterFrame = function() {
for (var j = -110; j<=30; j++) {
var x = 0;
var y = 0;
var b = i/50;
var a = j/50;
for (var n = 10; n--; ) { //I have no idea how this works! But it is so much
faster.
var xNew = x*x-y*y+a;
y = 2*x*y+b;
x = xNew;
var zAbs = x*x+y*y;
}
if (zAbs<4) {
mcImage.lineStyle(1, 0x000000, 100);
mcImage.moveTo(i-.5, j);
mcImage.lineTo(i+.5, j);

//drawing a short line is much quicker than
attaching a movie
//but if you need to use attachmovie use this code
instead
/*

theClip=mcImage.attachMovie("square","square"+depth,depth++);
theClip._x=i;
theClip._y=j;
*/
}
}
i++;
if (i>=60) {
this.onEnterFrame = null;
trace("Took "+(getTimer()-startTime));
}
};
smatsri
1/31/2005 4:32:44 AM
it works fantasticly faster
thanx very much
now i just need to figure out how to do the color scheam
i will post an update if it look good
David Stiller
1/31/2005 12:20:46 PM
smatsri,

My hat is off to you! Good deal, Rothrock, with your input, too. You
don't see that kind of huzzah in Flash every day.


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

smatsri
1/31/2005 4:56:50 PM
so here it is its not as good as a java applet,and i dont think flash could
handle something like this so i wont continue with a zoom function,but its nice
here`s the code,just copy and paste:

this.createEmptyMovieClip("mcImage", 100);
mcImage._x = Stage.width/2;
mcImage._y = Stage.height/2+50;
var i = -120;
var nInterval = setInterval(drawLine,60);

function drawLine():Void {
for (var j = -240; j<=100; j++) {
var x = 0;
var y = 0;
var b = i/100;
var a = j/100;
n=0
do {
n++
var xNew = x*x-y*y+a;
y = 2*x*y+b;
x = xNew;
var zAbs = x*x+y*y;
if(n>=20)break;

} while(zAbs<4)
if (zAbs>4) {
mcImage.lineStyle(1, 0xFF0000, 5*n);
mcImage.moveTo(i-1, j);
mcImage.lineTo(i+.1, j);
}
}
i++;
tMessage.text = "processing..."
if (i>=0) {
clearInterval(nInterval)
duplicate()
tMessage.text = ""
}
}

function duplicate():Void {
mcImage.duplicateMovieClip("dupMcImage",this.getNextHighestDepth())
dupMcImage._xscale = -100
dupMcImage._x -= 2
}
smatsri
1/31/2005 5:50:43 PM
thank you

and i have to thanx Rothrock for the great tips
Rothrock
2/1/2005 4:57:52 AM
Fantastic! And thank you. By the way I think you can speed it up just a bit
more by making the declaration of n in the function var n = 0; It is really
amazing how much using var speeds things up. I'm always amazed. Since you
moveTo(i-1,j) I'm not sure that you need to draw the line any further than
lineTo(i,j). That will save you quite a few additions.
AddThis Social Bookmark Button