summaryrefslogtreecommitdiff
path: root/06_rect/README
blob: 7309f2c47f01f0005e010621bc775cebacb77b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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.