Pattern Manipulation

You see them all the time, mostly as “busy” gizmos. They are animations that spin or dance to deceive you into believing that the software is really doing something. But they’re merely animations. In fact, they’re very simple animations that require practically effortless coding.

Consider the animation shown in Figure 1. It cycles through some basic shades of red. Your eye interprets the changing colors as movement, but nothing is really moving.

Figure 1. A simple color animation.

Figure 1. A simple color animation.

The image consists of 8 color panels. Each panel is assigned a color value. That value is manipulated to create the animation. So the panels don’t move or change location, only their color value is manipulated. The animated process is done by using simple math.

In Figure 2, you see a more basic example, which is actually output from this month’s Exercise:

Figure 2. Output from this month's Exercise.

Figure 2. Output from this month’s Exercise (animation speed reduced for readability).

Your task is to write code that cycles through values 0 through 7 in order, changing each position by 1, over and over. Yep: That’s an infinite loop. So you’ll need to press Ctrl+C to end the program, but in this instance, creating an infinite loop is desirable.

While many ways exist to accomplish this task, your directions are to use only one variable for all the numbers. A second variable can be added if you need a looping statement, although I’m only being kind by making that concession.

Click here to review my solutions. I have two: one that uses two variables and another that uses only a single variable. Please try the exercise on your own before you peek at my solutions.