CSc 4630/6630 Programming Assignment #3

Documentation
The first thing in your program should be documentation, such as the following. This should appear at the very top of your program.
  % program(number).m
  %
  % Author: (Your Name)
  % Account: (your account name, or student e-mail)
  % CSc 4630/6630 Program (this program number)
  %
  % Due date: (put the due date here)
  %
  % Description:
  % (Give a brief description of what your program does.)
  %
  % Input:
  % (State what the program inputs are.)
  %
  % Output:
  % (State what the program outputs are.)
  %
  % Usage:
  % (Give an example of how to use your program.)
  % (For example: out = myabs(in); )
  %
Verify that the help command shows this information.

Objectives

  1. Practice designing a program iteratively.
  2. Practice implementing a GUI and callback functions in MATLAB.
  3. Practice creating an image with an alpha component.

Parallax is where distance is approximated by having far-away things go by slowly, and close-by things go by quickly. For a side-scrolling game, we need a background that looks natural, one that is supposed to represent mountains in the distance. The mountains do not need to be detailed; we just need the shapes, and since they are so far away, we only need one color to represent them. One way to create such a background is to use several sinusoids, and add them together. A sinusoid has the pattern

Amplitude * sin(2*pi*Frequency*Time + phase).

By varying the amplitudes, phases, and frequencies, we can get a wide range of natural looking back drops. Let Time vary from 0 to 1, with a very tiny increment.

In this assignment, you will create a GUI tool to help make such a backdrop. To get started, you can use "guide" in MATLAB. Your interface should have an area for plot, a slider for each amplitude, a slider for each frequency, and this slider for each phase. Include at least three sinusoids. Include text labeling each input, and also show the numeric value for each of the sliders. Finally, include a button labeled "create". When the user presses this button, your program should create an image, show it in a new figure, and save it under the name "mountains.png". The alpha values should be either 0 (no mountain) or 255 (nothing can be seen through the mountain). RGB values for the mountain can be 46, 8, 52, respectively, or you can whatever values you like. The image should be 480x6400, based on a screen size of 480x640, with 10 screen width's worth of mountains.

This command writes a file called "myfile.png", where the RGB data is in "myimage", and the alpha data is in "myalpha":

    imwrite(myimage, 'myfile.png', 'PNG', 'Alpha', myalpha);


When one of the slider values changes, your program should find the sum of the sinusoid, and re-display the plot of it.

Backdrop image should have a clear area on top, where the alpha values are zero. At the curve, the alpha values should be 255, and any pixel under that should also be 255. The curve corresponds to the sum of the sinusoids, with an image height of 480. It does not look good to have the sinusoid very from the bottom of the image to the top, so instead it should very from 100 units from the top to 200 units from the top. The sinusoid should not appear to very too quickly, but we will leave this up to the user to get right. Amplitudes should range from 0.0 to 1.0, frequencies should range from 0 to 100 (use a default around 10), and the phase angles should vary from -pi/2 to +pi/2.

If you use guide, you can export the figure as a .m file, and it will be populated with code. Make sure that you understand what this code does, since you will need to modify it to complete this assignment. You do not have to use guide for this assignment, though you are strongly encouraged to try it. Whether it is part of your final solution or not is up to you.

With the code generated by guide, there is a structure available to the callback function. The structure contains the handles of the other UI components. Code such as
    handles.text9.set('String', amp3);
can be used to change the text of one of the components, in this case, "text9". Figuring out which handle name to use may require some trial and error. You may want to have global variables. Also, you can place a breakpoint in a callback function (see chapter 11) to inspect the variables.

When the user changes a value, have the program output a line confirming what changed and what the new value is, such as "changed frequency 1 to 12". When the user creates the image, have your program report all of the values and what they are.


Turn In Your Work

  1. your source code (and .fig if you have one)
  2. an example output image (.png)
  3. a log of at least one run

Make sure to use plain text files (.txt, .csv, or .m, as appropriate).


* A script is short for typescript, where everything typed is saved. This keeps a log of all the input and output. MATLAB provides this capability with the diary command. For example, diary out records all inputs and outputs in a file named ``out''. Do NOT call your script file ``programX.m'', or it may overwrite your homework! Make sure to keep a backup of your work. Type help diary at the MATLAB prompt for more information.