CSSE 120 -- Intro. to Software Development

Homework 25

  1. Don't forget to refer to the Python vs C comparison document. You may find this helpful as you try to do things in C that you already know how to do in Python. Feel free to suggest things that we might add to this document.
  2. Complete the 25-Structures1 project that we began in class.
  3. Note: this problem, while good and challenging, is not as directly useful to your mastery of C as the previous C exercises have been. If you are behind, catch up on the previous exercises (including the one above) and skip this exercise if necessary.

    Checkout the 25-Structures2 project. The rest of this assignment refers to that project.

    (35 points) This problem involves defining and using some 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.

    Intersecting rextangles

    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.cThe file main.c should NOT be modified.

    1. (3 pts) Name and programming style
    2. (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).
    3. (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.

    4. (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.
    5. (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.
    6. (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.
    7. (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.
    8. (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.
    9. (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.