CS 1112 Project 3, part A due Wed, Mar. 24, 11pm EDT

(Part B will appear in a separate document. Both parts have the same submission deadline.)

You must work either on your own or with one partner. If you work with a partner you must first register as a group in CMS and then submit your work as a group. Adhere to the Code of Academic Integrity. For a group, “you” below refers to “your group.” You may discuss background issues and general strategies with others and seek help from the course staff, but the work that you submit must be your own. In particular, you may discuss general ideas with others but you may not work out the detailed solutions with others. It is not OK for you to see or hear another student’s code and it is certainly not OK to copy code from another person or from published/Internet sources. If you feel that you cannot complete the assignment on you own, seek help from the course staff.

Objectives

Completing this project will solidify your understanding of user-defined functions and vectors. Part A focuses on user-defined functions and graphics; part B additionally involves vectors.

Ground rule

As usual, use only the functions and constructs learned so far in the course.

Lunar phases

Visualize the phases of the Moon for each day of the lunar month, implementing the functions described bellow using the inequalities from P5.3.8 in Insight. Be sure to read Insight §5.3 first—it’ll help you with this problem!

Lunar phases

Rectangles without borders

In lecture we introduced you to the function DrawRect(), which we presented as a “black box”—a tool that performed a well-defined task, even though we didn’t explain how it worked. Then on Project 2 you made use of another function named FillRect(), which performed a similar task but did not draw a border around the rectangle.

Compare the implementations of these two functions, focusing on the invocation of Matlab’s built-in fill(). By passing in an extra pair of arguments—'LineStyle' and 'none'—we instructed Matlab to skip drawing the border. LineStyle is one of many properties of graphics objects in Matlab. In addition to 'none' used in FillRect(), in Project 1 you experimented with other LineStyle property values, including ':' for a dotted line. There is no need to memorize such graphics options; look them up in the Matlab documentation whenever you need them! For example, type doc fill in the the Command Window to learn how to control the properties used by function fill().

For this problem you will be using the function FillRect() again, which you should download from the course website as FillRect.m and save in the same folder as the other .m files you will create.

PaintMoon, modified

Read the original problem statement of P5.3.8 (Insight p.126). You will implement a modified version of function paintMoon() that has five parameters instead of four:

function paintMoon(hc,vc,r,T,n)
% Draws on a black background the moon as it appears T days after the new moon.
% T must be >= 0 and < 28
% T = 0 corresponds to the new moon.
% T = 14 corresponds to the full moon.
% The moon is centered at (hc, vc), has radius r, and is painted by
% stacking rectangles of thickness r/n, where n is a positive integer.
% In other words, n is the number of intervals into which the radius is divided.

[When writing Matlab function files, include the entire function comment (as above) in your file. A function comment must be complete—fully specifying the function and explaining the function parameters.]

Use the given FillRect() function. Depending on your algorithm, you may also use the given DrawDisk() function.

Because graphics commands have side effects, we need to be careful about which ones we use in our functions. Remember that functions facilitate code reuse, so they may called from contexts different than what we anticipate when we first write them. For example, we know paintMoon() needs to ensure that hold on is active (since the image is composed of many stacked rectangles), but we shouldn't necessarily run hold off when our function finishes. What if the caller already ran hold on? They probably expect it to stay on. To accommodate this, your code should use Matlab’s ishold() function to determine whether hold was on when paintMoon() was called and to restore that state before it returns.

In a similar vein, since our paintMoon() might be adding to an existing figure, it should not contain graphics setup commands such as close all, figure, or even axis equal off. Let the caller take responsibility for setting up the figure; our function should just focus on drawing a moon.

Test paintMoon() to make sure that it works as specified. You may invoke your function directly from the Command Window, but an even better approach is to write a test script. Your test script (which is not included in your submission) can take care of setting up the figure. During testing, you may want to reveal the axes by changing its graphics setup from axis equal off to axis equal. Be sure that the function draws the moon at the correct location before moving on to the next section.

Phases of the Moon

To show the phases of the Moon, write a function moonPhases() (instead of the script specified in the textbook). Function moonPhases() has only one parameter, n, which is the number of intervals into which the radius of the moon is divided (for the purpose of painting a moon). Arrange the moons in four rows, as shown in the figure above.

Function moonPhases() is responsible for setting up the figure, so it should contain these graphics commands: close all, figure, hold on, axis equal off, and hold off. Use the unit radius (radius equals to 1) for the moon. Be sure to write a concise function comment for moonPhases(). Test moonPhases() by invoking it from the Command Window for several choices of n. How large does n have to be before your moons look relatively smooth? Write your answer (in a complete sentence) as a comment at the bottom of moonPhases.m.

Submission

Submit your files on CMS (after registering your group). They should include paintMoon.m, moonPhases.m, and the files from part B.