How to Draw a Mandelbrot Fractal on the Form’s Canvas

This source code shows you how to draw a Madelbrot fractal on the form’s Canvas. Although some of the Integer variables could be changed to Byte or Word variables they have not been changed because the Integer type is a native x86 type and calculations on Integers are the fastest.
This function is also (as tip 6.3) based on the source code written by Marc Capuano. This source is free for non-commercial use. The original source has been cleaned up and has been optimized for speed and size.
The function seems to have the same speed as tip 6.3.

  uses
    Graphics;
  ...
  procedure DrawMandelbrot;
  const
    aColors: array[0..14] of TColor = (clBlack, clMaroon, clGreen, clNavy,
      clPurple, clTeal, clGray, clSilver, clRed, clLime, clBLue, clFuchsia,
      clAqua, clWhite, clBlack);
  var
   iI, iJ, iNewColor : Integer;
   rU, rV, rX, rY, rZ: Real;
  begin
    for iI := 0 to ClientWidth - 2 do
      for iJ := 0 to ClientHeight - 2 do begin
        rX := -0.8 + 3 * iI / ClientWidth;
        rY := -1.4 + 2.8 * iJ / ClientHeight;
        iNewColor := 0;
        rU := 0;
        rV := 0;
        repeat
          rZ := Sqr(rU) - Sqr(rV) - rX;
          rV := 2 * rU * rV - rY;
          rU := rZ;
          Inc(iNewColor);
        until (Sqr(rU) + Sqr(rV) > 9) or (iNewColor = 14);
        Canvas.Pixels[iI + 1, iJ + 1] := aColors[iNewColor];
      end;
  end;

Tags: , , , , , , , , , , , , , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login