Home > Programming > Fractals > Simple mandel

simplemandel.gif

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 Mandel(ABitmap: TBitmap);
{  Rect     : Actual drawing rect
   Color    : Drawing Color
}
var
  
i, j      : integer;       { loop variables}
  
MaxY,MaxX : integer;      { Maximum X and Y coordinates }
  
xscale,
   yscale    : double;       { scale factor }
  
mag       : double;        { square of magnitude of complex number }
  
iter      : integer;       { escape iteration counter }
  
cx,cy     : double;        { x and y components of c }
  
x, y      : double;        { coordinate values in complex plane }
  
MaxY2     : integer;
   LeftValue: integer;
   Rect : TRect;
   PlotTimes : byte;         { Max times to plot in one iteration }
  
LLimit,
   MLimit,
   RLimit : integer;
   PlotLimit : integer;      { Maximum number to plot a pixel at one iteration }
  
ixScale,
   jyScale, zoom: double;
begin
 
SetRect(Rect, 0, 0, ABitmap.Width, ABitmap.Height);
  with Rect do
  begin
   
zoom := 2.0;
    MaxX := right - left;
    MaxY := bottom - top;     { find maximum Y screen coordinate }
   
if (MaxX=0) or (MaxY=0) then exit;
    xscale:= 2.0*zoom/MaxX;   { calculate zoom factor}
   
yscale:= 2.0*zoom/MaxY;    { calculate zoom factor}
   
x :=0;                     { zet z0 = 0 + 0i }
   
y :=0;
    MaxY2 := MaxY div 2;
    LLimit := MaxX div 10;        { Starting the disign }
   
RLimit := (MaxX * 13) div 20; { Stopping the design }
   
MLimit := (MaxX * 11) div 20; { Stopping the limited plotting of pixels }
   
PlotLimit := MaxY div 20 + 1;
    ixScale := LLimit*xScale;
    for i := LLimit to MLimit do
    begin
     
j := 0;
      jyScale := 0;
      PlotTimes := 0;
      while j<=MaxY2 do
      begin
       
x :=0;                    { zet z0 = 0 + 0i }
       
y :=0;
        cx:= ixScale-zoom;        { sweep value of c }
       
cy:= zoom - jyScale;
        mag :=0;                  { initial loop guards }
       
iter := 0;
        begin
          while
(iter < 30) and (mag < escape)   do
          begin
           
mult(x,y,x,y,x,y);         { square z }
           
add(x,y,cx,cy,x,y);        { add c }
           
mag := x*x+y*y;           { calculate square of magnitude }
           
inc(iter);                 { increment counter }
         
end;
          if mag < escape then        { output blue for non-escapees}
         
begin
           
inc(PlotTimes);
            LeftValue := i + left;
            ABitmap.Canvas.Pixels[LeftValue,j+top] := ABitmap.Canvas.Pen.Color;
            ABitmap.Canvas.Pixels[LeftValue,bottom-j] := ABitmap.Canvas.Pen.Color;
          end;
        end{ while loop}
       
if PlotTimes<PlotLimit then
        begin
         
inc(j);
          jyScale := jyScale + yScale;
        end
        else
        begin
         
ABitmap.Canvas.MoveTo(i+Left,j+top);
          ABitmap.Canvas.LineTo(i+Left,bottom-j);
          j := MaxY2+1;
        end;
      end{j loop}
     
ixScale := ixScale + xScale;
    end;{ i loop}
   
i := MLimit;
    while i<=RLimit do
    begin
     
plotTimes := 0;
      jyScale := 0;
      for j := 0 to MaxY2 do
      begin
       
x :=0;                    { zet z0 = 0 + 0i }
       
y :=0;
        cx:= ixScale-zoom;             { sweep value of c }
       
cy:= zoom - jyScale;
        mag :=0;                       { initial loop guards }
       
iter := 0;
        begin
          while
(iter < 30) and (mag < escape)   do
          begin
           
mult(x,y,x,y,x,y);         { square z }
           
add(x,y,cx,cy,x,y);        { add c }
           
mag := x*x+y*y;           { calculate square of magnitude }
           
inc(iter);                  { increment counter }
         
end;
          if mag < escape then        { output blue for non-escapees}
         
begin
           
inc(PlotTimes);
            LeftValue := i + left;

            ABitmap.Canvas.Pixels[LeftValue,j+top] := ABitmap.Canvas.Pen.Color;
            ABitmap.Canvas.Pixels[LeftValue,bottom-j] := ABitmap.Canvas.Pen.Color;
          end;
        end{ while loop}
       
jyScale := jyScale + yScale;
      end{j loop}
     
if PlotTimes = 0 then { If no plotted pixel }
       
i := RLimit + 1
     
else
      begin
       
inc(i);
        ixScale := ixScale + xScale;
      end;
    end;{ i loop}
 
end;
end;

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


� 2004 Jim Valavanis

Next

1