Programming a Graphical Design Tool

**Overview**:

This assignment provides an opportunity
to work with computer graphics by programming a simple user interface
that interacts with a user who is an apprentice graphical
designer. It also provides a richer example of how functions
can be used to divide a rather large programming problem into a series
of smaller ones. The readings and exercises for this assignment
are taken from section 7.8 of your text.

The following functions are available for working with graphics in a C++ program:

clearscreen(0); // clears the graphics screenDrag a copy of the Invitation folder from the csci107 server to the desktop, and then close the csci107 folder. Double-clock the icon

setcolor(x); // sets the drawing color to black (x=1) or white(x=0)

moveto(x, y); // moves the cursor to pixel (x, y) on the screen

lineto(x, y); // draws a line from the current cursor location to (x, y)

rectangle(x1, y1, x2, y2); // draws a rectangle with upper left corner at (x1, y1)

//and lower right corner at (x2, y2)

circle(x, y, r); // draws a circle centered at (x, y) and radius r pixels.

writedraw(text, x, y); // displays the message "text" beginning at location (x, y)

getmouse(x, y); // variables x and y get the (x, y) position of a mouse click

Now select **Execute** in the Compiler menu, and then select the
**Run** button at the bottom -- you will be prompted to click the
mouse, as shown below.

When you do that, a face will appear on the screen, centered at the x-y coordinates of your mouse click.

1. What statement in the program captures the x-y coordinates of your mouse click?

2. How many faces can be drawn during one run of this program?

3. How could the program be changed so that any number of faces can be drawn in this way?

4. Notice that the function "drawFace" in this program draws a face on the screen. Change this function so that it draws a "blockhead" - that is, the head should be square rather than circular.

5. There are three "circle" function calls inside this function. What does each one accomplish?

6. Change this function so that the face will have a small circular nose and a short horizontal line for a mouth, both positioned appropriately inside the face.

You may wish to review the discussion of C++ graphics in section 7.8 before answering the questions in this lab. Additional examples of simple C++ programs with that use graphics are called

In this lab, you will complete the programming of a primitive design tool, so that an architect can design a house like the one on page 356 of your text. A skeleton of this program is provided in the file

A mouse click in one of these boxes triggers a prompt to assist the user to draw the indicated figure. For example, clicking "scribble" gives the following prompt.

If the user (e.g., the architect) follows this instruction, she can draw a many-sided figure on the screen. Try it!

You will also need to implement a square root function. (Normally
this function is included in the math library, but for some reason it
does not work in Invitation..). Computing the square root of an
arbitrary number `x`

can be done using Newton's
method. This method iteratively refines an approximation
`y`

for the square root of `x`

. It starts from
an arbitrary value `y`

and repeatedly updates
`y`

until the error between `y*y`

and
`x`

is arbitrarily small. Newton gave a formula to update
`y`

to insure that `y*y`

gradually converges to
`x`

. More precisely:

let eps be the desired error for approximating square root of x y=1 while |y*y-x| < eps y = (y*y+x)/(2*y) //at this point y is an approximation for square root of x with error epsUsing this method, write a function to compute the square root:

double squareroot(double x, double eps)

Try calling this function with various values for x and eps and check that the results are correct!

Working in the following teams, you need to complete the
graphics7.cpp program.

Team 1: Astrid, Will*, Sam

Team 2: Catoria, Bryan*, Chris Sullivan

Team 3: Dan, Chris Field*

Team 4: Kevin*, Charles

Team 5: Adam, Sonia*, Tom

Team 6: Connor, Hai*

(* indicates the team captain.)

You should divide the work in this project so that each team member
individually writes and tests one or two of the functions in this
program that are not completed. The team captain will be
responsible for merging the work together, making sure that the entire
program works correctly, and submitting the completed program to the
Drop Box. The final program should allow an architect to design
houses like the one shown on page 356 of your text as a series of
lines, rectangles, circles, etc.

**Once you are finished in the lab, be sure to drag the CS107 icon to
the Trash - this step disconnects you from the server and prevents
someone else (who may use this iMac later in the day) from
accidentally accessing files in your personal folder. **