GWBasic Graphics
Screen 2
Just imagine, yet another screen mode to use. Providing you have the
hardware; and you almost certainly have, then you will have the
advantage of a better screen to do graphics with. See the SCREEN
statement in the Users Manual to see what all the advantages are with
Screen 2. Primarily, the screen has better definition than Screen 1
with 640 by 200 pixels.
By this time, you have learned how to copy the program text from these
examples to a source file and run it with GWBasic. I will provide a
Screen 2 example program and explain each step of the program. There
will be some new statements and a few algorithms that will prove useful
in your own programs later.
Just a
reminder that these exercises may be done on a floppy disk. This has
the distinct advantage of keeping all the incidental files created in a
location where they can be dealt with later. Of course, the exercise
may be done on the hard drive, but if some files get misplaces, they
may be hard to re-locate for subsequent deletion after the exercise.
Example of a Screen 1
graphics application
I have learned that the best way for me to create a program that I want
is to examine programs. I run them, evaluate them, edit them and
eventually get it the way I want. Copy and Paste this example into your
GWBasic folder and play around with it. It is a little longer than most
of the other examples, but I will comment on each step here.
Copy and paste this program to be run with GWBasic......
10 ' ________________ Input plot
parameters ____________
20 KEY OFF: COLOR 1,7: CLS
30 PRINT " Connect the points of the Rose
and Elipse"
40 '
50 LOCATE 2,1: PRINT STRING$(79,32)
60 LOCATE 2,1: INPUT " Enter the Elipse Width <0 TO 99> ",W$
70 IF W$="" THEN W$= "99"
80 W=INT(ABS( VAL(W$))): IF W>99 THEN 50
90 '
100 LOCATE 3,1: PRINT STRING$(79,32)
110 LOCATE 3,1: INPUT " Enter the Elipse Highth <0 TO 99> ",H$
120 IF H$="" THEN H$="99"
130 H=INT(ABS( VAL(H$))): IF H>99 THEN 100
140 '
150 LOCATE 4,1: PRINT STRING$(79,32)
160 LOCATE 4,1: INPUT " Enter the Rose Width <0 TO 99> ",A$
170 IF A$="" THEN A$="99"
180 A=INT(ABS( VAL(A$))): IF A>99 THEN 150
190 '
200 PRINT" Close any
key to view the 2 display"
210 N$= INKEY$: IF N$= "" THEN 210 ELSE CLS
220 '
230 '______________ plot the graph _______________
240 SCREEN 2
250 '
260 K= (2*3.1416)/360
270 AR= 2
280 HCL=640/2
290 VCL=200/2
300 '
310 '
320 FOR N= 1 TO 360 STEP 2
330 X1= H * SIN(N*K) * AR + HCL
340 Y1= W * COS(N*K) + VCL
350 PSET(X1,Y1)
360 '
370 '
380 P= A * COS(3*N*K)
390 X2= P * SIN(N*K) * AR + HCL
400 Y2= P * COS(N*K) + VCL
410 PSET(X2,Y2)
420 '
430 LINE (X1,Y1)-(X2,Y2)
440 '
450 T= TIMER
460 IF TIMER<T+.05 THEN 460
470 NEXT N
480 '
490 '
500 N$= INKEY$: IF N$= "" THEN 500
510 '
520 DEF SEG= &HB800
530 BSAVE "screen.x02",0,16384
540 '
550 SCREEN 0:COLOR 1,7: LIST
560 '
570 END
580 SAVE"screen_2",A
Brief line by line
explaination for the program
line
10
Program section banner. The ' makes it a remark
statement.....
line 20
My usual opening but with a different color.
line 30 A Banner
statement for when the program begins.
line
40
line
50 Prints 79 spaces at location row
2 column 1
line 60 Prompt for the width value for
the elipse
line 70 Conditions
input string variable
line 80 Conditions
input numeric variable. Repeat if unacceptable
line 90
line
100-130
The
same is done for the Elipse highth input into register H.
line 140
line 150-180 The same is done for
the Rose width input into register A.
line 190
line 200
Instructions for the program operator
line 210 Spin here till
something is input into N$ by pressing a key
line 220
line
230
Another program section banner,
plotting in screen 2 mode
line
240 Enter screen 2 mode
line 250
line 260 Constant to convert
degrees to radians for the trig functions
line 270 Aspect ratio for
matching absissa and ordinate scale factor
line 280 Horizontal Center
Line. One half the max absissa value
line 290 Vertical
Center line. One half the max ordinate value
line 300-310
line 320 For N equal 1
degree to 360 degrees in steps of 2 degrees
line 330 Calculate X1, point
for first angle of the FOR/NEXT loop
line 340 Calculate Y1, for
the same angle. note that K
converts degrees to radians.
line 350 Plot a point at that
location for the elipse
line 360-370
line 380 Calculate length of
the Rose at angle N with width A (polar co-ordinate format)
line 390 Calculate absissa
length for vector P,angle N
(rectangular co-ordinate format)
line 400 Calculate ordinate
value for rectangular cordinate (x2,y2)
line 410 Plot a dot at the
point using the PSET instruction.
line 420
line 430 Draw a line between
the two (x,y) points. Easily removed if you wish.
line 440
line 450 T= Real
time clock total seconds value in system variable TIMER.
line 460 Spin here till
system variable TIMER
becomes T
plus .05
line 470 Increment the angle
in N
and repeat the FOR/NEXT
loop operation.
line 480-490
line 500 Wait for a key
closure again.
line 510
line 520 Bsave the image
line 530 A control C in the
last wait will stop this Bsave from happening, if you wish.
line 540
line 550 Restor Screen 0 and
list the program during editing.
line 560
line 570 The END terminates
the program before it performs the next line of code.
line 580 Handy way to back-up
this program periodically during editing.
Some notes:
50 LOCATE 2,1: PRINT
STRING$(79,32)
60 LOCATE 2,1: INPUT " Enter the Elipse Width <0 TO 99> ",W$
70 IF W$="" THEN W$= "99"
80 W=INT(ABS( VAL(W$))): IF W>99 THEN 50
The gymnastic
with the input variable is to make the input user friendly.
The STRING$()
statement erases the line by posting 79 ascii spaces at location ROW 2,
COLUMN 1 using the LOCATE
instruction. The input statement is presented on the same line. If the
string needed to be repeated due to an error, then it wouldn't appear
on the new prompt because it would be erased before the prompt is
posted.
Input to a string variable permits any keystrokes. An error shows if a
non numeric or nothing is entered at the prompt if the input is
directed to a numeric variable. This input target is W$
I nothing is entered W$ will contain "", or no
value. If ENTER only is pressed then "99" will be inserted by the
IF statement as the default value. Makes it easy to key through the
program.
The string value in W$ is
converted to binary into W using
the VAL()
function. If the number is less than zero then its made posative with
the ABS()
function. Any fractional component is removed with the INT()
function. If the result is greater than 99, then another input is
requested.
260 K= (2*3.1416)/360
The GWBasc trig functions take their
angle parameter in Radians rather than degrees.
Therefore, the angle must be converted from degrees to radians.
Since 2 pi radians equal 360 degrees, then one degree equals two times
pi divided by 360.
270 AR= 2
The number of
horizontal pixels are doubled from Screen 1 to screen 2. To keep the x
and y scale the same, any x axis value should be doubled as well. The
aspect ratio of your monitor and the Windows screen scale you selected
can have an affect, but I found its easier to find the value
empreically. The value of 2 works real nice for
me.
280 HCL=640/2
290 VCL=200/2
Because 640
and 200 are the full scale pixel values for the agsissa and ordinate,
then the entire plot can be offset by one half of each to make
point(0,0) be in the center of the display. Notice also that the
direction of the ordinate is reversed in that the y value of 200 is at
the bottom of the screen. The absissa value of 640 is still
conventional for plots, at the right of the screen.
380 P= A * COS(3*N*K)
390 X2= P * SIN(N*K) * AR + HCL
400 Y2= P * COS(N*K) + VCL
I got the
equation for the ROSE from my math book that gives the formula in polar
co-ordinate form. The length of the vector is P and is
the COS
of three times the angular value in degrees. The value of K converts
the value of degrees in N to its
Radian value that the GWBasic COS function
uses.
This conversion to rectangular permits plotting using SCREEN 2's
PSET()
and LINE()
functions.
430 LINE (X1,Y1)-(X2,Y2)
We have
already plotted dots on the graph using the PSET instruction. For fancy
patterns, with various ROSE and ELIPSE sizes, the LINE() function joins
the dots of one figure with the other for the given angles selected in
the FOR/NEXT loop. Remove step 430 and only the dots will be plotted.
Use 0 values for either figure and only the other figure will be
displayed. Select a FOR/NEXT STEP value of 1 (default) and the figure
will essentially be plotted as a line representation.
450 T= TIMER
460 IF TIMER<T+.05 THEN 460
470 NEXT N
The Real Time
clock has its own register that counts the total number of elapsed
seconds. It constantly updates a register that can be called as a
numeric variable named TIMER with
GWBasic. TIMER can be accessed at any time, the same as any other
GWBasic numeric variable. Its handy for delays because the TIMER
variable takes the same amount of time for different computer stations,
even though the system clock may be faster or slower for different
computers. The output of the Real Time Clock is the same for all
computers. A simple FOR/NEXT loop to kill time for a program delay
would be faster or slower for computers with different system clocks.
Summary
Screen 2 may be used to create graphics display using the GWBasic
mathmatical instruction. It also permits the use of ascii text to
prompt the operatore for inputs needed in the plot.
The use of Screen 2 will depend on your stations configuration because
the Video display and Extended Video hardware has evolved a great deal.
A google search for CGA, EGA, VGA, XGCA, are a few search target
strings. The best way to learn your particular features is to
jump in and see what you can do. My example should work for most
configurations; however, there are many more features that you may wish
to use on your station.
In short.... play around and have fun.........