(35 points) This problem involves defining and using some
structures (structs) to model filled rectangles on the Cartesian (x, y) plane. A
rectangle is represented by two points, point1
and point2, indicating opposite corners of
the rectangle. The figure below illustrates the problem. The
labels in the figure correspond to the variable names in the provided
test code.
The parts of this problem must be completed in the
order given; testing of the later steps depends on your implementation
of the earlier steps. Part (b) should be completed in file rectangle.h
and the remaining parts of the problem should be completed in rectangle.c.
The file main.c should NOT be
modified.
- (3 pts) Name and programming style
- (2 pts) We've provided an empty struct type Rectangle in file
rectangle.h. Change this struct to store the necessary
information (the points described above).
- (2 pts) Now write a function Rectangle
makeRect(int x1, int y1, int x2, int y2) in rectangle.c
that makes a Rectangle from the given coordinates, where point1
has coordinates (x1, y1) and point2 has coordinates (x2, y2).
Any opposite corners may be given; for example r1 in
the figure could be generated using any one of the following:
- r1 = makeRect(-3, 3, 5, -2) ,
- r1 = makeRect(-3, -2, 5, 3) ,
- r1 = makeRect(5, -2, -3, 3) , or
- r1 = makeRect(5, 3, -3, -2).
The function stub is already included, as is code in main()
that tests your function.
- (2 pts) Write a function int getLeft(Rectangle
r) that returns the x-coordinate of the left-most edge
of the given rectangle. For example, getLeft(r1) should return -3. The
function stub is already included, as is code in
main()
that tests your function.
- (2 pts) Write a function int getRight(Rectangle
r) that returns the x-coordinate of the right-most edge
of the given rectangle. For example, getRight(r1) should return 5. The
function stub is already included, as is code in
main()
that tests your function.
- (2 pts) Write a function int getTop(Rectangle r)
that returns the y-coordinate of the top-most edge of the given
rectangle. For example, getTop(r1) should return 3. (Note: y values
increase as we move up, like in mathematics.) The function stub is
already included, as is code in
main()
that tests your
function.
- (2 pts) Write a function int
getBottom(Rectangle r) that returns the y-coordinate of
the bottom-most edge of the given rectangle. For example, getBottom(r1)
should return -2. The function stub is already included, as is code in
main()
that tests your function.
- (12 pts) Write a function int
areIntersecting(Rectangle q, Rectangle r) that returns a
"true" value if the given rectangles touch and a "false" value if they
do not touch. For example, areIntersecting(r1,r3) should return
"false", but areIntersecting(r1,r2) and areIntersecting(r2,r3) should
both return "true". The function stub is already included, as is code
in
main()
that tests your function.
- (8 pts) Write a function Rectangle
intersect(Rectangle q, Rectangle r) that returns a new
rectangle representing the overlapping area of the two given
rectangles. You may assume that the two rectangles do, in fact,
overlap. The function stub is already included, as is code in
main()
that tests your function.
When your code is working, commit it to your repository.