Thursday, 7 May 2009

sine in action - part 5

Making adjustments

When we first plotted our sine function we added ½ the screen height to the value of the function. This is because the sine function oscillates above and below zero, and to plot the entire function on screen we need to adjust the values so that it is centred.

Instead of merely adding a constant to the sin function, we could make more complex adjustments to vary the result.

This is the reason we have chosen to factor out the sine function as plotSineWave() and the adjustment() function as separate functions making it easier to change or entirely swap one or the other. In the final version of the program we have bundled several different adjustment functions into one, and the function to use can be set in the initial options. This is similar to the way we set up the choice of colours. It is designed to make it simple to change the program, as we merely have to set one variable instead of rewriting an entire section of our code.

There are three options for the choice of adjustment to make. The choice is set by the variable adjustmentChoice. If a valid adjustment choice isn’t made then the adjustment function returns 0, and the sine function is plotted without any modification.

The first choice is the one we have already discussed, that of a constant added to our sine function. The second choice of adjustment is based on a linear function. As we saw in section 24 the general form of a linear function is:

g(x) = intersect + gradient * x

In the function the variable intersect is where the line intersects the y axis and the gradient is the gradient of the line.

Our second adjustment function is based on this form of equation; however our gradient is varied based on the frame count.

The variable count takes the values 1 and -1. Each time the modulus of the frameCount reaches our variable range, count is set to the opposite value. This is done with the following code in the draw() method.

if ( frameCount % range == 0){
count = -count;
}


The variable count is tested in our adjustment function to determine whether we should be increasing or decreasing the gradient. In either case the gradient is varied from the minimum and the maximum each time. In our example the minimum is 0 and the maximum is 1, since we have set range to be equal to the image height. If range was set larger or smaller then the extreme values of the gradient would also be changed.

With the colour type set to greyscale this adjustment function creates an image similar to that in Figure 8.

Figure 8 greyscale sine function with linear adjustment function

Our third choice is somewhat more complex and the end result looks less like our simple sine function.
Option three combines a linear function of the form g(x) above with a sine function. The linear function has a variable gradient although this time the variance in the gradient is based on a sine function with a period equal to the width of our image. This means that the gradient varies smoothly between plus and minus the amplitude of this sine function (In our example we have set this to be 0.25).

This time we have based the value of the intersect variable on our variable count. If count is positive then we increase the intersect until it reaches the value of our variable range. At this point count will become negative, and we decrease the intersect until it reaches the minimum (in our example this is zero, but if range is greater than the height of the image then this could be below zero). In this way the entire sine wave is moved up and down the screen.


The final part of this function includes slightly more variation, by adding another sine wave to the result. This sine wave has a low amplitude and fairly high frequency so it contributes a fairly subtle variance to the overall function and causes it to move in a more interesting manner.


The impact of making these adjustments can be seen when they are added to a constant value. This is shown in Figure 9.


Figure 9 adjustment function three plotted with a constant

Final Program


Putting all of these functions together gives us our final program included below. The result of which can be seen in Figure 10
Figure 10 coloured sine function with adjustment function three

code for the final program can be found in the earlier post below.


1 comment:

Sharon Dawes said...

I loved the finished program Rebecca - stunning. Great illustrations in the chunk too.