From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 06_rect/README | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 06_rect/README (limited to '06_rect/README') diff --git a/06_rect/README b/06_rect/README new file mode 100644 index 0000000..7309f2c --- /dev/null +++ b/06_rect/README @@ -0,0 +1,65 @@ +For this problem, we will be writing a function which takes +two rectangles, determines the region in which they overlap +(which is also a rectangle), and returns that region. + +Before proceeding, think about what the corner case(s) [no pun intended!] +in this problem might be. + + 1. Open the provided file called "rectangle.c" + + 2. In the "rectangle.c" file, define a struct for rectangles + which has 4 fields: x, y, width, and height. Each of these + fields should be an int. Use typedef so that the typename + "rectangle" refers to your struct. + + 3. One corner we might have to deal with is if the rectangle's + representation is non-standard: if the width or height are + negative. One way to deal with inputs that come in non-standard + formats is to "canonicalize" them---convert them to the standard + (or "canonical") representation. First, write the function + + rectangle canonicalize(rectangle r); + + which takes a rectangle, and "fixes" its representation + by ensuring that the width and height are non-negative (and + appropriately adjusting the x and/or y co-ordinate). That is, + if your canonicalize function were passed a rectangle with + x=3, y=2, width=-2, height=4 + then it should return a rectangle with + x=1, y=2, width=2, height=4 + as these both describe the same rectangle, but the later is + in a canonical representation. + + It may be a good idea to stop and test this function + before proceeding. Compile and run your code. + The main function we have provided should be useful, + as it canonicalizes the rectangles it prints. + + 4. Now, write the function + rectangle intersection(rectangle r1, rectangle r2) + which takes two rectangles (r1, and r2), and returns + the rectangle representing the intersection of the two. + + Note that there is a corner case where the correct answer + is "no intersection". We have not learned how to represent + "no such thing" yet, but we will consider a rectangle with + both width and height equal to 0 to mean "no such rectangle". + + We will consider a rectangle to have one (but not the other) + of width or height equal to 0 to be an appropriate answer + when rectangles share an edge but do not overlap. For example, + x=0,y=0,width=1, height=1 + and + x=-1,y=1,width=3,height=2 + Should result in the "rectangle" + x=0,y=1,width=1,height=0 + 5. We have provided a main function which tests your code, + as well as correct output (rectangle_ans.txt) to diff against. + + 6. Submit your code + +Hint: + Do Step 1 (work an example yourself) four or five times. + Draw a variety of different ways that rectangles can overlap. + Use these to help you think abou the general algorithm for how + to determine their overlapping region. -- cgit v1.2.3