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: Given that much information about two such rectangles, it is possible to determine whether they intersect. 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. 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 C function that satisfied the conditions stated in the header comment: / Determines whether two rectangles, A and B, intersect. Pre: 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 / 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. To be complete, 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. The supplied program will not compile. You must add code in the indicated places. This is a purely individual assignment! 1
The 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 -2994-5134 5 5-2991 -5135 4 4 7903-4382 6 4 7895-4384 5 5-2453 -6046 3 4-2461 -6042 5 5-8714 -2687 4 4-8716 -2689 8 8 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 and NE corners of two rectangles, 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 ------------------------------------------------------------------- (-2994, -5134) 5 5 (-2991, -5135) 4 4 overlap detected ( 7903, -4382) 6 4 ( 7895, -4384) 5 5 no overlap detected (-2453, -6046) 3 4 (-2461, -6042) 5 5 no overlap detected (-8714, -2687) 4 4 (-8716, -2689) 8 8 overlap detected ------------------------------------------------------------------- The file begins with two lines describing the contents of the file and a line of hyphens marking the beginning of the results table; those are followed by pairs of lines of data corresponding to the test cases; for each test case, the rectangle data is logged, formatted precisely as shown, followed (on a separate line) by the string "overlap detected" or "no overlap detected", as appropriate. 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 6 C language statements, which could be reduced to a single statement, if I were willing to sacrifice clarity and readability. Make useful comments in your implementation of Overlap(). The same general guidelines for commenting that you have been taught in your Java courses should provide sufficient guidance. You will submit your completed.c file, 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. This is a purely individual assignment! 2
The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: http:www.cs.vt.edu/curator/ 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 Overlap() function must retain the specified interface. You are expected to provide useful comments in the body of the function Overlap(). / CS 2505 Spring 2014 Supplied framework 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 #define MAXLEN 100 Declaration for function Overlap(): int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth); int main( ) { FILE Data / =??? /; FILE Log / =??? /; 1: Open the test data file 2: Open the results log file int32_t aswx, aswy, aheight, awidth, specification of 1st rectangle bswx, bswy, bheight, bwidth; specification of 2nd rectangle char Line[MAXLEN + 1]; fgets(line, MAXLEN, Data); fgets(line, MAXLEN, Data); Read header lines / fprintf(log,???); / 3: Write data header lines to the log file / fprintf(log,???); / 4: Read the data for first pair of rectangles: int32_t numread / = fscanf(data,??? ) /; while ( numread / ==??? /) { 5: Check if all values were read 6: Write data for current rectangles to log: / fprintf(log,??? ); / This is a purely individual assignment! 3
7: Test for an overlap and log the results: / if (??? ) { fprintf(log, " else { overlap detected\n"); fprintf(log, " no overlap detected\n"); / 8: Read data for next pair of rectangles: / numread = fscanf(data,??? ); / 9: Write the marker for the end of the table: / fprintf(log,???); / /???; / 10: Close the input and output files /???; / return 0; / Determines whether two rectangles, A and B, intersect. Pre: 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 / int32_t bswx, int32_t bswy, uint32_t bheight, uint32_t bwidth) { bool overlapdetected = false; Your implementation goes here... return overlapdetected; This is a purely individual assignment! 4
Pledge: 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. Suggestions for implementing 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. 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 Overlap() 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 Overlap(). 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 if statement (7) that's commented out in the posted code. Once I've got Overlap() tested, I'd complete the code to write the header lines (3), and the rectangle data (6), and the 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! 5