Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 10, NO. 2 / FEBRUARY 1984 / PAGE 165

Spectrum, a colorful look at math functions. Christopher J. Shaw.

The idea behind Spectrum, the program presented in this article, is quite simple:

Compute the value of a numeric function of two variables, x and y. Use the values of x and y to determine a spot on the screen. Use the function value to designate a color. Display the color at the proper spot. Do this for each spot on the screen.

Spectrum is written in Microsoft Basic and incorporates this algorithm. It also allows you to select any of 25 functions to plot, and you can easily modify it to incorporate your own functions.

Spectrum also allows you to select the color palette to be used--up to the 16-color maximum permitted by the display hardware.

I wrote Spectrum for the Commodore 64 computer, and I have modified it to run on the Vic 20 and on the IBM PC. The Commodore 64 version is given in Listing 1. The changes needed for other machines are shown in Listings 2 and 3. (Spectrum won't quite fit into an unexpanded Vic 20, unless you leave out all the REMARKS and unnecessary spaces. And even then you may have to delete one or two of the functions to give it enough room to run.)

Spectrum uses low-resolution color graphics. Each spot on the screen is a character position, and the color is generated by POKEing a reversed blank with the computed color as background.

High-resolution plots are also possible, of course, but the color palette is more limited (for vs. sixteen colors), so the patterns, though more detailed, are less colorful. It also takes 32 times as long to plot the color spectrum of a function in hi-res, and Spectrum is slow enough as it is, particularly for difficult functions.

Spectrum produces some fascinating color patterns, and it is fun--and curiously soothing--to watch them build up on the screen, eventually overwriting the function definition displayed at the top. But the main fascination of the program, I think, lies in making minor modifications to the functions being plotted, and seeing how these modifications affect the pattern displayed. To get an appreciation for this, it is necessary to understand how Spectrum converts a function value to a color number. Converting A Function Value

To do this, Spectrum first takes the integer part of the function value (using built-in Basic INT function). Then it divides this integer by the number of colors in the current palette and takes the remainder of this division. This remainder, added to the number of the first color in the palette, is the color number for the spot corresponding the values of x and y used in evaluating the function.

If we consider the function value as a binary number, with a binary point somewhere within it, and we assume a 16-color palette, the color number is determined by the four bits, just before the binary point, shown capitalized in the example below.

By redefining the function to include a power-of-two multiplier or divisor, we can, in effect, shift the four color-determining bits either left (by division) or right (by multiplication). This usually has a significant impact on the color pattern.

The most significant bits of the function value are often very regular (zero is typical), and the patterns they produce can be very broad--and dull, sometimes invisible. On the other hand, the least significant bits of the function value are typically fairly random--any patterns are hard to see within the relatively small window of the display screen--so the resulting patterns appear random and, though very colorful, also dull.

The challenge is to experiment with each function until you find a factor--and it doesn't need to be a power of two--that produces a pattern that is neither too regular nor too random.

Another way to change a pattern is to shift it left, right, up, or down. You can do this by adding a term to x or to y everywhere it appears in the function definition. You can also change the scaling of the pattern in the x or y direction by multiplying or dividing x or y every-place it appears in the function definition by your chosen scaling factor.

Pattern rotation is possible by applying the appropriate mathematical transformations to the function, but that may be more work than it is worth.

Once you have defined a function that generates a pattern you like, you can experiment with the colors. Spectrum allows you to choose a color palette by specifying the number of colors and the initial color.

After Spectrum displays its initial screen of instructions, you may press RETURN to proceed to the function prompt or space to go to the palette selection menu. A number in the range 3-27 is the proper response to the function prompt, but you may press q to quit the program or enter a number outside the range (e.g., 0) to return to the initial instruction screen and then the color palette menu. After Spectrum finishes plotting a function, you may press any key to be prompted for another function number.

The version of Spectrum given in Listing 1 contains 25 functions in statements 3 through 27. (I have numbered the functions 3 to 27, instead of 1 to 25, to emphasize this.) If you get involved in using the program, you may find that this is not enough. You can easily modify it to hold more functions, but it is even easier to make a separate copy of the program for each set of 25 functions you find interesting. Defining A New Function

A new function is defined by replacing any statement in the range 3 to 27 to compute the new function value. Be sure to change the corresponding print statement in the 403 to 427 range to display the new function.

The main thing to look out for in defining your own functions is to avoid division by zero (add a small, non-zero term to the divisor) and illegal quantities caused by out of range function arguments. Of course, the Basic interpreter will tell you about these errors, but sometimes not until you get to the middle of the screen where x and y are both zero.

Some of the functions included in the program are particularly interesting, as you can see from the illustrations accompanying this article.

Function 3, F = RND(0), generates a completely random pattern.

Function 4, F = TIME, shows a certain regularity, indicating that the time needed to execute the major display loop is fairly constant.

Function 16 displays an American flag (almost), the mathematical definition of which relies on the fact that Microsoft Basic treats logical expressions, such as (A = B), as zero when they are false.

Function 8 was displayed on the cover of the May 1982 issue of a magazine called Computer Graphics and Applications. A brief article about that cover, by E.P. Miles, Jr. of Florida State University, inspired the development of my program, Spectrum.

If you type the program into your computer, I hope you enjoy investigating the color spectrum of simple mathematical functions as much as I have. And if you find any really neat patterns, be sure to let me know about them.