1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
program mandelbrot1;
{First demo of drawing a Mandelbrot Set. This program merely
demonstrates the basics of calculating and plotting.}
uses SDL; {A unit that allows us to read single keypresses.}
{Note to C programmers: the variable type "real" is equivalent
to the C type "float".}
var cx,cy: real; {Where do we want to center the brot?}
scale: real; {This is the "zoom" factor.}
limit: word; {Divergence check value.}
lp: word; {Convergence check value.}
a1,b1,a2,b2: real; {For calculating the iterations.}
x,y: integer; {The pixel we are drawing.}
ax,ay: real; {The actual position of (x,y) in relation to
the Mandelbrot set.}
Screen: PSDL_Surface;
Pal: array [0..255] of UInt32;
{ Trace un pixel sur une surface 32 bits}
procedure SDL_PutPixel32(Surface: PSDL_Surface; X, Y: SInt16; Color: UInt32);
var
Offset: cardinal;
P: ^UInt8;
Pixel: ^UInt32;
begin
Offset := Y * Surface^.Pitch + X * 4;
P := Surface^.Pixels + Offset;
Pixel := Pointer(P);
Pixel^ := Color;
end;
procedure InitGFX;
var
I: Integer;
begin
SDL_Init(SDL_INIT_VIDEO);
Screen:=SDL_SetVideoMode(320, 200,32, SDL_SWSURFACE);
if Screen = nil then
Halt;
for I:=Low(Pal) to High(Pal) do
Pal[I]:=SDL_MapRGB(Screen^.format, Random(256), Random(256), Random(256));
end;
procedure EndGFX;
begin
SDL_FreeSurface(Screen);
SDL_Quit;
end;
procedure PutPixel(a,b: word; c: byte);
begin
SDL_PutPixel32(Screen, a, b, Pal[C]);
end;
begin
Randomize;
{Set up video mode.}
InitGFX;
SDL_LockSurface(Screen);
{Set up initial values for drawing. Try compiling the program
with different values here if you like!}
cx:=0; cy:=0; scale:=0.02;
limit:=4;
{Loop through all pixels on screen. For reasons that will become
clear, I am counting not from (0,0) but from (-160,-100).}
for x:=-160 to 159 do
for y:=-100 to 100 do begin
{What is the *mathematical* value of this point?}
ax:=cx+x*scale; ay:=cy+y*scale;
{And now for the magic formula!}
a1:=ax; b1:=ay; lp:=0;
repeat
{Do one iteration.}
lp:=lp+1;
a2:=a1*a1-b1*b1+ax;
b2:=2*a1*b1+ay;
{This is indeed the square of a+bi, done component-wise.}
a1:=a2; b1:=b2;
until (lp>255) or ((a1*a1)+(b1*b1)>limit);
{The first condition is satisfied if we have convergence.
The second is satisfied if we have divergence.}
{Define colour and draw pixel.}
if lp>255 then lp:=0;
{If the point converges, it is part of the brot and we
draw it with colour 0, or black.}
PutPixel(x+160,y+100,lp);
end;
SDL_UnlockSurface(Screen);
SDL_Flip(Screen);
SDL_Delay(5000);
EndGFX;
end. |
Partager