Home > Programming > Fractals > Julia Sin

juliasin.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 JuliaSinZ(ABitmap: TBitmap);
{ compute and display Julia set of sin z}
var
  
i, j      : integer;         { loop variables}
  
MaxX,
   MaxY      : integer;        { Maximum Y coordinate}
  
MaxY2     : integer;
   scale     : double;          { scale factor }
  
mag       : double;          { square of magnitiude of complex number }
  
iter      : integer;         { escape iteration counter }
  
continue  : boolean;        { continue iteration counter }
  
x,y       : double;          { real and complex parts of z }
  
MaxColor  : byte;            { maximum number of colors }
  
MaxColor2 : byte;
   curColor   : TColorRef;
   Rect: TRect;
   color      : byte;
   zoom,
   ixScale,
   jyScale : double;
   iLeft,
   iRight,
   jTop,
   jBottom: integer;
   xStop,yStop : integer;
   oldPEN      : TColor;
   colorLimit  : integer;
begin
 
SetRect(Rect, 0, 0, ABitmap.Width, ABitmap.Height);
  with Rect do
  begin
   
zoom := 2.0;
    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;
    MaxColor := 15; { Maximum number of colors }
   
MaxColor2 := MaxColor*2;
    colorLimit := -1;
    scale:= 2.0*zoom/MaxY;    { calculate zoom factor}
   
ixScale := 0;

    curColor := GetRGBColor(maxColor);
    oldPEN := ABitmap.Canvas.Pen.Color;
    ABitmap.Canvas.Pen.Color := curColor;

    xStop := trunc(MaxY / 100 * 37) ;
    yStop := MaxY div 5;

    for i := 0 to xStop do
    begin
     
jyScale := 0;
      for j := 0 to yStop do
      begin
       
x := ixScale-zoom;             { set starting value of real(z) }
       
y := zoom-jyScale;             { set starting value of imag(z) }
       
continue := true;              { assume point does not escape  }
       
iter :=0;
        while continue do
        begin
         
csin(x,y,x,y);                    { calculate complex sine }
         
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
               
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}
     
iLeft := i+ Left;
      iRight := Right-i;
      jTop := yStop+Top;
      jBottom := Bottom - yStop;

      ABitmap.Canvas.MoveTo(iLeft,jTop);
      ABitmap.Canvas.LineTo(iLeft,jBottom);

      ABitmap.Canvas.MoveTo(iRight,jTop);
      ABitmap.Canvas.LineTo(iRight,jBottom);
      ixScale := ixScale + scale;
    end;{ i loop}
   
for i := xStop to maxY2 do
    begin
     
ABitmap.Canvas.MoveTo(i+left,top);
      ABitmap.Canvas.LineTo(i+Left,bottom);
      ABitmap.Canvas.MoveTo(right-i,top);
      ABitmap.Canvas.LineTo(right-i,bottom);
    end;

    ABitmap.Canvas.Pen.Color := oldPEN;
  end;
end;

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


2004 Jim Valavanis

Previous

Next

1