Mega Code Archive

 
Categories / Delphi / Graphic
 

Draw the Mandelbrot Fractal

Title: Draw the Mandelbrot Fractal? procedure DrawMandelbrot(ACanvas: TCanvas; X, Y, au, bu: Double; X2, Y2: Integer); var c1, c2, z1, z2, tmp: Double; i, j, Count: Integer; begin c2 := bu; for i := 10 to X2 do begin c1 := au; for j := 0 to Y2 do begin z1 := 0; z2 := 0; Count := 0; {count is deep of iteration of the mandelbrot set if |z| =2 then z is not a member of a mandelset} while (((z1 * z1 + z2 * z2 and (Count do begin tmp := z1; z1 := z1 * z1 - z2 * z2 + c1; z2 := 2 * tmp * z2 + c2; Inc(Count); end; //the color-palette depends on TColor(n*count mod t) {$IFDEF LINUX} ACanvas.Pen.Color := (16 * Count mod 255); ACanvas.DrawPoint(j, i); {$ELSE} ACanvas.Pixels[j, i] := (16 * Count mod 255); {$ENDIF} c1 := c1 + X; end; c2 := c2 + Y; end; end; procedure TForm1.Button1Click(Sender: TObject); var R: TRect; au, ao: Integer; dX, dY, bo, bu: Double; begin // Initialize Mandelbrot R.Left := 0; R.Right := 200; R.Top := 0; R.Bottom := 205; ao := 1; au := -2; bo := 1.5; bu := -1.5; //direct scaling cause of speed dX := (ao - au) / (R.Right - R.Left); dY := (bo - bu) / (R.Bottom - R.Top); DrawMandelbrot(Self.Canvas, dX, dY, au, bu, R.Right, R.Bottom); end;