Home > Programming > Fractals > EGK

egk.gif

Previous

Next


const
  
escape=4.0;                { escape value }
  
attract=0.0001;            { attractor sensitivity }

const
  
Black        = 0;
   Blue         = 1;
   Green        = 2;
   Cyan         = 3;
   Red          = 4;
   Magenta      = 5;
   Brown        = 6;
   LightGray    = 7;
   DarkGray     = 8;
   LightBlue    = 9;
   LightGreen   =10;
   LightCyan    =11;
   LightRed     =12;
   LightMagenta =13;
   Yellow       =14;
   White        =15;

function GetRGBColor(color:byte):longint;
begin
  case
Color of
   
black : GetRGBColor := RGB(0,0,0);
    blue  : GetRGBColor := RGB(0,0,128);
    green : GetRGBColor := RGB(0,128,0);
    cyan  : GetRGBColor := RGB(0,255,255);
    red   : GetRGBColor := RGB(128,0,0);
    magenta : GetRGBColor := RGB(128,0,128);
    brown : GetRGBColor := RGB(128,128,0);
    lightgray : GetRGBColor := RGB(192,192,192);
    darkgray : GetRGBColor := RGB(128,128,128);
    lightBlue : GetRGBColor := RGB(0,0,255);
    lightGreen : GetRGBColor := RGB(0,255,0);
    lightcyan : GetRGBColor := RGB(0,255,255);
    lightRed : GetRGBColor := RGB(255,0,0);
    lightmagenta : GetRGBColor := RGB(255,0,255);
    yellow: GetRGBColor := RGB(255,255,0);
    white : GetRGBColor := RGB(255,255,255);
  else
   
GetRGBColor := RGB(0,0,255);
  end;
end;

////////////////////////////////////////////////////////////////////////////////

procedure EKG(ABitmap: TBitmap);
{ compute and display a simulated "EKG" from the Julia set of

    f(z) = z^2 + -1.5

}
var
  
i, j      : integer;       { loop variables}
  
MaxX,
   MaxY      : integer;      { Maximum Y screen coordinate}
  
MaxY2     : integer;
   scale     : double;       { scale factor }
  
mag       : double;        { square of magnitude of complex number }
  
iter      : integer;       { escape iteration counter }
  
continue  : boolean;      { continue iteration counter }
  
x,y       : double;        { double and complex parts of z }
  
MaxColor  : byte;          { maximum number of colors on graphics card }
  
MaxColor2 : byte;
   curColor  : TColorRef;
   Rect      : TRect;
   color     : byte;
   iLeft,
   jTop,
   iRight,
   jBottom: integer;
   zoom,
   ixScale,
   jyScale  : double;
   colorLimit : integer;
begin
 
SetRect(Rect, 0, 0, ABitmap.Width, ABitmap.Height);
  with Rect do
  begin
   
zoom := 2.5;
    MaxY := bottom - top;     { find maximum Y coordinate }
   
MaxX := right - left;
    if (MaxX=0) or (MaxY=0) then exit;
    MaxX := Max(MaxX,MaxY);
    MaxY := MaxX;
    right := left + MaxX;
    bottom := top + MaxY;
    MaxY2 := MaxY div 2;
    if MaxY<100 then
     
MaxColor := 5
   
else if MaxY<200 then
     
MaxColor := 8
   
else
     
MaxColor := 15; { Maximum number of colors }
   
MaxColor2 := MaxColor*2;
    colorLimit := -1;
    scale:= 2.0*zoom/MaxY;   { calculate zoom factor}

   
ixScale := 0.0;
    for i := 0 to MaxY2 do    { MaxY is usually smaller than MaxY }
   
begin
     
jyScale := 0.0;
      for j := 0 to MaxY2 do
      begin
       
x := ixScale-zoom;             { set starting value of double(z) }
       
y := zoom-jyScale;             { set starting value of imag(z) }
       
continue := true;              { assume point does not escape  }
       
iter :=0;
        while continue do
        begin
         
mult(x,y,x,y,x,y);         { square z }
         
x := x-1.5;
          mag := x*x+y*y;            { calculate square of magnitude }
         
if mag < attract then
            
continue := false       { point is an attractor }
         
else
            if
(mag < 100) and (iter < MaxColor2) then { keep iterating function }
             
inc(iter)
            else                      { point escapes, plot it }
           
begin
             
color := iter div 2;
              if color>colorLimit then
              begin
                if
color=MaxColor then
                 
CurColor := RGB(255,255,255)
                else
                 
CurColor := GetRGBColor(color);

                iLeft := i+ Left;
                iRight := Right - i;
                jTop := j+Top;
                jBottom := Bottom - j;

                ABitmap.Canvas.Pixels[iLeft,jTop] := curColor;

                ABitmap.Canvas.Pixels[iRight,jBottom] := curColor;

                ABitmap.Canvas.Pixels[iRight,jTop] := curColor;

                ABitmap.Canvas.Pixels[iLeft,jBottom] := curColor;

              end;
              continue := false        { get out of loop }
           
end
        end
{ while loop}
       
jyScale := jyScale + scale;
      end{j loop}
     
ixScale := ixScale + scale;
    end;{ i loop}
 
end;
end;

////////////////////////////////////////////////////////////////////////////////


2004 Jim Valavanis

Previous

Next

1