Fractal Related Discussion > Noob's Corner

(Question) Pascal Code for the Mandelbrot (math only)

(1/2) > >>

Hi everyone,
I am searching for a simple code to start drawing a mandelbrot set.
I was out of programming for several years and my last math-related work was in the eighties.
I want to join in into programming and start from scratch.
I have set up a program, that can paint on the screen, display math functions and the like. But I am struggling at getting a mandelbrot set displayed.
I have found two simple sources that didn't really work out.
I want to display it in general and then by varying one parameter zoom in or by varying another parameter and you can "move" left, right, up, down etc.
I can handle all the UI stuff, just looking for a starting point in maths.
Thank you
Pascal Code preferred, but I can read other code too


Hello and welcome, Mash!

Found this for you on
Maybe a starting point for you to study?

Mind you, I have no Pascal interpreter here, so no guarantees ;)

--- Code: ---program mandelbrot;
      {$MODE DELPHI} 
   ixmax = 800;
   iymax = 800;
   cxmin = -2.5;
   cxmax =  1.5;
   cymin = -2.0;
   cymax =  2.0;
   maxcolorcomponentvalue = 255;
   maxiteration = 200;
   escaperadius = 2;
   colortype = record
      red   : byte;
      green : byte;
      blue  : byte;
   ix, iy      : integer;
   cx, cy      : real;
   pixelwidth  : real = (cxmax - cxmin) / ixmax;
   pixelheight : real = (cymax - cymin) / iymax;
   filename    : string = 'new1.ppm';
   comment     : string = '# ';
   outfile     : textfile;
   color       : colortype;
   zx, zy      : real;
   zx2, zy2    : real;
   iteration   : integer;
   er2         : real = (escaperadius * escaperadius);
   assign(outfile, filename);
   if ioresult <> 0 then
      {$IFDEF FPC}
         writeln(stderr, 'Unable to open output file: ', filename);
         writeln('ERROR: Unable to open output file: ', filename);
   writeln(outfile, 'P6');
   writeln(outfile, ' ', comment);
   writeln(outfile, ' ', ixmax);
   writeln(outfile, ' ', iymax);
   writeln(outfile, ' ', maxcolorcomponentvalue);
   for iy := 1 to iymax do
      cy := cymin + (iy - 1)*pixelheight;
      if abs(cy) < pixelheight / 2 then cy := 0.0;
      for ix := 1 to ixmax do
         cx := cxmin + (ix - 1)*pixelwidth;
         zx := 0.0;
         zy := 0.0;
         zx2 := zx*zx;
         zy2 := zy*zy;
         iteration := 0;
         while (iteration < maxiteration) and (zx2 + zy2 < er2) do
            zy := 2*zx*zy + cy;
            zx := zx2 - zy2 + cx;
            zx2 := zx*zx;
            zy2 := zy*zy;
            iteration := iteration + 1;
         if iteration = maxiteration then
     := 0;
   := 0;
    := 0;
     := 255;
   := 255;
    := 255;
         write(outfile, chr(, chr(, chr(;
--- End code ---

Good luck and enjoy coding :)  :thumbs:

The smallest and simplest code to display a Mandelbrot (in ASCII) was posted by galac:

The math part starts at the while loop, variables a,b denote the pixel's coordinates and If you change the print statement: " " e.g. to put a white pixel on the screen and "*" to a black pixel, then you have a good starting point.

One big ressources of code  is
Suggest you that read downloads topics, there are many programs discussion and you could take much ideas.

@marcm200 I was looking for exactly that and couldn't find it!  :happy:
Great, thanks for sharing the link! :thumbs:


[0] Message Index

[#] Next page

Go to full version
Powered by SMFPacks Rates (Facepunch) Mod
Powered by SMFPacks SEO Pro Mod | Sitemap
Powered by Advanced Topic Prefix Pro
Powered by SMFPacks Advanced Attachments Uploader Mod