UMBC CMSC 202
UMBC CMSC 202 CSEE | 202 | current 202

CMSC 202 Project 2

Quadrilaterals

Posted:

Monday 2/17/03

Design Document Due:

Before midnight, Tuesday 2/25/03

Project Due:

Before midnight, Tuesday 3/4/03

Notes

Feb 22, 2003 8:45am
The third hint for trapezoids "the vertical distance from UR to UL equals the horizontal distance from LL to LR" was added on the 18th when the additional orientation of the trapezoid was added. This has now been removed. It could have led to irregular quadrilaterals being identified as trapezoids. The sample ouput for the "new trapezoid" has been removed as well. Feb 21, 2003 2:30pm
The calculations of the area of the new trapezoid added to the sample output on Feb 18th
(Upper Left: ( 0, 5) Upper Right: ( 0, 10) Lower Left: ( 5, 0) Lower Right: ( 10, 0))
is incorrect. The correct area is 37.5 (more or less).

When investigating this discrepancy we realized that requiring students to make calculations for a trapezoid with this orientation takes away from the focus of the project -- implmenting and using classes and objects in C++. Therefore, students may assume (as was originally the plan) that trapezoids will be oriented as first described and as shown in the diagrams below. The parallel sides are either parallel to the x-axis or parallel to the y-axis.

Feb 18, 2003 2:30pm
The original trapezoid explanation assumed that the base of the trapezoid was one of the two parallel sides which was also parallel to the x-axis. A student pointed out that it's possible to have one of the non-parallel sides be the base that is parallel to the x-axis. The explanation of trapezoid has been expanded to include this general case. This new trapezoid case has been added to the sample output.
Feb 18, 2003 10:00am
The explanation of trapezoid was slightly modified. It used "distance" when "vertical distance" and "horizontal distance" were correct.

Feb 17, 2003
Because UMBC is closed on Tuesday 2/18/03, we have decided to extend project 2 as well. Note the new due dates above. It seems the original design due date was wrong anyway.
The original posting this morning had a couple of references to Proj1 which were obviously typos. These were changed to Proj2


Objectives


Project Description

In geometry, a 4-sided figure is called a "Quadrilateral". Depending on its exact shape, a quadrilateral can be classified as follows

  1. A parallelogram -- both pairs of sides parallel
  2. A rectangle - a parallelogram with right angles
  3. A square - a rectangle with equal sides
  4. A trapezoid -- a quadrilateral with just one pair of parallel sides
  5. Irregular -- none of the above.

In this project, you will ask the user to input the x- and y-coordinates of the four corners of a quadrilateral. The corners are called "UpperRight", "UpperLeft", "LowerLeft" and "LowerRight" and hereafter referred to as UR, UL, LL and LR respectively.

Your program will then classify the quadrilateral as above and output some information about the quadrilateral.

To make this project reasonable, your code may assume that the base of the quadrilateral is parallel to the x-axis (i.e. the y-coordinate of "LowerLeft" and "LowerRight" is the same). Your user input code will enforce this requirement.


What to Code

This project will require you design and implement two C++ classes as described below. Be sure to consider the OOP principles of encapsulation and data hiding when designing and implementing these classes. main() and other functions are also described.

A Point Class

The Point class will encapsulate the x- and y-coordinates of one point in the coordinate plane. In keeping with the coding standards, the Point class will be defined in Point.H and implemented in Point.C. The Quadrilateral class uses the Point class to represent each of its four corners (an example of aggregation/composition). You may assume that the coordinates are integers (although this has little effect on your code).

The only operations allowed by the Point class are listed. If you find that you don't need all these operations, then you are not required to implement them. No other public methods are permitted. You may provide whatever PRIVATE methods you feel are necessary.

  1. One or more constructors
  2. Accessors for the coordinates
  3. Mutators for the coordinates

A Quadrilateral Class

This class represents a quadrilateral in the coordinate plane by using the Point class to store the x- and y-coordinates of its corners. This class will be defined in Quadrilateral.H and implemented in Quadrilateral.C.

The "const"ness of the member functions has purposely been omitted. It's up to you to determine which should be const and which should not.

The operations permitted by the Quadrilateral class are listed. No other public methods are permitted. If you find that you don't need all these operations, then you are not required to implement them. No other public methods are permitted. You may provide whatever PRIVATE methods you feel are necessary.

  1. One or more constructors
  2. Accessors for the corners
    You may name and implement these as you see fit, but their return type must be
    const Point&
  3. Mutators for the corners
    You may name and implement these as you see fit, but their one and only parameter must be a
    const Point&
  4. bool IsParallelogram( void ) -- returns true if the quadrilateral is a parallelogram; false otherwise.
  5. bool IsRectangle( void ) -- returns true if the quadrilateral is a rectangle; false otherwise.
  6. bool IsSquare( void ) -- returns true if the quadrilateral is a square; false otherwise.
  7. bool IsTrapezoid( void ) -- returns true if the quadrilateral is a trapezoid; false otherwise.
  8. bool IsIrregular( void ) -- returns true if the quadrilateral is irregular; false otherwise.
  9. double Area( void ) -- returns the area of the quadrilateral. This method returns 0.0 if called for an irregular quadrilateral.
  10. double Perimeter( void ) -- returns the perimeter of the quadrilateral.

Distance Functions

To ease the coding of the Quadrilateral methods, the following functions are also required. These functions are NOT members of either the Point or Quadrilateral classes; they are "non-member" functions. Since they are related to Points, their prototypes may be defined in Point.H and they may be implemented in Point.C.

  1. double Distance (const Point& point1, const Point& point2)
    If we let x1 and y1 be the coordinates of point1 and let x2 and y2 be the coordinates of point 2. Then the formula for the distance between two points is
    distance = square root of ( |x1 - x2|2 + |y1 - y2|2 )

The next two definitions rely on the base of the quadrilateral being parallel to the x-axis. Because of this constraint, you can use these functions to find the distance between two parallel lines.

  1. double VerticalDistance (const Point& point1, const Point& point2)
    The vertical distance between two points is the absolute value of the difference of the y-coordinates.
  2. double HorizontalDistance (const Point& point1, const Point& point2)
    The horizontal distance between two points is the absolute value of the difference of the x-coordinates.


About math functions

main() in Proj2.C

Proj2 is invoked with no command line arguments. An appropriate error message must be displayed if command line arguments are provided. Your program should then exit.

Your main() will prompt the user for the coordinates of the four corners of your quadrilateral. You can use whatever prompts you want, but the user must be allowed to input both the x- and y-coordinate on the same line as in the sample output below. You may assume that the user will input the corners in their proper relationship (i.e. "UpperRight" really will be the corner in the relative upper-right position). You may assume the user will enter four distinct corners that really do form a quadrilateral. You may assume that the coordinates are integers and that the user will input integers when requested. Your input validation must check that the base of the quadrilateral (the line between LL and LR) is parallel to the x-axis (see above). If not, produce an error message and reprompt.

Your program will also produce the output shown below and formatted as follows.

The area and perimeter must be printed on separate lines, their labels must be right justified and aligned and their values must be aligned and printed with exactly 4 decimal places of precision. Print trailing zeros if necessary.

The corners must be printed as shown. In particular the labels must be right justified and aligned and the coordinates must be printed in a 3-character field, separated by a comma and enclosed in parentheses.

You should of course use good top-down design for main. If you write any functions called by main, they must be implemented in Proj2Aux.C and their prototypes must be in Proj2Aux.H.


Classifying A Quadrilateral

Parallelogram

A quadrilateral is a parallelogram if both pairs of sides are parallel. This is equivalent saying that the lengths of the opposite sides of each pair of sides are equal.
I.e. if the distance from UL to UR equals the distance from LL to LR
and the distance from UL to LL equals the distance from UR to LR

Rectangle

A quadrilateral is rectangle if it is a parallelogram with four right angles. Note that it is sufficient to test the angle at just one corner, since if a parallelogram has one right angle, it must have four right angles, else it's not a parallelogram.

Square

A quadrilateral is a square if it is a rectangle with four equal sides. Note that since it is a rectangle (and hence a parallelogram) it is only necessary to compare the length of one horizontal side with the length of one vertical side.

Trapezoid

A quadrilateral is a trapezoid if it has exactly one pair of parallel sides.
It's not a parallelogram and the horiztonal distance from UL to UR equals the horizontal distance from LL to LR,
OR the vertical distance from UL to LL equals the vertical distance from UR to LR,

The area of a trapezoid = 1/2 * (sum of the lengths of the parallel sides) * (distance between the parallel sides)

Irregular

A quadrilateral is irregular if it is none of the above.


Sample Run

You have to match this output almost exactly (see above). Your prompts may vary, but you must allow both coordinates to be input on the same line. The greeting is optional, but if present will be more elaborate than the sample below.

linux3[1]% Proj2 2 3 4
Invalid Command Line: No command line arguments required
Usage: Proj2
 
linux3[1]% Proj2
 
This is my optional greeting
 
Please enter x- and y-coordinates of the UpperLeft corner:  4 5
Please enter x- and y-coordinates of the LowerLeft corner:  4 1
Please enter x- and y-coordinates of the LowerRight corner: 8 4
Y-coordinate must be the same for LowerLeft and LowerRight
Please enter x- and y-coordinates of the LowerRight corner: 8 6
Y-coordinate must be the same for LowerLeft and LowerRight
Please enter x- and y-coordinates of the LowerRight corner: 8 1
Please enter x- and y-coordinates of the UpperRight corner: 8 5
 
The Quadrilateral's Corners
       Upper Left: (  4,  5)      Upper Right: (  8,  5)
       Lower Left: (  4,  1)      Lower Right: (  8,  1)
 
This Quadrilateral is a Square
             Area:  16.0000
        Perimeter:  16.0000
linux3[2]% 
linux3[2]% Proj2
 
This is my optional greeting
 
Please enter x- and y-coordinates of the UpperLeft corner:  1 4
Please enter x- and y-coordinates of the LowerLeft corner:  1 2
Please enter x- and y-coordinates of the LowerRight corner: 10 2
Please enter x- and y-coordinates of the UpperRight corner: 10 4
 
The Quadrilateral's Corners
       Upper Left: (  1,  4)      Upper Right: ( 10,  4)
       Lower Left: (  1,  2)      Lower Right: ( 10,  2)
 
This Quadrilateral is a Rectangle
             Area:  18.0000
        Perimeter:  22.0000
linux3[3]% 
linux3[3]% Proj2
 
This is my optional greeting
 
Please enter x- and y-coordinates of the UpperLeft corner:  6 4
Please enter x- and y-coordinates of the LowerLeft corner:  9 1
Please enter x- and y-coordinates of the LowerRight corner: 16 1
Please enter x- and y-coordinates of the UpperRight corner: 13 4
 
The Quadrilateral's Corners
       Upper Left: (  6,  4)      Upper Right: ( 13, 4)
       Lower Left: (  9,  1)      Lower Right: ( 16, 1)
 
This Quadrilateral is a Parallelogram
             Area:  21.0000
        Perimeter:  22.4853
linux3[4]% 
linux3[4]% Proj2
 
This is my optional greeting
 
Please enter x- and y-coordinates of the UpperLeft corner:  5 5
Please enter x- and y-coordinates of the LowerLeft corner: -5 -5
Please enter x- and y-coordinates of the LowerRight corner: 7 -5
Please enter x- and y-coordinates of the UpperRight corner: 7 5

The Quadrilateral's Corners
Upper Left: ( 5, 5) Upper Right: ( 7, 5)
Lower Left: ( -5, -5) Lower Right: ( 7, -5)

This Quadrilateral is a Trapezoid
Area: 70.0000
Perimeter: 38.1421

linux3[5]% 
linux3[5]% Proj2
 
This is my optional greeting
 
Please enter x- and y-coordinates of the UpperLeft corner:  2 3
Please enter x- and y-coordinates of the LowerLeft corner:  0 1
Please enter x- and y-coordinates of the LowerRight corner: 11 1
Please enter x- and y-coordinates of the UpperRight corner: 7 5
 
The Quadrilateral's Corners
       Upper Left: (  2,  3)      Upper Right: (  7,  5)
       Lower Left: (  0,  1)      Lower Right: ( 11,  1)
 
This quadrilateral is Irregular
             Area:  0.0000
        Perimeter:  24.8704
linux3[6]% 

 


Design Assignment

Your project design document for project 2 must be named design2.txt. Be sure to read the design specification carefully. Submit your design in the usual way: submit cs202 Proj2 design2.txt


Error Handling

You must validate all user input. If you detect invalid input, output a meaningful error message and reprompt for the input. See the sample output above.


Project Make File

You are required to submit a make file with this project. The grader should simply need to type "make Proj2" and your project should compile and link to an executable called Proj2. The grader should also have the ability to do a "make clean" and "make cleanest." Hint: Start with the makefile from project 1 and modify it.

See the make file tutorial for more information on make files.


Grading

The grade for this project will be broken down as follows. A more detailed breakdown will be provided in the grade form you receive with your project grade.


Project Submission

You must use separate compilation for this project. You should submit the following files.

Submit as follows:

           submit cs202 Proj2 <list of files>

 A complete guide to submit tools is available on the course website.
More complete documentation for submit and related commands (not including submitmake and submitrun) can be found here.

Remember -- if you make any change to your program, no matter how insignificant it may seem, you should recompile and retest your program before submitting it. Even the smallest typo can cause compiler errors and a reduction in your grade.


Last Modified: Saturday, 22-Feb-2003 08:51:05 EST