Fractal Related Discussion > Noob's Corner

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

(1/2) > >>

mash:
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

Sabine62:
Hello and welcome, Mash!

Found this for you on http://rosettacode.org/wiki/Mandelbrot_set#Pascal
Maybe a starting point for you to study?

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

--- Code: ---program mandelbrot;

{$IFDEF FPC} {$MODE DELPHI}
{$ENDIF} const ixmax = 800; iymax = 800; cxmin = -2.5; cxmax = 1.5; cymin = -2.0; cymax = 2.0; maxcolorcomponentvalue = 255; maxiteration = 200; escaperadius = 2; type colortype = record red : byte; green : byte; blue : byte; end; var 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); begin {$I-}
assign(outfile, filename);
rewrite(outfile);
if ioresult <> 0 then
begin
{$IFDEF FPC} writeln(stderr, 'Unable to open output file: ', filename); {$ELSE}
writeln('ERROR: Unable to open output file: ', filename);
{\$ENDIF}
exit;
end;

writeln(outfile, 'P6');
writeln(outfile, ' ', comment);
writeln(outfile, ' ', ixmax);
writeln(outfile, ' ', iymax);
writeln(outfile, ' ', maxcolorcomponentvalue);

for iy := 1 to iymax do
begin
cy := cymin + (iy - 1)*pixelheight;
if abs(cy) < pixelheight / 2 then cy := 0.0;
for ix := 1 to ixmax do
begin
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
begin
zy := 2*zx*zy + cy;
zx := zx2 - zy2 + cx;
zx2 := zx*zx;
zy2 := zy*zy;
iteration := iteration + 1;
end;
if iteration = maxiteration then
begin
color.red   := 0;
color.green := 0;
color.blue  := 0;
end
else
begin
color.red   := 255;
color.green := 255;
color.blue  := 255;
end;
write(outfile, chr(color.red), chr(color.green), chr(color.blue));
end;
end;

close(outfile);
end.
--- End code ---

Good luck and enjoy coding :)  :thumbs:

marcm200:
The smallest and simplest code to display a Mandelbrot (in ASCII) was posted by galac:
https://fractalforums.org/programming/11/small-mandelbrot-set-in-c/2658/msg13287#msg13287

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.

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

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