CS-211 Fall 2015 Lab 10 Lists of Lists
Back in Lab 4, we coded a functions to determine if a point fell inside the bounding box of a polygon. In lab 5, we extended that code to determine if a points was actually inside a polygon. In lab 4 and lab 5, we treated a polygon as a list of x coordinates and a list of y coordinates. For this lab, we are going to treat a polygon as a list of floating point numbers. In that list, the first element will be the number of verteces in the polygon (e.g. 3.0 for a triange, 4.0 for a rectangle, and so on). The remaining floating point numbers will be alternating x and y coordinates - a pair for each vertex.
However, in this lab, we are going one step further. We are actually going to look at a list of polygons - so utlimately, we are looking at a list of lists.
After you have logged on to one of the Linux Lab machines, open a terminal window, download lab10.tar.gz and move it into you home directory. Use the command tar -xvzf lab10.tar.gz. This will unpack the tar file and make a new lab10 subdirectory. Make the lab10 directory your current directory, and list the files in the lab4 directory. There should be 5 files... one called Makefile, three program files called contains.c, poly.c and poly.h, and one example polygon files called shapes.txt
The Polygon and Polygon List Functions
The two C files called poly.h and poly.c contain the declarations and definitions for the functions to handle polygons and lists of polygons. Look over this code and make sure you get a feel for how things work in these files.
The poly.h file contains everything you need to invoke the polygon functions. Therefore, we #include "poly.h" in our main C file. The function definitions for these files is in the poly.c file. But the important thing is how these functions work.
The poly.h file contains the following definitions:
- A typedef for the polygon type. A polygon is defined as a pointer to one or more float numbers (or a list of floating point numbers.)
- The definition of a poly_list structure. This is a structure which contains an integer... the number of polygon shapes in the polygon list named the nshapes field, and an array of elements of type "polygon" (which is really a list of floating point numbers). Our array of polygons is "MAXSHAPES" large (where MAXSHAPES) is defined above as 100.
- A typedef for the polylist type, which consists of a pointer to an instance of the poly_list structure.
- Function declarations for the functions that deal with lists of polygons. These functions are as follows:
- polylist makePolyList() - This function allocates memory for a new list of polygons and returns a pointer to the instance of the poly_list structure it has created.
- polylist readPolyList(char *fromFile) - This function reads a file that contains desriptions of polygons (similar to the one we used in lab4... but now we support a list of polygons in a file rather than just one.) The input argument is a file name of a text file that contains the description of a list of polygons. The return value is a pointer to a new isntance of a polylist structure which contains the list of polygons read from the file.
- void deletePolyList(polylist this) - This function takes, as an argument, a pointer to an instance of the poly_list structure. The function then frees up all the memory associated with that instances (i.e. returns that memory to the operating system heap.)
- Finally, there is are function declarations for the functions that deal with individual polygons. These functions are very similar to those we used in lab4 and lab5, but the internals have changed a little because the way we represent polygons has changed. The following is the list of functions that deal with polygons:
- polygon makePolygon(FILE *pfile) - This function creates a new polygon by reading the file specified by the "pfile" argument. It reads the next polygon out of the file, and advances the file pointer past what it has read. If this function sucessfully reads a polygon, it returns a pointer to that polygon. If there is a problem reading the polygon, this function will issue an error message and exit the entire program. If there are no more polygons in the file, this function returns a NULL.
- void printPolygon(polygon p) - This function prints information about the polygon to the monitory.
- void deletePolygon(polygon p) - This function
- int verteces(polygon p) - This function returns the number of verteces associated with polygon "p".
- float vertexX(polygon p, int index) - This function returns the X coordinate of the index'th vertex of polygon p.
- float vertexY(polygon p, int index) - This function returns the Y coordinate of the index'th vertex of polygon p.
All of the definitions for all of the functions declared in poly.h are in the poly.c file.
The contains.c File
As in lab4 and lab, the main function for the command we will be building is included in the main.c file, along with a couple of extra functions, as follows:
- int contains(polygon p, float px, float py) - This function is the one we worked on in lab 4 and lab 5 - but I am including a correct version already written for you. This goes through the process of looking at a specific polygon, as described by the "p" parameter, and a point as described by the "px" and "py" parameters, and figuring out if the point is inside the polygon. This function is quite verbose - writes a lot of messages about what it is doing - but eventually, it returns a "1" if the point is inside the perimeter of the polygon, and a "0" if the point is outside the perimeter.
- float interpolate(float x1, float y1, float x2, float y2, float xq) - As in lab5, this function determines the y value associated with the intersection of a vertical line at "xq" with the line segment described by (x1,y1) and (x2,y2).
- int main(int argc, char **argv> - The main function reads the first parameter - the name of the polygon shapes file, and a second and third parameter - the specification of the (x,y) coordinates of a single point. Currently, main then checks to see if the point is contained in the first shape in the polylist that has been read. Your job is to make main check through all of the polygons in the polylist. There is code to do this already in the main function, but it is commented out. Your job is to uncomment this code and get it working correctly.
Using The Makefile
The Makefile file in the lab10 directory contains the recipes to build the C program associated with whose "main" function is in contains.c. To build the contains executable, use the make contains.
The Makefile file contains several other "targets" which allow you to do other things using the make command. The following targets are supported:
- make contains - Makes the contains executable if you have changed the source code.
- make test - Makes the contains executable if you have changed the source code, and then runs that executable on test case, shapes.txt.
- make clean - Removes the contains executable.
Hints on Making Everything Work
Notice that if you uncomment the code, it calls a new function that has not yet been defined called getPolyList. The real job in this lab is to write the getPolyList function. Clearly, based on how it is used, this function is going to take a variable called "pl" as its first argument. "pl" is a polylist variable - a pointer to an instance of the poly_list structure... in fact, the one that makePolyList has created for you. The second argument is just an integer - the index of the polygon you want to extract from the polylist variable. The return value is the polygon itself.
You may decide how to write this function, and where to put it, but I would recommend putting this in the poly.c (and poly.h) files to be consistent with the rest of the polylist functions.
Download and edit the following file: lab10_report.txt. Then submit your editted file on Blackboard in the Lab_10 report area.