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

This source code shows you how to draw a Julia 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 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.
This function is sure not the fastest but it’s fast enough. It takes 12 seconds to draw this fractal on a maximized window (screen resolution: 1280×1024, 32 bit color) on a Pentium III 450 Mhz (overclocked to 504Mhz) system with 128 MB of RAM memory. The graphics card shouldn’t have effect on the speed. Rewriting the math part in assembly or using the Windows API SetPixel() function don’t seem to speed this up.

  uses
    Graphics;
  ...
  procedure DrawJulia;
  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;
    rX, rY, rZ       : Real;
  begin
    for iI := 0 to ClientWidth - 2 do
      for iJ := 0 to ClientHeight - 2 do begin
        rX := -1.8 + 3.6 * iI / ClientWidth;
        rY := -1 + 2 * iJ / ClientHeight;
        iNewColor := 0;
        repeat
          rZ := Sqr(rX) - Sqr(rY) - 1.06;
          rY := 2 * rX * rY;
          rX := rZ;
          Inc(iNewColor);
        until (Sqr(rX) + Sqr(rY) > 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