This is only a preview of the January 1997 issue of Silicon Chip. You can view 24 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Control Panel For Multiple Smoke Alarms; Pt.1":
Articles in this series:
Articles in this series:
Items relevant to "Build A Pink Noise Source":
Items relevant to "Computer Controlled Dual Power Supply; Pt.1":
Purchase a printed copy of this issue for $10.00. |
COMPUTER BITS
BY RICK WALTERS
Drawing circles in GW-Basic
In the July 1996 issue, we discussed drawing
borders on the screen using GW-Basic. This
month, we will show you how to draw an
analog clock on the screen.
To draw circles, or for any acceptable current graphics, we need to
use the highest resolution screen we
can access. This is SCREEN 9 in GW
Basic. Next we need to choose some
colours that look effective. You can
play around with the COLOR statement
on line 1270 but be careful. Black on
black is very hard to see!
If we want to draw an analog clock
face the first step is to draw a circle.
This is done with the CIRCLE statement on line 2030. You will notice that
we have not used numeric values for
the circle centre and radius but values
we have defined previously in the INIT
subroutine.
38 Silicon Chip
As we will have to use these values
again in the “update hands” subroutine, if we need to re-position the
clock on the screen it is only necessary to change this one set of values
on line 1080, rather than trying to
find all the lines where the values
were used.
Perhaps we should elaborate on
SCREEN 9 at this point. It consists of
640 x 350 pixels for drawing graphics
but at the same time allows you to
print text on an 80 x 25 grid. Thus,
each character will occupy (640/80)
= 8 pixels by (350/25) = 14 pixels. We
can’t draw graphics on the 25th line,
although we can place text there.
Once we have drawn the circle and
it looks about the right diameter, we
have to calculate the position of each
of the numbers which are placed
every 30° around the circle. This is
done using the formula on line 2050,
which calculates the sine and cosine
of the angle and locates the cursor at
that point ready to print the number.
So far we have a clock face, now for
some hands. How do we know where
to place them?
PRINT TIME$ in Basic looks at the
computer’s internal clock and prints
this time, so if we use this function
our clock will be as accurate as the
computer’s is. Line 1260 dissects the
time readout and defines the three
parameters we are interested in: hours,
minutes and seconds. By defining
them as functions (DEF FN) they are
available immediately; we don’t have
to access the time and dissect it each
time we need it.
To synchronise the analog clock
time to the computer we save the
current second in line 3040 and wait
until the next second begins. Initially
there are no hands drawn, but once
they are drawn they have to be erased
and re-drawn every second. This is
done in lines 3070-3120.
To finish off we draw a couple of
small circles at the centre of the face
in line 3130.
Right, you now have the bare bones
clock which is capable of displaying
and updating the time. If you want to
improve it, add the individual minutes between the five minute marks
and perhaps make the face a different
colour to the surrounding screen.
Look up your Basic manual for more
information on color.
Next time we will discuss sequential and random access files and the
methods used for storing and retrievSC
ing data.
Listing 1
1 GOTO 10
2 GOSUB 1890: LPRINT TAB(55);” Printed on “;TODAY$
1200 ULI = CHR$(195): DLI = CHR$(204): URI = CHR$(180): DRI =
CHR$(185)
3 LLIST
1205 ‘Single & Double Left & Right intersections
4 END
1210 UTI = CHR$(194): DTI = CHR$(203): UBI = CHR$(193): DBI =
CHR$(202)
5 SAVE “C:\clock”,A ‘Save file on C drive
6 SAVE “B:\clock”,A ‘Save file on B drive
7 END
10 REM Draw analog clock on screen
11 REM run 2 will print listing on printer
12 REM run 5 will save program to drive C
13 REM run 6 will save program to drive B (change to A if required)
14 REM GOSUB 1900 Will clear from current cursor line to Line 24
20 GOSUB 1030 ‘Initialise
30 GOSUB 2030 ‘Draw face
40 GOSUB 3050 ‘Draw current time
50 GOSUB 3030 ‘Erase & update hands
1215 ‘Single & Double Top & Bottom intersections
1220 UCI = CHR$(197): DCI = CHR$(202) ‘Single & Double Centre
intersection
1230 PI = 3.14159: BH = 8: RH = 4 ‘Black & red hands
1240 DEF FNS(X) = SIN (X * PI/180) ‘Define sine function from radians
1250 DEF FNC(X) = COS (X * PI/180) ‘Define cosine function from radians
1260 DEF FNHR$ = LEFT$(TIME$,2): DEF FNMIN$ = MID$(TIME$,4,2):
DEF FNSEC$ = RIGHT$(TIME$,2)
1270 SCREEN 9: FC = 4: BC = 7: COLOR FC,BC ‘Fore & background
1280 YC = 175: XC = 325: RAD = 215 ‘Circle centre & radius
1890 TODAY$ = MID$(DATE$,4,2) + “-” + LEFT$(DATE$,2) + “-” +
RIGHT$(DATE$,2)
60 LOCATE 25,1: PRINT FNCENTRE$(“Press SPACEBAR to return to
DOS.”);
1899 RETURN
70 K = INKEY$: IF K < > “” THEN 999
1910 ‘Clear to end of screen subroutine.
80 GOTO 50
999 END’SYSTEM ‘Erase END’ when running OK
1000 ‘***********************
1010 ‘Initialisation routine.
1020 ‘***********************
1030 KEY OFF: DEFINT A-Z: DEFSTR D,E,K,U: DEFSNG P,S,C
1040 TODAY = VAL(MID$(DATE$,4,2))
1050 ESC = CHR$(27): ENTER = CHR$(13): KSP = CHR$(32) ‘Spacebar
1060 KLA = CHR$(0) + CHR$(75): KRA = CHR$(0) + CHR$(77) ‘Left &
right arrows
1070 KUA = CHR$(0) + CHR$(72): KDA = CHR$(0) + CHR$(80) ‘Up &
down arrows
1080 KPU = CHR$(0) + CHR$(73): KPD = CHR$(0) + CHR$(81) ‘Page
up & down
1090 KHOME = CHR$(0) + CHR$(71): KEND = CHR$(0) + CHR$(79)
‘Home & end
1100 DATA January, February, March, April, May, June, July
1110 DATA August, September, October, November, December
1120 DIM MONTH$(12): FOR A = 1 TO 12: READ A$: MONTH$(A) = A$:
NEXT
1130 MONTH$ = MONTH$(VAL(LEFT$(DATE$,2))) ‘Current month
1140 DEF FNCENTRE$(M$) = SPACE$((79 - LEN(M$))/2) + M$ ‘Centre
text
1150 DEF FNCEOL$ = STRING$(79 - POS(Q),” “)
1160 DEF FNYN = INSTR((“ YyNn”) + ENTER + ESC,INKEY$)
1165 ‘0 or 1, no key, 2 or 3 - Y, 4 or 5 - N, 6 - enter, 7 - escape
1170 ULT = CHR$(218): DLT = CHR$(201): URT = CHR$(191): DRT =
CHR$(187)
1175 ‘Singe & Double Left & Right top corners
1180 ULB = CHR$(192): DLB = CHR$(200): URB = CHR$(217): DRB =
CHR$(188)
1185 ‘Singe & Double Left & Right bottom corners
1190 UH = CHR$(196): DH = CHR$(205): UV = CHR$(179): DV =
CHR$(186)
1195 ‘Single & Double Horizontal & vertical lines
1900 ‘**********************************
1920 ‘**********************************
1930 VIEW PRINT CSRLIN TO 24: CLS: VIEW PRINT
1999 RETURN
2000 ‘****************
2010 ‘Draw clock face.
2020 ‘****************
2030 CIRCLE (XC,YC),RAD ‘Draw circle centre XC,YC radius RAD
2040 FOR A = 1 TO 12
2050 LOCATE 13 - 10 * FNC(30 * A),40 + 24 * FNS(30 * A) ‘Calculate
position
2060 PRINT A;: NEXT ‘write numbers
2099 RETURN
3000 ‘*************
3010 ‘Update hands.
3020 ‘*************
3030 OLDSEC = VAL(FNSEC$): OLDMIN = VAL(FNMIN$) + VAL(FNSEC$)/60:OLDHR = VAL(FNHR$) MOD 12 + VAL(FNMIN$)/60
3040 WHILE OLDSEC = VAL(FNSEC$): WEND ‘Wait for next second
3050 SEC = VAL(FNSEC$): MIN = VAL(FNMIN$) + VAL(FNSEC$)/60: HR
= VAL(FNHR$) MOD 12 + VAL(FNMIN$)/60
3060 ‘ erase old hands, draw new ones
3070 LINE (XC,YC) - (XC + (180 * FNS(6 * OLDSEC)),YC - 130 * FNC(6
* OLDSEC)),BC
3080 LINE (XC,YC) - (XC + (180 * FNS(6 * SEC)),YC - 130 * FNC(6 *
SEC)),RH
3090 LINE (XC,YC) - (XC + (172 * FNS(6 * OLDMIN)),YC - 126 * FNC(6
* OLDMIN)),BC
3100 LINE (XC,YC) - (XC + (172 * FNS(6 * MIN)),YC - 126 * FNC(6 *
MIN)),BH
3110 LINE (XC,YC) - (XC + (154 * FNS(30 * OLDHR)),YC - 112 *
FNC(30 * OLDHR)),BC
3120 LINE (XC,YC) - (XC + (154 * FNS(30 * HR)),YC - 112 * FNC(30 *
HR)),BH
3130 FOR A = 1 TO 4: CIRCLE (XC,YC),A: NEXT
3199 RETURN
January 1997 39
|