Create a class that stores only the Cartesian coordinates of the four corners of
ID: 640960 • Letter: C
Question
Create a class that stores only the Cartesian coordinates of the four corners of a rectangle. Provide member functions that calculate the length, width, diagonal, perimeter and area. Provide set and get functions for the length and width attributes. The set function also verifies that the supplied coordinates do, in fact, specify a rectangle. The length is the larger of the two dimensions.
The constructor calls a set function that accepts four sets of coordinates and verifies that each of these is in the first quadrant with no single x or y coordinates larger than a floating point integer of 20.0 and no smaller than 0.0.
Include a predicate function square that determines whether the rectangle is a square. Include a draw function that displays the rectangle inside a 25 by 25 box enclosing the portion of the first quadrant in which the rectangle resides. Include a setFillCharacter function to specify the character of which the body of the rectangle will be drawn (#). Include a setPerimeterCharacter function to specify the character that will be used to draw the border of the rectangle.
Create a class that stores only the Cartesian coordinates of the four corners of a rectangle. Provide member functions that calculate the length, width, diagonal, perimeter and area. Provide set and get functions for the length and width attributes. The set function also verifies that the supplied coordinates do, in fact, specify a rectangle. The length is the larger of the two dimensions. The constructor calls a set function that accepts four sets of coordinates and verifies that each of these is in the first quadrant with no single x or y coordinates larger than a floating point integer of 20.0 and no smaller than 0.0. Include a predicate function square that determines whether the rectangle is a square. Include a draw function that displays the rectangle inside a 25 by 25 box enclosing the portion of the first quadrant in which the rectangle resides. Include a setFillCharacter function to specify the character of which the body of the rectangle will be drawn (#). Include a setPerimeterCharacter function to specify the character that will be used to draw the border of the rectangle.Explanation / Answer
// Exercise 9.18 Solution: Ex09_12.cpp
#include <iostream>
#include <stdexcept>
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;
int main()
{
Point w( 1.0, 1.0 );
Point x( 5.0, 1.0 );
Point y( 5.0, 3.0 );
Point z( 1.0, 3.0 );
Point j( 0.0, 0.0 );
Point k( 1.0, 0.0 );
Point m( 1.0, 1.0 );
Point n( 0.0, 1.0 );
Rectangle r1( z, y, x, w );
cout << "Rectangle 1: ";
cout << "length = " << r1.length();
cout << " width = " << r1.width();
r1.perimeter();
r1.area();
cout << "The rectangle "
<< ( r1.square() ? "is" : "is not" )
<< " a square. ";
Rectangle r2( j, k, m, n );
cout << " Rectangle 2: ";
cout << "length = " << r2.length();
cout << " width = " << r2.width();
r2.perimeter();
r2.area();
cout << "The rectangle "
<< ( r2.square() ? "is" : "is not" )
<< " a square. ";
try
{
Rectangle r3( w, x, m, n );
}
catch ( invalid_argument & )
{
cout << " Not a valid Rectangle" << endl;
}
try
{
Point v( 99.0, -2.3 );
}
catch ( invalid_argument & )
{
cout << "Not a valid Point" << endl;
}
} // end main
point.cpp
// Member-function definitions for class Point.
#include <stdexcept>
#include "Point.h" // include definition of class Point
using namespace std;
Point::Point( double xCoord, double yCoord )
{
setX( xCoord ); // invoke function setX
setY( yCoord ); // invoke function setY
} // end Point constructor
// set x coordinate
void Point::setX( double xCoord )
{
if ( xCoord >= 0.0 && xCoord <= 20.0 )
x = xCoord;
else
throw invalid_argument( "x must be >= 0.0 and <= 20.0" );
} // end function setX
// set y coordinate
void Point::setY( double yCoord )
{
if ( yCoord >= 0.0 && yCoord <= 20.0 )
y = yCoord;
else
throw invalid_argument( "y must be >= 0.0 and <= 20.0" );
} // end function setY
// return x coordinate
double Point::getX() const
{
return x;
} // end function getX
// return y coordinate
double Point::getY() const
{
return y;
} // end function getY
point.h
#ifndef POINT_H
#define POINT_H
class Point
{
public:
explicit Point( double = 0.0, double = 0.0 ); // default constructor
// set and get functions
void setX( double );
void setY( double );
double getX() const;
double getY() const;
private:
double x; // 0.0 <= x <= 20.0
double y; // 0.0 <= y <= 20.0
}; // end class Point
#endif
Rectangle.cpp
// Member-function definitions for class Rectangle.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdexcept>
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;
Rectangle::Rectangle( Point a, Point b, Point c, Point d )
{
setCoord( a, b, c, d ); // invokes function setCoord
} // end Rectangle constructor
void Rectangle::setCoord( Point p1, Point p2, Point p3, Point p4 )
{
// Arrangement of points
// p4.........p3
// . .
// . .
// p1.........p2
// verify that points form a rectangle
if ( ( p1.getY() == p2.getY() && p1.getX() == p4.getX()
&& p2.getX() == p3.getX() && p3.getY() == p4.getY() ) )
{
point1 = p1;
point2 = p2;
point3 = p3;
point4 = p4;
} // end if
else
throw invalid_argument(
"Coordinates do not form a rectangle! " );
} // end function setCoord
double Rectangle::length() const
{
double side1 = fabs( point4.getY() - point1.getY() ); // get side1
double side2 = fabs( point2.getX() - point1.getX() ); // get side2
double length = ( side1 > side2 ? side1 : side2 );
return length;
} // end function length
double Rectangle::width() const
{
double side1 = fabs( point4.getY() - point1.getY() ); // get side1
double side2 = fabs( point2.getX() - point1.getX() ); // get side2
double width = ( side1 < side2 ? side1 : side2 );
return width;
} // end function width
void Rectangle::perimeter() const
{
cout << fixed << " The perimeter is: " << setprecision( 1 )
<< 2 * ( length() + width() ) << endl;
} // end function perimeter
void Rectangle::area() const
{
cout << fixed << "The area is: " << setprecision( 1 )
<< length() * width() << endl;
} // end function area
bool Rectangle::square() const
{
return length() == width();
} // end function square
Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "Point.h" // include definition of class Point
class Rectangle
{
public:
// default constructor
explicit Rectangle( Point = Point( 0.0, 1.0 ), Point = Point( 1.0, 1.0 ),
Point = Point( 1.0, 0.0 ), Point = Point( 0.0, 0.0 ) );
// sets x, y, x2, y2 coordinates
void setCoord( Point, Point, Point, Point );
double length() const; // length
double width() const; // width
void perimeter() const; // perimeter
void area() const; // area
bool square() const; // square
private:
Point point1;
Point point2;
Point point3;
Point point4;
}; // end class Rectangle
#endif
2nd problem
#include "Rectangle.h" // include definition of class Rectangle
int main()
{
Point point1( 12.0, 12.0 );
Point point2( 18.0, 12.0 );
Point point3( 18.0, 20.0 );
Point point4( 12.0, 20.0 );
Rectangle rectangle( point1, point2, point3, point4, '?', '*' );
rectangle.draw(); // invokes function draw
} // end main
Rectangle.cpp
// Member-function definitions for class Rectangle.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdexcept>
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;
Rectangle::Rectangle( Point a, Point b, Point c, Point d,
char fillChar, char perimeterChar )
{
setCoord( a, b, c, d ); // invoke function setCoord
setFillCharacter( fillChar ); // set fill character
setPerimeterCharacter( perimeterChar ); // set perimeter character
} // end Rectangle constructor
void Rectangle::setCoord( Point p1, Point p2, Point p3, Point p4 )
{
// Arrangement of points
// p4.........p3
// . .
// . .
// p1.........p2
// verify that points form a rectangle
if ( ( p1.getY() == p2.getY() && p1.getX() == p4.getX()
&& p2.getX() == p3.getX() && p3.getY() == p4.getY() ) )
{
point1 = p1;
point2 = p2;
point3 = p3;
point4 = p4;
} // end if
else
throw invalid_argument(
"Coordinates do not form a rectangle! " );
} // end function setCoord
double Rectangle::length() const
{
double side1 = fabs( point4.getY() - point1.getY() ); // get side1
double side2 = fabs( point2.getX() - point1.getX() ); // get side2
double length = ( side1 > side2 ? side1 : side2 );
return length;
} // end function length
double Rectangle::width() const
{
double side1 = fabs( point4.getY() - point1.getY() ); // get side1
double side2 = fabs( point2.getX() - point1.getX() ); // get side2
double width = ( side1 < side2 ? side1 : side2 );
return width;
} // end function width
void Rectangle::perimeter() const
{
cout << fixed << " The perimeter is: " << setprecision( 1 )
<< 2 * ( length() + width() ) << endl;
} // end function perimeter
void Rectangle::area() const
{
cout << fixed << "The area is: " << setprecision( 1 )
<< length() * width() << endl;
} // end function area
bool Rectangle::square() const
{
return length() == width();
} // end function square
// draw rectangle
void Rectangle::draw() const
{
for ( double y = 25.0; y >= 0.0; --y )
{
for ( double x = 0.0; x <= 25.0; ++x )
{
if ( ( point1.getX() == x && point1.getY() == y ) ||
( point4.getX() == x && point4.getY() == y ) )
{
// print horizontal perimeter of rectangle
while ( x <= point2.getX() )
{
cout << perimeterCharacter;
++x;
} // end while
cout << '.'; // print remainder of quadrant
} // end if
else if ( ( ( x <= point4.getX() && x >= point1.getX() ) ) &&
point4.getY() >= y && point1.getY() <= y )
{
cout << perimeterCharacter;
// fill inside of rectangle
for ( ++x; x < point2.getX(); )
{
cout << fillCharacter;
++x;
} // end for
cout << perimeterCharacter;
} // end else if
else
cout << '.'; // print quadrant background
} // end for
cout << ' ';
} // end for
} // end function draw
// set fill character
void Rectangle::setFillCharacter( char fillChar )
{
fillCharacter = fillChar;
} // end function setFillCharacter
// set perimeter character
void Rectangle::setPerimeterCharacter( char perimeterChar )
{
perimeterCharacter = perimeterChar;
} // end function setPerimeterCharacter
Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "Point.h" // include definition of class Point
class Rectangle
{
public:
// default constructor
explicit Rectangle( Point = Point( 0.0, 1.0 ), Point = Point( 1.0, 1.0 ),
Point = Point( 1.0, 0.0 ), Point = Point( 0.0, 0.0 ),
char = '*', char = '*' );
// sets x, y, x2, y2 coordinates
// sets x, y, x2, y2 coordinates
void setCoord( Point, Point, Point, Point );
double length() const; // length
double width() const; // width
void perimeter() const; // perimeter
void area() const; // area
bool square() const; // square
void draw() const; // draw rectangle
void setPerimeterCharacter( char ); // set perimeter character
void setFillCharacter( char ); // set fill character
private:
Point point1;
Point point2;
Point point3;
Point point4;
char fillCharacter;
char perimeterCharacter;
}; // end class Rectangle
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.