lab 5: area of an n-sided polygon
EN47/COMP9, Fall 2009
Out: October 22, 3:00pm
Due: October 29, 3:00pm
the problem
In this lab, you will write a program that computes the area of a
variable-sided polygon and animates the algorithm. The user will specify the
polygon by entering the x- and y-coordinates for each vertex in clockwise
order. As the number of vertices can change every time
you run your program, you should use arrays to store your coordinate data.
The user interaction with your program should look something like below.
This program will compute the area of a polygonal garden.
How many vertices does your polygon have (20 or fewer)? 5
Enter the x and y coordinates for each vertex:
1 2
10 15
15 8
14 1
5 3
The X coordinates are: 1 10 15 14 5
The Y coordinates are: 2 15 8 1 3
The area of the polygon is: 101.5
visualization
You will be using the LEDA
library to animate the polygon area algorithm. For every line segment on the
polygon, you should draw the corresponding trapezoid and pause your program for
one second. You should write a drawing function with the following function
prototype:
void draw_polygon(int x[], int y[], int numVerts):
This function takes three parameters that represent an array of
x-coordinates, an array of y-coordinates, and the number of
vertices.
We are providing some ready-to-compile code, including five functions that
you can use to interact with LEDA:
void window_open(): Call this function once
at the beginning of your program to open a LEDA visualization window.
void window_close(): Call this function once
at the end of your program to close the LEDA window.
void window_clear(): This function erases the drawings in a
LEDA window. You might want to use this for the extra credit.
void wait(int seconds): This function pauses your program so
that you will have time to admire your LEDA drawings.
void draw_trapezoid(int xi, int yi, int xf, int yf): Use this
function to draw a trapezoid in the LEDA window. The parameters are the
endpoints of a line segment that defines the trapezoid (as in previous labs).
If the height (xf-xi) is positive, the trapezoid will be colored red. If the
height is negative, the trapezoid will be white.
the algorithm
Here is one possible approach to this problem. You are welcome to use
this as a guide in developing your solution.
- Declare local variables and constants. Declare necessary arrays
- Open the visualization window
- Print directions for the user
- Read in the number of vertices for the polygon
- Read in the coordinates of each vertex
- Calculate and visualize the area of the polygon
- Print the X and Y coordinates, and the area of the garden
- Close the visualization window
After reading the problem description and sample algorithm, work out your
approach on paper first.
the program
getting started
- When you are finally ready to start writing code, create a directory for
today's lab (
lab5) and a cd into it.
- You will need to copy several files into your
lab5
directory using this command:
cp /comp/9/public_html/lab/lab5/* .
This will copy four files into your directory: poly.cpp,
visualize.cpp, lab5_sol,
and Makefile. (These files can also be downloaded from the website.)
- For LEDA to be loaded correctly, run the following instructions once at the
command line:
use leda
- Open
poly.cpp. This will be the file that you edit and hand
in. Right now it contains a simple template (skeleton code).
- Before making any changes, try compiling the skeleton code. Type
make at the command line. It is as simple as that! This creates an
executable program named poly in your current directory. Run the
program by typing ./poly at the command line. A window should pop
up containing a red trapezoid.
- Experiment with a few changes in
poly.cpp to
understand the visualization functions. After the wait()
function call, try adding another draw_trapezoid() call
followed by another wait(). This time, make sure that xf
is smaller than xi. What is the difference in animation when compared
to trapezoid drawing with xf greater than xi? Try adding another one
or two calls to draw_trapezoid() to draw a triangle,
quadrilateral, or polygon. Remember to compile each time after making
changes!
- Now you are ready to tackle the programming assignment!
testing & handing in
Test your code with a variety of different polygons. You can run the
solution program (lab5_sol) with the same input to test your
program. Your results should match lab5_sol in every case. When
you are satisfied with your program, submit it using provide:
provide comp9 lab5 poly.cpp
extra credit
- For 10% extra credit, modify your program so that it can read in any number of polygons, keeping
track of the running average. It should start by prompting the user for the
number of polygons to read in.
- For an additional 10% of extra credit, modify your program so that the user
doesn't have to enter the number of vertices for each polygon. Instead,
continue to read in vertex coordinates until the user enters -1 for the x and
y-coordinates. For example:
This program will compute the area of a polygonal garden.
How many gardens? 1
Enter the x and y coordinates for each vertex:
1 2
10 15
15 8
14 1
5 3
-1 -1
The X coordinates are: 1 10 15 14 5
The Y coordinates are: 2 15 8 1 3
The area of the polygon is: 101.5
Make any extra credit changes in a copy of your code:
poly_extra.cpp. Compile this program with the command make
extra. This will automatically create an executable file named
poly_extra which you then run by typing ./poly_extra
at the command prompt.
When you are confident that your extra credit program
works correctly, submit it using provide:
provide comp9 lab5 poly_extra.cpp
|