Decision-making in C (Possibly) Intersecting Rectangles Background A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of one corner and the height and width; for example: So, we may consider the SW corner, height and width to form a set of defining attributes of such a rectangle. Given that much information about two such rectangles, it is possible to compute he attributes of their intersection, if any. Assignment For part of this assignment, you will use very basic C techniques to implement a C function to determine whether two rectangles (as described above) do intersect, and if so specify the intersection by giving the lower-left corner, the height and the width. For our purposes, consider each rectangle to be closed; i.e., the points on the border of the rectangle are considered to be part of the rectangle. So, it's possible for two rectangles to intersect in many ways, from a single point up to being entirely identical. You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment: / Determines whether two rectangles, A and B, intersect, and writes an appropriate message to the supplied output stream. Pre: Log is open on a file aswx and aswy specify the SW (lower, left) corner of A aheight specifies the vertical dimension of A awidth specifies the horizontal dimension of A bswx and bswy specify the SW (lower, left) corner of B bheight specifies the vertical dimension of B bwidth specifies the horizontal dimension of B / void Intersection(FILE Log, int32_t aswx, int32_t aswy, uint32_t aheight, uint32_t awidth, int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth); Note that it's perfectly valid for some, or all, of the coordinates to be negative. It's also perfectly valid for the height and/or width of the intersection to be zero (but not negative). To be more specific, you will finish the implementation of the short C program that is supplied later in this specification (and on the course website). The supplied program provides some code and comments indicating how it can be completed. This is a purely individual assignment! 1
The completed program will read test data from an input file named TestData.txt. Here is a sample file: A B SWx SWy Height Width SWx SWy Height Width 5 10 5 15 10 5 5 5 4 4 4 4 10 5 5 5 9 2 8 2 8 4 4 4 5 2 8 5 8 4 4 4 5 5 5 5 5 5 5 5 The file begins with a two-line header describing the contents. The header is followed by one or more lines of test data; each line contains eight integer values, specifying the SW corners of two rectangles, along with their heights and widths, in the order indicated by the header lines. The upper limit on the number of lines of test data is unspecified, so the program that reads the file must be designed so that it works correctly by detecting the end of the input data. The program will write results to a file named Results.txt. Here is a sample output file that corresponds to the input file shown above: Rectangle A Rectangle B SW corner Height Width SW corner Height Width ------------------------------------------------------------------- ( 5, 10) 5 15 ( 10, 5) 5 5 ( 10, 10) 0 5 ( 4, 4) 4 4 ( 10, 5) 5 5 no intersection ( 9, 2) 8 2 ( 8, 4) 4 4 ( 9, 4) 4 2 ( 5, 2) 8 5 ( 8, 4) 4 4 ( 8, 4) 4 2 ( 5, 5) 5 5 ( 5, 5) 5 5 ( 5, 5) 5 5 ------------------------------------------------------------------- The file begins with two lines describing the contents of the file and a line of hyphens marking the beginning of the results table. For each test case there are three lines of output. The first line contains the coordinates of the SW corner, height and width of each rectangle; the second line contains the coordinates of the SW corner, the height and the width of the rectangular intersection (or the string "no intersection"). The third line is blank. You should assume that the x and y coordinates will be restricted to the range -9999 to 9999 and format your output exactly as shown above. You should test your solution thoroughly; there is no guarantee that the supplied test data covers all cases. You may assume that the supplied data will be logically correct. You should not make any other assumptions about the test data values. Be warned: this problem may be harder than it appears. You should think carefully about your analysis of the possible relationships the two rectangles could have in the plane. That does not mean that a solution needs to be complicated. Mine involves a total of four C functions and about 40 lines of code, not counting main(). You should consider writing helper functions, even for a short program like this; they clarify your logic. Make useful comments in your implementation of Intersection() and any helper functions. The same general guidelines for commenting that you have been taught in your Java courses should provide sufficient guidance. This is a purely individual assignment! 2
What to turn in: You will submit a Linux tar file containing your completed driver.c, Intersection.c, and Intersection.h, making changes where indicated in the supplied code. Your submission will be compiled, tested and graded according to the formatting of your output and how many cases your solution handles correctly. You will be allowed up to ten submissions for this assignment. Use them wisely. Test your program thoroughly before submitting it. Make sure that your program produces correct results for every test case you can think of. If you do not get a perfect score, analyze the problem carefully and test your fix with the test data shown in the Curator grade report, before submitting again. The highest score you achieve will be counted. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: Pledge: http:www.cs.vt.edu/curator/ Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file: On my honor: - I have not discussed the C language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C language code or documentation used in my program was obtained from an authorized source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. - I have not designed this program in such a way as to defeat or interfere with the normal operation of the Curator System. <Student Name> Failure to include this pledge in a submission is a violation of the Honor Code. This is a purely individual assignment! 3
Suggestions for implementing and testing your solution: There are two different foci for this assignment. You must deal with implementing the input and output operations, and you must deal with implementing the correct logic to determine whether two rectangles overlap, and with determining the correct attributes of the intersection when they do. I'd begin with the first issue. In the posted version of the supplied code, all the lines that you must complete have been commented so that the code will compile (albeit with warnings if you use Wall). Complete the code to open the input and output files (1, 2), and to close the input and output files (10). Note: you'll need to download one of the data files from the website, and save it to the same directory as your source code, and make sure it has the right name (TestData.txt). Next, complete the code to read a line of data (4, 8) into the appropriate variables. At this point, I'd add a printf() statement just at the beginning of the while loop body and write out the values that were placed into those variables; that way I can easily see whether I've got the input code right. You'll also have to complete the test for the while loop (5), although this may seem to work properly as it is given. Until you can correctly acquire the test data from the input file, there's not much point in worrying about the logic for the Intersection() function. Once I'm sure my input code is correct, I'd remove that printf() statement (or comment it out). Next, I'd focus on the implementation of Intersection(). You may find this is harder than you expect, because there's a seductive, inadequate way of thinking about this. Since you've got access to test data files and the correct results for those tests, it should be easy to determine whether you've got this right or not. In order to actually test this, you'll want to complete the call to the function (7), as well as the function itself. Once I've got Intersection() tested, I'd complete the code to write the header lines (3), and the rectangle data (6), and marker for the end of the output table (9) to the results file. Use the sample results files to determine how to format your output. This is a purely individual assignment! 4
Supplied code: The following code is supplied on the course website. You should use this file as your starting point. Feel free to modify the design used here, but your solution must conform to the requirements stated above, and the Intersection() function must retain the specified interface. You are expected to provide useful comments in the body of the function Intersection() and any helper functions you incorporate into your solution. The supplied code will compile, with warnings: CentOS code> gcc -o driver -std=c99 -Wall driver.c Intersection.c driver.c: In function main : driver.c:57: warning: statement with no effect driver.c:33: warning: unused variable bwidth driver.c:33: warning: unused variable bheight driver.c:33: warning: unused variable bswy driver.c:33: warning: unused variable bswx driver.c:32: warning: unused variable awidth driver.c:32: warning: unused variable aheight driver.c:32: warning: unused variable aswy driver.c:32: warning: unused variable aswx driver.c:25: warning: unused variable Log driver.c:23: warning: unused variable datafname driver.c:22: warning: unused variable logfname driver.c:29: warning: Data is used uninitialized in this function driver.c:46: warning: numread is used uninitialized in this function (The warnings above were issued by gcc version 4.8.3; a different version may issue somewhat different warnings.) But executing it will produce a segfault until you have completed certain parts of it. driver.c: / CS 2505 Summer 2015: driver.c Supplied driver for Intersecting Rectangles project. Your task is to complete the supplied code to satisfy the posted specification for this assignment. The occurrences of "???" in the code below indicate places where you must complete the implementation. Student: <ENTER YOUR NAME HERE> PID: <ENTER YOUR VT EMAIL PID HERE> / #include <stdio.h> for I/O functions #include <stdlib.h> generally useful #include <stdbool.h> for the bool type #include <stdint.h> for exact-width integer types #include <inttypes.h> for exact-width integer output #include "Intersection.h" for declaration of Intersection() #define MAXLEN 100 maximum number of chars in an input line int main() { char logfname[] = "Results.txt"; char datafname[] = "TestData.txt"; FILE Log / =??? /; FILE Data / =??? /; 1: open output file 2: open input file This is a purely individual assignment! 5
char Line[MAXLEN + 1]; fgets(line, MAXLEN + 1, Data); fgets(line, MAXLEN + 1, Data); read header lines int32_t aswx, aswy, aheight, awidth, specification of 1st rectangle bswx, bswy, bheight, bwidth; specification of 2nd rectangle 3: Write output header: 4: Read data for first two rectangles; return value is how many values were actually assigned to variables: int32_t numread / = fscanf(data,???) /; 5: The logic here is that as long as we've read the expected number of values, we will process what we've just read, and try to read more data. while ( numread / ==??? / ) { 6: Write data for current rectangles to log (one or two statements): 7: Compute intersection, if any, and log the results: / Need an appropriate function call here. / } 8: Try to read data for next pair of rectangles: numread / = fscanf(data,???) /; 9: Write the marker for the end of the results table: 10: Close the input and output files: /??? / } Exit the program: return 0; Intersection.h: #ifndef INTERSECTION_H #define INTERSECTION_H #include <stdio.h> #include <stdint.h> for I/O functions for exact-width integer types void Intersection(FILE Log, int32_t aswx, int32_t aswy, uint32_t aheight, uint32_t awidth, int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth); #endif This is a purely individual assignment! 6
Intersection.c: / CS 2505 Summer 2015: Intersection.c Supplied framework for Intersecting Rectangles project. Your task is to complete the supplied code to satisfy the posted specification for this assignment. Student: <ENTER YOUR NAME HERE> PID: <ENTER YOUR VT EMAIL PID HERE> / #include "Intersection.h" #include <stdlib.h> #include <inttypes.h> generally useful for exact-width integer output Declarations for helper functions should go here: / Determines whether two rectangles, A and B, intersect, and writes an appropriate message to the supplied output stream. Pre: Log is open on a file aswx and aswy specify the SW (lower, left) corner of A aheight specifies the vertical dimension of A awidth specifies the horizontal dimension of A bswx and bswy specify the SW (lower, left) corner of B bheight specifies the vertical dimension of B bwidth specifies the horizontal dimension of B Returns: true if A and B share at least one point; false otherwise / void Intersection(FILE Log, int32_t aswx, int32_t aswy, uint32_t aheight, uint32_t awidth, int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth) { } Your implementation goes here: Implementations of helper functions should go here: This is a purely individual assignment! 7