Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 9, NO. 3 / MARCH 1983 / PAGE 204

TRS-80 graphics made almost painless: part 2. John Crew.

TRS-80 Graphics Made Almost Painless

This is the second article in a three part series. The first segment appeared in the January 1982 issue. Here we discuss some quirks of Level II Basic and describe Vector Plotter, a program that draws lines on the sereen between any two points. Vector Plotter can produce random vectors, or you can supply the x, y coordinates of the initial and terminal points of a vector.

Many people don't know what vector means because it is often misused. The correct difinition--used by mathematicians, engineers, and scientists other than biologists--is a line which has two properties, length and direction. Some programmers use vector to mean array. Airplane pilots and science fiction writers often use it in place of direction. Biologists use vector to mean "a disease carrying organism.'

Peculiarities of Level II

There are two problems with the VAL function, which are not mentioned in the reference manual. The first problem is that VAL doesn't recognize numeric character strings preceded by a minus sign if there are blanks before the minus sign. That problem was described in Radio Shack's "Microcomputer News' (Oct. and Nov. 1980 issues). The second problem is that if a percent symbol is the first nonblank character after a string of numeric characters, an SN (syntax) error message will be printed when VAL is used on that string.

Apparently the programmers at Microsoft were uncertain about what should be done in this case. They could divide the value by 100 to get a decimal equivalent, they could leave the number as a percentage, or they could have an error result and leave it to you to write an error handling subroutine to perform whichever calculation you want.

They chose the latter option, but for some reason they call it an SN error instead of an FC (illegal function argument) error. Listing 1 demonstrates both problems with the VAL function. Listing 2 shows the extra lines needed to make the program in Listing 1 work as desired.

If you want the decimal equivalent of a percentage instead of the percentage returned by VAL, then use an error handling subroutine like the one in Listing 2 but insert/100 after

LV=VAL (LEFT$ (B, K - 1))

in line 100.

Because I often want compact programs, I sometimes use IF-THEN statements with an implied THEN. The Level II reference manual doesn't say when THEN is unnecessary, so by experiment I discovered when it can be omitted.

Figure 1 shows different legal IF-THEN-ELSE statements, most of which use an implied THEN, that don't work properly.

The first two examples in Figure 2 have the same error. In a compact IF-THEN-ELSE statement with an implied THEN, if the logical expression ends with a string constant, the THEN branch works properly, but the ELSE branch will be ignored. That problem can be solved by reversing the last comparisor (so it is "YES'=IN$ ) or by inserting a comma, blank space, or THEN between the logical expression and the THEN branch.

Another solution would be to put parentheses around the logical expression. That problem is one of the few cases I know of which can be solved by inserting a space; Level II usually ignores spaces.

An odd IF-THEN-ELSE statement I found doesn't have a THEN branch. It is


If the logical expression is true, nothing is done, otherwise the ELSE branch is taken.

I suggest you avoid using unusual forms of IF-THEN or IF-THEN-ELSE statements because line renumbering programs. Basic complers, and other versions of Basic almost certainly won't allow such things. Bessides those problems, using odd formats of Basic statements makes your programs hard to read or debug. Only use unusual forms of Basic statements If you desperately need to save memory or you want your program to be incomprehensible.

Multiple IF-THEN-ELSE statements

I often use complex, multiple IF-THEN-ELSE statements to consolidate a long process into one porgram line. This eliminates many GOTO statements so the program is easier to read and runs faster. The Level II manual isn't very helpful in explaining how to write complex, multiple IF-THEN-ELSE statements and it even gives an incorrect example on page 4/17. Contrary to the claim in the manual, you can't nest IF-THEN-ELSE statements within an IF-THEN-ELSE statement. (The writers of the manual were thinking of Fortran or perhaps PL/1. ) ELSE is matched with the most recent THEN (or implied THEN) in that program line. See the following listings for examples.

How To Use Vector Plotter

In the instructions you will see the maximum number of pairs of endpoints which can be stored (which is the same as the number of vectors which can be stored). The maximum number of vectors is calculated in line 100 based on the amount of free memory. The formula was arrived at by experimentation. Some free space is left for use by Basic. The more free memory you have, the smaller the percentage used for the coordinate array.

After the instructions, you are asked if you want random vectors. Usually, my programs look for Y and anything else is treated as no, but in line 150 of Vector Plotter you must reply Y or N.

If you don't want random vectors, you are next asked how many vectors you want to enter. You should ask for as many or more than you expect to enter. The computer then asks for the coordinates of the initial and terminal points. After you enter each pair of endpoint coordinates, the number of endpoint pairs entered so far is printed. You may leave this loop early by pressing the S key instead of entering coordinates. To see instructions, press the H key instead of entering coordinates. H and S are recognized only when they are pressed before you have typed anything in response to an input request.

Entering Coordinates

If you want a nonzero x coordinate, type if first. Then if you want a nonzero y coordinate, type a decimal point followed by the y coordinate. If the y coordinate is only one digit, put a zero between the decimal point and the digit. If you press ENTER without typing anything, the x and y values are set to zero by default. To backspace and erase the last character, press the key as usual.

The x coordinate must be between 0 and 127. The y coordinate must be between 0(bottom of the screen) and 47 (top of the screen)--the y coordinate is inverted from the standard Level II use by subtracting the requested y coordinate from 47.

The computer constantly checks to see if the value you typed has an x or y coordinate which is too high or too low. If either the x or y coordinate is too high or too low, the last digit entered is rejected; you aren't allowed to type an illegal coordinate. If you press a key which isn't used for coordinate entry, it is ignored. I have tried to make this program foolproof.

After all the coordinates have been stored, the vectors are drawn. There is a delay of a few seconds before the first vector is drawn.

To quit using Vector Plotter, press the BREAK key.

Table: Figure 1. Examples of Legal IF-THEN-ELSE statements

Table: Figure 2. Examples of IF-THEN-ELSE Statements Which Don't Work as Desired

Table: Figure 3. Variables Used in Vector Plotter

Table: First sample run of Vector Plotter.

Table: Listing 1. Demonstration of the Two Problems with the VAL Function.

Table: Listing 2. Extra Lines Needed to Make the Program in Listing 1 Work as Desired.

Table: Listing 3. Vector Plotter.

Table: Second sample run of Vector Plotter.