Lab 7 Ex2

You cannot submit for this problem because the homework's deadline is due.

Ex2. Rectangles (30 marks)

Based on what you have done for Ex1, implement a data type to represent rectangles with edges parallel to the axes in a 2D Cartesian coordinate system.

We have provided you with a file Rectangle.h that contains a struct definition as well as declarations for three functions:

#include "Point.h"

typedef struct {
    // A rectangle's position is determined by its left-down vertex and right-up
    // vertex. The other diagonal pair also works, but we choose to use this
    // pair.
    Point left_down_p;
    Point right_up_p;
} Rectangle;

// Create a rectangle given a pair of diagonal vertices. left_down_p is
// guaranteed to have smaller x and y coordinate than right_up_p, left_down_p ==
// (2, 1) and right_up_p == (3, 4) for instance. Also, this means that the
// rectangle's area is guaranteed to be non-zero. The points are given as
// pointers to reduce copy.
Rectangle create_rectangle(const Point* left_down_p, const Point* right_up_p);

// Return the area of the given rectangle. The rectangle is given as a pointer to reduce
// copy.
double rectangle_area(const Rectangle* rect);

// Return whether two rectangles overlap.
bool rectangle_overlap(const Rectangle* rect1, const Rectangle* rect2);

Implement the three functions in a new file named Rectangle.c. Note that to test your function implementation, you need to write your own driver program. As Rectangle.h includes Point.h, you will need to make sure that Point.h is in the same directory as Rectangle.c. If you use any function defined in Point.c, you need to add it to your compilation command to compile it together with Rectangle.c. Submit Rectangle.c only!

For rectangle_overlap(), return true if and only if their overlapping region has an area greater than 0. In other words, two rectangles with only a vertex or an edge overlapping should not be considered overlapping.

You can use any C standard library function, including those declared in math.h.

Hint: It might be easier to judge whether two rectangles do not overlap.

We will use a driver program to test your implementation. An example driver program ex2_sample_test.c can be downloaded from Canvas, and its output is shown below:

rect1 area: 12.000
rect2 area: 9.000
overlap: 1

As long as your implementation is correct, you will pass the tests on JOJ. You do not need to worry about I/O.

Lab 7 Exercises

Not Claimed
Status
Finished
Problems
4
Open Since
2022-06-30 18:15
DDL
2022-07-01 23:59
Extension
0.0 hour(s)