Draw a Koch curve. See details: Koch curve

    ' Chaos:  start at any point,  this program uses the middle of the screen  (or universe.  One of six
    ' degrees of freedom (a direction)  is chosen at random  (by throwing a six-sided die),  and a line
    ' is drawn from the old point to the new point in the direction indicated by the pip on the die.
    '
    ' The traverse distance is always a fraction of the last distance drawn;  the fraction (here) uses:
    '
    '                                       +-                                -+
    '                                       |                                  |
    '       distance  <===  old_distance *  |  1/2 - 1/8 - 1/32 - 1/128 - ...  |
    '                                       |                                  |
    '                                       +-                                -+
    '            ---or---
    '                                       +-                                -+
    '                                       |   1     1     1      1           |
    '       distance  <===  old_distance *  |  --- - --- - ---- - ----- - ...  |
    '                                       |  2**1  2**3  2**5   2**7         |
    '                                       +-                                -+
    '
    ' (The series above has a limit of  1/3.)
    '
    ' The six degrees of freedom:                      1         6
    '
    '                                                    \     /
    '                                                     \   /
    '                                                      \ /
    '                                            2  <------ X ------>  5
    '                                                      / \
    '                                                     /   \
    '                                                    /     \
    '
    '                                                  3         4
    '
    ' When the amount to be moved is too small to show on the terminal screen,  the chaos curve is
    ' starting again  (from the initial point,  the middle of the screen/universe).
    '
    ' All subsequent chaos curves are superimposed on the first curve.
    '
    ' The envelope of this chaos curve is defined as the snowflake curve.
    '
    ' If any cursor key (one of the "arrow" keys) is pressed,  program execution is halted.
    '
    ' If any function key is pressed during execution, the random chaos curve is stopped, the screen
    ' cleared, and the snowflake curve is drawn by a non-random method (brute force).
    '
    ' Once the random snowflake (chaos) curve is being drawn, the pressing of function keys 1-->9 will
    ' force the randomness to move in a particular direction, the direction (the degree of freedom) is
    ' the direction indicated by the number of times that function key is pressed for that curve point.
    ' That is, function key 1 is used for the first point (part of the chaos curve),  function key 2 is
    ' used for the second point, function key 3 for the third point,  etc.
 
    DEFINT A-Y                    ' define variables that begin with   A-->Y   as integers.
    DEFSNG Z                      ' define variables that begin with     Z     as single precision.
    DIM XP(16,6),YP(16,6),KY(16)  ' define some (integer) arrays.
    MP= 16                        ' set the maximum number of points (1st dimension) that can be plotted.
    CLS                           ' clear the screen for visual fidelity.
    SCREEN 2                      ' make the screen high-res graphics.
    GOTO 230                      ' branch around a  RETURN  statement that  ON KEY(i) uses.

220 RETURN
230 FK= 0                         ' set FK  (used to indicate that a function key was pressed).
 
      FOR I=1  TO 10              ' allow the use of function keys to stop the deliberate snowflake
                                  ' curve and start drawing it randomly.
      KY(I)= 0
      ON KEY(I)  GOSUB 220        ' allow the trapping of function keys,  but don't process it as yet.
      KEY(I)  ON
      KEY(I) STOP
      NEXT I
 
    CLS                           ' clear the screen for visual fidelity.
    ZZ= 2 + TIMER                 ' on some PCs, a pause of at least one second prevents scrolling.

240 IF TIMER<ZZ  THEN GOTO 240
    RANDOMIZE TIMER               ' randomize the RND function from the timer.
    XM= 640 - 1                   ' define the number of points on the screen (for plotting).
    YM= 200 - 1
    XO= XM \ 2                    ' define the origin of the chaos curve.
    YO= YM \ 2
    ZT= 1 / 3                     ' define the traverse distance,  it's this distance that each part of
                                  ' the chaos curve "breaks",  when the distance that the next part of
                                  ' the chaos curve is moved to.
    ZA= 1                         ' define the aspect ratio for the terminal screen.
    ZX= XM * ZA                   ' define the initial distance to be plotted (for a line).
    ZY= YM                        '    "    "     "        "     "  "    "      "  "   "
           FOR I=1  TO MP         ' compute (once) all the  x & y  distances for each part of the curve.
           ZX= ZX * ZT * ZA
           ZY= ZY * ZT
           XP(I, 1) = -ZX / 2
           XP(I, 2) = -ZX
           XP(I, 3) = -ZX / 2
           XP(I, 4) =  ZX / 2
           XP(I, 5) =  ZX
           XP(I, 6) =  ZX / 2
           YP(I, 1) = -ZY
           YP(I, 2) =   0
           YP(I, 3) =  ZY
           YP(I, 4) =  ZY
           YP(I, 5) =   0
           YP(I, 6) = -ZY
           NEXT I
    N0=0
           FOR II=1  TO MP        ' find the maximum number of points that can be plotted.
             FOR I=1  TO 6
             IF XP(II, I) <> 0  THEN N0= II
             IF YP(II, I) <> 0  THEN N0= II
             NEXT I
           NEXT II
 
           FOR I=11  TO 14        ' quit if any cursor key is pressed.
           ON KEY(I)  GOSUB 598
           KEY(I)  ON
           NEXT I
 
           FOR I=1  TO 10         ' If any function key is pressed during execution, the deliberate
           ON KEY(I)  GOSUB 400   ' curve is stopped,  the screen is cleared, and the snowflake curve is
           KEY(I)  ON             ' drawn by a random process  (AKA,  the chaos curve).
           NEXT I
 
    GOTO 500

400 FK= 1                         ' come here when any function or cursor key is pressed,  and set  FK
                                  ' that is checked by the deliberate snowflake curve generator.
    RETURN

500 CLS                           ' clear the screen before starting  (for visual fidelity).
                  FOR I1=1  TO 6  ' plot the curve via non-random (deliberate calculation) points.
                  X1= XO  +  XP(1, I1)
                  Y1= YO  +  YP(1, I1)
                  IF FK  THEN GOTO 600
                  LINE (XO, YO)  -  (X1, Y1)
                    FOR I2=1  TO 6
                    X2= X1  +  XP(2,I2)
                    Y2= Y1  +  YP(2,I2)
                    IF FK  THEN GOTO 600
                    LINE (X1, Y1)  -  (X2, Y2)
                      FOR I3=1  TO 6
                      X3= X2  +  XP(3, I3)
                      Y3= Y2  +  YP(3, I3)
                      IF FK  THEN GOTO 600
                      LINE (X2, Y2)  -  (X3, Y3)
                        FOR I4=1  TO 6
                        X4= X3  +  XP(4, I4)
                        Y4= Y3  +  YP(4, I4)
                        IF FK  THEN GOTO 600
                        LINE (X3, Y3)  -  (X4, Y4)
                          FOR I5=1  TO 6
                          X5= X4  +  XP(5, I5)
                          Y5= Y4  +  YP(5, I5)
                          IF FK  THEN GOTO 600
                          LINE (X4, Y4)  -  (X5, Y5)
                          NEXT I5
                        NEXT I4
                      NEXT I3
                    NEXT I2
                  NEXT I1
    ZZ= 10+TIMER                  ' The snowflake curve is now complete.

555 IF TIMER<ZZ  THEN GOTO 555    ' loop for ten seconds.
598 SYSTEM                        ' stick a fork in it, we're all done.

600 ON  KEY(1)   GOSUB 710        ' trap all function keys for toggling.
    ON  KEY(2)   GOSUB 720
    ON  KEY(3)   GOSUB 730
    ON  KEY(4)   GOSUB 740
    ON  KEY(5)   GOSUB 750
    ON  KEY(6)   GOSUB 760
    ON  KEY(7)   GOSUB 770
    ON  KEY(8)   GOSUB 780
    ON  KEY(9)   GOSUB 790
    ON  KEY(10)  GOSUB 700
 
              FOR I=1  TO MP      ' re-active trapping all the function keys.
              KEY(I)  ON
              NEXT I
    CLS                           ' clear the screen before starting.
    GOTO 900                      ' go and start drawing the chaos curve.

700           FOR I0=1  TO MP     ' reset all toggle settings for all points.
              KY(I0)= 0
              NEXT I0
    RETURN

710 KI= 1                         ' toggle setting for point #1 (bypass).
              GOTO 800
720 KI= 2                         ' toggle setting for point #2 (bypass).
              GOTO 800
730 KI= 3                         ' toggle setting for point #3 (bypass).
              GOTO 800
740 KI= 4                         ' toggle setting for point #4 (bypass).
              GOTO 800
750 KI= 5                         ' toggle setting for point #5 (bypass).
              GOTO 800
760 KI= 6                         ' toggle setting for point #6 (bypass).
              GOTO 800
770 KI= 7                         ' toggle setting for point #7 (bypass).
              GOTO 800
780 KI= 8                         ' toggle setting for point #8 (bypass).
              GOTO 800
790 KI= 9                         ' toggle setting for point #9 (bypass).

800 KY(KI)= (1 + KY(KI) )  MOD 7  ' reset toggle settings for all higher points.
 
              FOR IK=KI+1  TO MP
              KY(IK)= 0
              NEXT IK
    RETURN

900 N= 0                          ' initialize the number of points in this particular chaos curve.
    X= XO                         ' move the start-of-the-chaos-curve to the origin.
    Y= YO
    LINE (X, Y)  -  (X, Y)
    N= N + 1                      ' bump number of points drawn so far.
    IF N>N0   THEN GOTO 900       ' # points drawn exceeds possible?  Start another chaos curve.
                                  ' start of diminishing loop to create an envelope for the chaos curve.
    IF KY(N)  THEN R= KY(N)   ELSE R= 1 + INT(RND*6)
    X= X  +  XP(N, R)             ' exercise a degree of freedom (one of six).
    Y= Y  +  YP(N, R)
    LINE -(X, Y)                  ' depending on the "die",  draw the next part of the chaos curve.
                   GOTO 900       ' now, go and do another point.