CSSE 120 -- Intro. to Software Development

Homework 23

  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. You must do this assignment using the RectangleStructs project that you checked out from your individual SVN repository in class.

    Be sure to check out that project into your Eclipse C workspace, not your Python workspace.

  3. (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 shows four specific rectangles that the provided test code will use to test your code. The labels (r1 .. r4) are the variable names used in the provided test code for those rectangles.

    Intersecting rectangles

    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 RectangleStructs.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 two points described above the figure).
    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. At this point your code should run but it won’t pass any of the tests in main() yet.

    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". Note: If you use the getXXX functions that you already implemented, the test for intersection involves just 4 comparisons.  If you think you need more comparisons, get help before proceeding. 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.

Preparation for Next Time

Watch the set of videos assigned for next time and complete the take-home quiz. The main schedule page contains links to the videos, slides, and quiz. Paper copies of the quiz are due at the start of next class.