' ******************************************************************************
' **                            Mandelbrot Fractals                           **
' **                               in GFA Basic                               **
' **                            by Stephen Lovejoy                            **
' **                                Portland, Or                              **
' ******************************************************************************
' Based on an ST Basic Program in "Atari ST Graphics & Sound"
' by J. Walkowiak      Published by Abacus Software
Infinity=10000000      ! High limit for calculations
Cls                    ! Clear the screen
If Xbios(4)<>2 Then    ! Is it High Resolution?
  Alert 1,"Sorry, this program|runs in Monochrome|    only!",1,"[Geez!]",B
  End       ! Life is hard, then you die.
Endif
Print Tab(30);"BASIC Fractal--   Julia/Mandelbrot "
For X=1 To 80   ! I hate seeing a singleprint statement with a repeated character.
  Print "=";
Next X
Input "Calculation depth [ex. 10..99]";Calcdepth    ! Higher calculation depth is prettier, but slower.
Input "Character depth   [ex   1..33]";Chardepth    ! See above.
Xwidth=640    ! Screen Size
Height=400
Input "remin [ex. -0.75  0.0   1.1   0.14]";Remin    ! Real Number Minimum
Input "remax [ex.  2.25  0.2   1.27  0.18]";Remax    ! Real Number Maximum
Input "immin [ex. -1.2  -1.06 -0.35 -1.05]";Immin    ! Imaginary Number(!) Minimum
Input "immax [ex.  1.2  -0.9  -0.25 -1.02]";Immax    ! Isn't this obvious by now?
Gosub Warning   ! Beware of Short programs that take a long time.
Cls
Dreal=(Remax-Remin)/(Xwidth-1)
Ddimag=(Immax-Immin)/(Height-1)
For Iimag=0 To Height       !  Main Thinker tinker.
  For Ireal=0 To Xwidth
    Lreal=Remin+Ireal*Dreal
    Limag=Immin+Iimag*Ddimag
    Re=Lreal
    Im=Limag
    Depth=0
    Gosub Recalc
    Gosub Sub
  Next Ireal
Next Iimag
Repeat   ! lets you see what held up your ST so long without that nasty "Program End" box.
Until Inkey$<>""
End
Procedure Recalc    ! Where all the local squares hang out.
  Repeat
    Xx=Re*Re
    Yy=Im*Im
    Im=2*Re*Im-Limag
    Re=Xx-Yy-Lreal
    Inc Depth     ! That means increment Depth by 1. Faster Than Depth = Depth + 1.
  Until ((Depth=Calcdepth) Or ((Xx+Yy)>Infinity))
Return
Procedure Plotting     ! Draw a dot
  If (Depth>Chardepth) Then
    Plot Ireal,Iimag
  Else
    Gosub Rte350
  Endif
Return
Procedure Rte350
  If (Depth Mod 2) Then
    Plot Ireal,Iimag
  Endif
Return
Procedure Sub
  If (Depth<Calcdepth) Then
    Gosub Plotting
  Endif
Return
Procedure Warning    ! Last chance without reboot.
  Alert 1,"This is going to tie up|your ST for quite a While.",2,"Ack!|[Go Ahead!]",B
  If B=1 Then
    End
  Endif
Return
