Graphics Examples - Tutorial 010

The programs of this tutorial are developments of some found in the chapter "Graphics I" in Alan Thomas'  "Further Programming for the BBC Micro" . On page 1 Dr Thomas gives the following recipe for budding programmers in BBC Basic :

   10 REPEAT
   20   Read any program
   30   Type it in
   40   RUN it
   50   Try the suggested alterations
   60   Try your own ideas
   70 UNTIL fed up


This I have done. Many thanks to you Dr Thomas! Make some changes yourself and email your program for publication on this page....

POLYGONS
This program draws polygons when you tell it how many sides (n) to draw. If n is big enough a circle is approximated.

      REM Polygons approximate a Circle
      REM RGWeston 20.2.03
      MODE8
      COLOUR 8,15
      REPEAT
        INPUT"Sides = "n
        ORIGIN 640,512
        R=300
        t=0
        dt=2*PI/n
        MOVE R,0
        FOR i = 0 TO n
          GCOL0,n MOD 15
          DRAW R*COS(t), R*SIN(t)
          t=t+dt
        NEXT i
      UNTIL FALSE


Notes :

COLOUR 0,15 Redefines Colour Black to intense White (so we can see it!)
COLOUR 8,15 Redefines Colour 8 (grey-hard to see) to intense white

R is the "Radius" - when n is large (30 or so) the polygon looks quite circular

t is the angle (theta) at the centre of the polygon

PI is the ratio of the circumference to the diameter of a circle ( 3.142....)

dt is the increase in t as a fraction of a full turn (2*PI radians is equivalent to 360 degrees)

n MOD 15 always gives a number less than 15 so, if n is 17, then (n MOD 15) is 2; if n =31 then n MOD 15 is 1; thus we always get a visible colour for the polygon.

t=t+dt increases the value of t by dt each time the FOR... NEXT loop comes round.
( Another way of programming this is t += dt, but I don't find this as readable so don't use it much).

INTERFERENCE PATTERN

      MODE8
      REM ORIGIN 640,512
      FOR st =20 TO 1 STEP -4
        FOR y=0 TO 1024 STEP st
          MOVE 5000,512
          DRAW 0,y
          delay=INKEY(1)
        NEXT y
        delay=INKEY(100)
        CLS
      NEXT st

Linescope - press a key to restart

            MODE8:OFF
      COLOUR 0, 2
      COLOUR 8, 1
      mx=640:my=512
      ORIGIN 640,512
      st=RND(100)
      x=RND(mx) : y=RND(my)
      sw=0
      n=0
      REPEAT
        n=n+1
        GCOL0,n MOD 15
        delay=INKEY(1)
        x1=x: y1=y
        IF sw=1 THEN x1=(mx/st)*RND(st)
        IF sw=0 THEN y1=(my/st)*RND(st)
        MOVE x,y : DRAW x1,y1
        MOVE -x,y : DRAW -x1,y1
        MOVE -x,-y DRAW -x1,-y1
        MOVE x,-y : DRAW x1,-y1
        x = x1 : y = y1
        sw=(sw+1) MOD 2
        G$=INKEY$(10)
      UNTIL n=40
      delay=INKEY(200)
      RUN

Thread Pattern

      MODE8
      COLOUR 0, 15
      COLOUR 8,15
      R=500
      CLS:ON
      INPUT"n = "n
      OFF
      dt=2*PI/n
      ORIGIN 640,512
      t=0
      FOR j=0 TO n-1
        GCOL0,j MOD 15
        u=2*j*PI/n
        x=R*COS(u) : y=R*SIN(u)
        FOR i= 0 TO n
          MOVE x,y
          DRAW R*COS(t), R*SIN(t)
          t=t+dt
          delay=INKEY(100/n)
        NEXT i
      NEXT j
      PRINTTAB(60,30)"Press SPACE BAR"
      hold=GET
      RUN

Next Tutorial

Richard Weston's Homepage