Archive

Posts Tagged ‘integer type’

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

December 15th, 1999 m3Rlin 1 comment

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;

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

December 15th, 1999 m3Rlin No comments

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;