CS21 Spring 2005
Homework 5
Due Sunday, Feb. 13, before 11:30pm
For some of these problems this week, you may want to use functions in the
math library.
#include < math.h >
Some prototypes for math library functions that you may find useful:
double sin(double x); // returns the sine of x, where x is given in radians
double cos(double x); // returns the cosine of x, where x is given in radians
double sqrt(double x); // return the sqrt of x, don't pass a negative value
There is also a constant defined for the value of pi, named M_PI
,
that you can use, and in case you forgot how to convert degrees to radians,
use the relation:
radians pi
------- = ---
degrees 180
Also, if you'd like, you can look at the man page for the sqrt, sin, cos,
etc. functions. Here is how to veiw the man page for the sin function:
% man sin
# hit the space bar to scroll down the displayed man page
Here
is more information about man pages (I'll talk about man later in
the semester too).
You can look at the header file for the math library, but unlike the
Roberts' library header files, it is pretty difficult to read, so I
wouldn't suggest doing so. However, if you are curious, it is here:
/usr/include/math.h
Please complete the following four programs and turn them in. There
is also an extra credit problem this week (only try it after completing
the first four problems).
- 7.1, Crossed Box Program
- Draw a 3D box. Write a program that prompts that user to
enter values for x, y, width and height, and draws a 3-D box with
its lower left hand front corner of the box at (x,y). You can decide
the perspective of your 3D view (it doesn't have to be exaclty like the
example below), but it cannot be a head-on veiw. For example,
the call to your function, Draw3DBox(2.0, 1.0, 3.0, 2.0), may result the
following being drawn:
- 7.3, Draw a Heart
- Pyramid of boxes program (like 7.2 but with a modification).
For this program, write a function, DrawPyramid, that takes the
number of levels of boxes in the pyramid
as input and prints out a pyramid of boxes, that just fit inside the graphics
window (each box in a pyramid with three levels is larger then each box in
a pyramid with 20 levels). Your program should prompt the
user to enter a number of levels between 2 and 20, and then call your
DrawAPyramid function to draw the pyramid. For example:
This program draws a pyramid of boxes in the graphics window
enter values for the number of levels between 20 and 2: 1
hey, 1 isn't between 2 and 20
enter values for the number of levels between 20 and 2: 21
hey, 21 isn't between 2 and 20
enter values for the number of levels between 20 and 2: 20
Press return to exit.
Draws:
If the user enters 5 instead:
This program draws a pyramid of boxes in the graphics window
enter values for the number of levels between 20 and 2: 5
Your program will draw:
Extra Credit Problem. Implement four functions, DrawStarOne,
DrawStarTwo, etc. that take as input an (x,y) coordinate
for the center of the star, and a value for the height of the star. Each
function then draws one of the 4 stars shown below, centered at (x,y) and with
the given height. Test each function by calling it with different x, y,
and height values to make sure it works for all cases. Then write a
program that makes a call to your DrawStar functions to draw the 4
different stars to non-overlapping locations in the Graphics Window.