Tutorial 019 Mandelbrot  for BB4W


The following listing is an adaptation for BB4W from one given in "Computer Science for A Level" by Ray Bradley

      REM : Mandelbrot Set
      REM : after R Bradley
      MODE8
      *FLOAT64
      MagZ=0
      x=0:y=0
      FOR p=-2 TO 2 STEP 0.01
        FOR q= -2 TO 2 STEP 0.01
          Loop=1
          WHILE ABS(MagZ) < 100 AND Loop <64
            X= x^2 -y^2 + p
            Y= 2*x*y + q
            MagZ = SQR(x^2 + y^2)
            x=X : y=Y
            Loop=Loop + 1
          ENDWHILE
          GCOL Loop
          PLOT69,(p+2)*200,(q+2)*200
          x=0 : y=0 : MagZ=0
        NEXT q
      NEXT p
      PRINT"Mandelbrot Set"



 To see better what's going on (though at a severe cost in speed) we can print the values of p,q and Loop (see lines 80, 110 and 200 below)

   10 REM : Mandelbrot Set
   20 REM : after R Bradley
   30 MODE8:OFF
   40 *FLOAT64
   50 MagZ=0
   60 x=0:y=0
   70 FOR p=-2 TO 2 STEP 0.01
   80   PRINTTAB(1,1)"p = ";p;SPC(8)
   90   FOR q= -2 TO 2 STEP 0.01
  100    
  110     PRINTTAB(20,1)"q = ";INT(q);SPC(8)
  120     Loop=1
  130     WHILE ABS(MagZ) < 100 AND Loop <64
  140       X= x^2 -y^2 + p
  150       Y= 2*x*y + q
  160       MagZ = SQR(x^2 + y^2)
  170       x=X : y=Y
  180       Loop=Loop + 1
  190       :
  200       IF Loop>30 PRINTTAB(40,1)"Loop>30 = ";Loop;SPC(8)
  210     ENDWHILE
  220     GCOL Loop
  230     PLOT69,(p+2)*200,(q+2)*200
  240     x=0 : y=0 : MagZ=0
  250   NEXT q
  260 NEXT p
  270 PRINT"Mandelbrot Set"



Notes:  At line 40 *FLOAT64 preserves accuracy and increases the speed of plotting.

Next Tutorial

Richard Weston's Homepage