Jump to content


Photo

Julia And Mandelbrot Fractals


  • Please log in to reply
5 replies to this topic

#1 RabbidMickeyMouse

RabbidMickeyMouse

    GMC Member

  • New Member
  • 241 posts

Posted 03 February 2007 - 01:38 AM

A site which has helped me in the past had a section concerning julia and mandelbrot fractals (link). They provided code in C, and so I thought, why not port it over to GM?

The script for mandelbrot fractals...
// Each iteration, it calculates: newz = oldz * oldz + p, where p is the
// current pixel, and oldz stars at the origin.
var pr, p_;                        // Real and imaginary part of the pixel p
zoom = 1; moveX = -0.5; moveY = 0; // You can change these to zoom and change position
var color;                         // The RGB color value for the pixel
maxIterations = 30;                // After how much iterations the function should stop
newRe = 0;                         // Real and imaginary parts of new and old z
newIm = 0;                         // These should start at 0,0
oldRe = 0;
oldIm = 0;

w = room_width;
h = room_height;

// Loop through every pixel
for(xx=0; xx<w; xx+=1;)
for(yy=0; yy<h; yy+=1;)
{
 // Calculate the initial real and imaginary part of z, based
 // on the pixel location and zoom and position values.
 pr = 1.5 * (xx - w / 2) / (0.5 * zoom * w) + moveX;
 p_ = (yy - h / 2) / (0.5 * zoom * h) + moveY;

 newRe = 0; // These should start at 0,0
 newIm = 0;
 oldRe = 0;
 oldIm = 0;

 // Start the iteration process
 for(i=0; i<maxIterations; i+=1;)
 {
  // Remember value of previous iteration
  oldRe = newRe;
  oldIm = newIm;

  // The actual iteration, the real and imaginary part are calculated
  newRe = oldRe * oldRe - oldIm * oldIm + pr;
  newIm = 2 * oldRe * oldIm + p_;

  // If the point is outside the circle with radius 2: stop
  if((newRe * newRe + newIm * newIm) > 4) break;
 }

 // Use color model conversion to get rainbow palette,
 // make brightness black if maxIterations reached
 color = make_color_hsv(i mod 256, 255, 255 * (i < maxIterations));

 // Draw the pixel
 draw_set_color(color);
 draw_rectangle(round(xx),round(yy),round(xx)+1,round(yy)+1,false);
}

Julia fractals...
// Each iteration, it calculates: new = old * old + c, where c is
// a constant and old starts at current pixel.
var cRe, cIm;                     // Real and imaginary part of the constant c,
                                  // Determinate shape of the Julia Set
var newRe, newIm, oldRe, oldIm;   // Real and imaginary parts of new and old
zoom = 1; moveX = 0; moveY = 0;   // You can change these to zoom and change position
var color;                        // The RGB color value for the pixel
maxIterations = 30;               // After how much iterations the function should stop

// Pick some values for the constant c, this determines the shape of the Julia Set
cRe = -0.7;
cIm = 0.27015;

w = room_width;
h = room_height;

// Loop through every pixel
for(xx=0; xx<w; xx+=1;)
for(yy=0; yy<h; yy+=1;)
{
 // Calculate the initial real and imaginary part of z, based
 // on the pixel location and zoom and position values.
 newRe = 1.5 * (xx - w / 2) / (0.5 * zoom * w) + moveX;
 newIm = (yy - h / 2) / (0.5 * zoom * h) + moveY;

 // Start the iteration process
 for(i=0; i<maxIterations; i+=1;)
 {
  // Remember value of previous iteration
  oldRe = newRe;
  oldIm = newIm;

  // The actual iteration, the real and imaginary part are calculated
  newRe = oldRe * oldRe - oldIm * oldIm + cRe;
  newIm = 2 * oldRe * oldIm + cIm;

  // If the point is outside the circle with radius 2: stop
  if((newRe * newRe + newIm * newIm) > 4) break;
 }

 // Use color model conversion to get rainbow palette,
 // make brightness black if maxIterations reached.
 color = make_color_hsv(i mod 256, 255, 255 * (i < maxIterations));

 // Draw the pixel
 draw_set_color(color);
 draw_rectangle(round(xx),round(yy),round(xx)+1,round(yy)+1,false);
}

Because, as many know, GM is not as fact as C, the speed at which this script runs at is quite slow. To curb this, the size of the room should be low, and the amount of iterations should be low as well.

In a 256x192 room, at 30 iterations, the mandelbrot fractal looks like this...

Posted Image

These scripts may not be entirely uselful, yet nevertheless interesting.
  • 0

#2 cybot99

cybot99

    GMC Member

  • New Member
  • 190 posts

Posted 03 February 2007 - 02:56 AM

hi, RabbidMickeyMouse

yea, you're right, it is pretty interesting, but not very useful.
Still, nicely done.
  • 0

#3 celebraces

celebraces

    GMC Member

  • GMC Member
  • 956 posts

Posted 03 February 2007 - 03:15 AM

Looks like one of Yourself's avatars. Same thing?
  • 0

#4 Hach-Que

Hach-Que

    RoketGames Admin

  • New Member
  • 1490 posts

Posted 03 February 2007 - 03:19 AM

The program Xaos does this thing 10 times faster, with a zoom function (you can keep zooming and zooming and zooming....), and has some other fractal patterns as well.

And yes, Yourself does have fractals as part of his dynamic avatar (afterall, he is a maths genius).
  • 0

#5 RabbidMickeyMouse

RabbidMickeyMouse

    GMC Member

  • New Member
  • 241 posts

Posted 03 February 2007 - 06:46 AM

I should have looked beforehand to see if anyone else had posted their own scripts to generate mandelbrot fractals. Anyone interested in seeing other generators can check out the following that Google has led me to so far.

FredFredrickson's
http://forums.gamema...hp/t231773.html

Gumgo's
http://forums.gamema...howtopic=261457

Yourself's (Post #9)
http://forums.gamema...howtopic=125318
  • 0

#6 uuf6429

uuf6429

    Covac Software

  • New Member
  • 2522 posts

Posted 03 February 2007 - 07:50 AM

Don't know why but they remind me of sun black spots.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users