Classic Computer Magazine Archive COMPUTE! ISSUE 38 / JULY 1983 / PAGE 233

STARS

George Trepal

This short graphics program draws stars — separate or concentric. It is designed for the TRS-80 Color Computer, but its simplicity makes it a candidate for conversion to any machine. You'll learn how to create many types of patterns and also some interesting tricks you can use with other Color Computer programs.

This routine for the TRS Color Computer draws star-like patterns. It's a no-frills program which is easy to convert to other computers.

The stars can have as many points and sides as you want. However, the resolution of the TV sets an upper limit of visibility at about 25. The points used to draw the stars are stored in arrays X and Y; since 25 is the upper limit, these arrays are DIMensioned to 25 in line 10. Lines 20 and 30 simply clear the screen and ask for the number of sides desired. After you've typed in the program, a good number to start with is 17.

Line 40 puts the Color Computer into its highest resolution mode.

Lines 50 to 90 use polar coordinates and the computer draws an imaginary circle. It then finds points that equally divide the circumference into N equal parts. N is the number of sides you input in line 30. The Color Computer is not able to plot points given in polar form, so they have to be converted to rectangular (also called Cartesian) coordinates. Each of the points is stored in the X and Y arrays. If you want to know more, you'll find this discussed in high school algebra books.

The 96s in line 70 are special instructions for the Color Computer. The highest resolution screen is 264 by 192 separate dots, called pixels. Since I like big pictures, I'm telling the computer to take up the whole screen when it draws its circle. A circle with a diameter of more than 192 would be too big for the screen. Half of 192 is 96. In other words, 96 is the image size and the radius of the circle. We'll get back to this in a minute.

Now we have all the coordinates stored in arrays, and the screen is still blank. Lines 100 to 130 draw lines between the dots. In line 120, the 128 and 96 refer to the point (128, 96) which is the center of the screen. That's where we want the center of our circle. All the other points in the arrays are in relation to the circle center.

Line 140 locks the computer in a loop so the program continues running, and the picture stays on the screen.

Now that we have a nice 14-line program that draws pretty pictures, here are a few suggestions for improvements.

Concentric Stars

Remember line 70 with its 96? Instead of 96, let's put in a variable R (for radius). Let's add a line: 15 R = 96. If you run the program, there is no change at all. Let's add another line: 135 R = R/2: GOTO 50. Now when the program is run, the machine draws the star as expected. Next it draws another star half as big inside the first star. Then it draws a star half as big as the second star inside the second star, and so on forever or until you press the BREAK key.

Of course, you need not divide R by 2. You could use 1.4 or any other number you like. You don't need to draw an infinite number of concentric stars either, if you set up a counter. A good counter could be made by adding these two lines.

35 INPUT "HOW MANY STARS" ; HM
95 C = C + 1 : IF C = HM THEN GOTO 140

Multiple Stars

So much for multiple concentric stars. Let's modify line 120. Remember that coordinates 128,96 are the center of the screen. They are also the center of the figure. If you change them, the location of the figure will change. If you duplicate line 120 (call it line 125) with the 96 and 128 reversed, the figure will appear twice on the screen. Maybe you'd like lots of little stars on the screen in different places. You could do this by making lots of duplicates of line 120 with different center coordinates. Or you could store the coordinates in a DATA statement.

To use a DATA statement, first change the 96s in line 120 to DY (for data Y coordinate) and change the 128s to DX. Add line 95 READ DX: READ DY and put your DATA statement wherever you want.

POKE For Speed And Sound

Do you want to make the program draw faster? If the program is running, press the BREAK key. Then carefully type POKE 65495,0, and press the ENTER key. The program will now run twice as fast. This POKE (sometimes called vitamin E, by Color Computer users) doubles the rate of the internal clock, but it will not work with all Color Computers, especially the early models. The drawback to this POKE is that sound routines no longer work, printers print garbage, modems don't work, and you can't CLOAD or CSAVE. To get the computer back to normal you can: (1) POKE 65494,0 and ENTER, (2) press the reset button, or (3) turn off the machine.

If you want to use the magic POKE and have sound with the graphics, the way to do it is POKE 65494,0: SOUND 1,1: POKE 65495,0.

Stars

10 DIM X(25), Y(25)
20 CLS
30 INPUT "NUMBER OF SIDES" ; N
40 PMODE 4, 1 : PCLS : SCREEN 1, 1
50 FOR I = 1 TO N
60 A = I * (3.14159 / (N / 2))
70 X = 96 * COS (A) : Y = 96 * SIN (A)
80 X (I) = X : Y (I) = Y
90 NEXT I
100 FOR J = 1 TO N
110 FOR K = J TO N
120 LINE (X (J) + 128, Y (J) + 96) - (X(K) + 128, Y (K) + 96), PSET
130 NEXT : NEXT
140 GOTO 140