Faculty of Applied Science and Engineering, University of Toronto
CSC181: Introduction to Computer Programming

Assignment 2: Point and Rectangle Functions

Due: Thursday, 12 October by the start of lecture
Marking Scheme: 50% Correctness, 25% Style, 25% Documentation

Announcements are available on the course Web site. It is your responsibility to keep up-to-date on these announcements.

Introduction

The purpose of this assignment is to give you practice implementing data types: the structures holding the data and the functions manipulating the data. You will be working with three data types:

You must define the data structures for these types (with matching typedefs) inside a file called a2.h:

typedef ?????? Point;
typedef ?????? Rectangle;
typedef ?????? RectangleList;

a2.h will also include the prototypes for the functions you write. The actual definitions will be placed in a file called a2.c.

You must also add the following line to a2.h, since some of the functions you'll be writing will return boolean values:

typedef enum {false, true} boolean; 

Point Functions

A Point represents a point on a 2-D plane. As such, it has x- and y-coordinates, which are initially set through calls to pointInit, and subsequently accessed through calls to pointX and pointY.

The complete set of functions you have to implement for Point are as follows:

Point pointInit(const double x, const double y)
Returns a point with the given x- and y-coordinates.

double pointX(const Point p)
Returns the x-coordinate of the given point.

double pointY(const Point p)
Returns the y-coordinate of the given point.

boolean pointAreEqual(const Point p1, const Point p2)
Returns true if the two given points are equal (i.e. have the same x- and y-coordinates), false otherwise.

double pointDistanceBetween(const Point p1, const Point p2)
Returns the distance between the two given points.

Rectangle Functions

A Rectangle represents a rectangle on a 2-D plane. It has a centre point, as well as a width and height, which are initially set through calls to rectangleInit, and subsequently accessed through calls to rectangleCentre, rectangleWidth and rectangleHeight.

The complete set of functions you have to implement for Rectangle are as follows:

Rectangle rectangleInit(const Point c, const double w, const double h)
Returns a rectangle with the given centre, width (on the x-plane) and height (on the y-plane).

Point rectangleCentre(const Rectangle r)
Returns the centre of the given rectangle.

double rectangleWidth(const Rectangle r)
Returns the width of the given rectangle.

double rectangleHeight(const Rectangle r)
Returns the height of the given rectangle.

boolean rectangleAreEqual(const Rectangle r1, const Rectangle r2)
Returns true if the two given rectangles equal (i.e. they have the same width and height, and also the same centre), false otherwise.

Rectangle rectangleUnion(const Rectangle r1, const Rectangle r2)
Returns the rectangle which is the union of the given two rectangles.

Rectangle rectangleIntersection(const Rectangle r1, const Rectangle r2)
Returns the rectangle which is the intersection of the given two rectangles.

double rectangleArea(const Rectangle r)
Returns the area of the given rectangle.

RectangleList Functions

A RectangleList is an ordered collection of Rectangles. They are indexed like a C array, with the first rectangle at index 0 and the last rectangle at index n-1, where n is the number of rectangles in the list (returned by rectangleListSize). The list should have a minimum capacity of 1024; that is, it must be able to hold at least 1024 rectangles.

rectangleListInit is used to get an initially empty list. New lists can be constructed through calls to rectangleListAdd (which appends a rectangle) and rectangleListSet (which substitutes a rectangle at a particular position). Rectangles in the list can be accessed through calls to rectangleListGet.

The complete set of functions you have to implement for Rectangle are as follows:

RectangleList rectangleListInit(void)
Returns an empty list of rectangles.

int rectangleListSize(const RectangleList rl)
Returns the size of the given list of rectangles.

Rectangle rectangleListGet(const RectangleList rl, const int i)
Returns the rectangle at the given index in the given rectangle list. The given index must be between 0 and n-1 inclusive, where n is the size of the list.

RectangleList rectangleListAdd(const RectangleList rl, const Rectangle r)
Returns a rectangle list that would be the result of appending the given rectangle to given rectangle list.

RectangleList rectangleListSet(const RectangleList rl, const Rectangle r, const int i)
Returns a rectangle list that would be the result of replacing the rectangle at the given index in the given rectangle list with the given rectangle. The given index must be between 0 and n-1 inclusive, where n is the size of the list.

RectangleList rectangleListSubList(const RectangleList rl, const int a, const int b)
Returns a rectangle list containing the portion of the list between a, inclusive, and b, exclusive. (In the returned list, the rectangle at index 0 will be the rectangle that is at index a in rl.)

Rectangle rectangleListUnion(const RectangleList rl)
Returns the rectangle which is the union of all of the rectangles in the given rectangle list.

Rectangle rectangleListIntersection(const RectangleList rl)
Returns the rectangle which is the intersection of all of the rectangles in the given rectangle list.

double rectangleListArea(const RectangleList rl, const boolean discountOverlap)
If the given boolean is false, then the function simply returns the sum of the areas of the rectangles in the given rectangle list. However, if the given boolean is true, then the function must take into account overlapping rectangles; specifically, an intersecting area between two (or more) rectangles can only be counted once in the calculation of the sum of the areas of the rectangles.

Notes and Hints

1. All of the function parameters are marked const. Therefore, your functions cannot change them.

2. You may assume that all Point, Rectangle and RectangleList variables are initialized using their respective initialization functions before they are used for any other purpose.

3. Use structs in your definitions of the Point, Rectangle and RectangleList data structures, but do not use pointers.

4. Your functions must stay silent unless there is an error. That is, they cannot print anything (using printf, for example) unless an error occurs.

5. You should test your functions thoroughly, but you do not have to hand in any testing.

6. Do not include a main function in a2.c.

7. Above all else, keep it simple. NO extra credit will be given for extra features.

What to Submit

This assignment must be done in C. Note that this means that assignments done in C++ will not be accepted. We will use gcc to compile your programs, and they must compile using gcc. It is your responsibility to ensure that your source files compile on ECF.

As specified above, write your solution code in a2.h and a2.c. You should also put together a meta-file (called info) containing other information about your assignment. The format of the meta-file is as follows:

Name:<your name>
StudentNo:<your student #>
Files:<comma-separated list of files submitted>

Electronically submit your solution code and meta-file to your Assignment 2 subdirectory under the CSC181 submit directory on the ECF server:

submitcsc181f 2 a2.c a2.h info

Note that if you run this command again, it will overwrite your previous submission.

You can check your submission using the following command:

submitcsc181f -l 2

Last updated on 2000-10-02 by Ray Ortigas.