(Enhancing Class Rectangle) Modify class Rectangle from Exercise 9.21 to 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 out 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. If you feel ambitious, you might include functions to scale the size of the rectangle, rotate it, and move it around within the desig- nated portion of the first quadrant.

What will be an ideal response?


```
#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "Point.h" // include definition of class Point

class Rectangle
{
public:
// default constructor
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
void setCoord( Point, Point, Point, Point );
double length(); // length
double width(); // width
void perimeter(); // perimeter
void area(); // area
bool square(); // square
void draw(); // 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
```
```
// Member-function definitions for class Rectangle.
#include
#include
#include
#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
{
cout << "Coordinates do not form a rectangle!\n"
<< "Use default values.\n";
point1 = Point( 0.0, 1.0 );
point2 = Point( 1.0, 1.0 );
point3 = Point( 1.0, 0.0 );
point4 = Point( 0.0, 0.0 );
} // end else
} // end function setCoord

double Rectangle::length()
{
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()
{
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()
{
cout << fixed << "\nThe perimeter is: " << setprecision( 1 )
<< 2 * ( length() + width() ) << endl;
} // end function perimeter

void Rectangle::area()
{
cout << fixed << "The area is: " << setprecision( 1 )
<< length() * width() << endl;
} // end function area

bool Rectangle::square()
{
return length() == width();
} // end function square

// draw rectangle
void Rectangle::draw()
{
for ( double y = 25.0; y >= 0.0; y-- )
{
for ( double x = 0.0; x <= 25.0; x++ )
{
( ( 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 << '\n';
} // 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
```
```
#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
```
..........................
..........................
..........................
..........................
..........................
............*******.......
............*?????*.......
............*?????*.......
............*?????*.......
............*?????*.......
............*?????*.......
............*?????*.......
............*?????*.......
............*******.......
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................

Computer Science & Information Technology

You might also like to view...

One point is how big?

A) 1/72 of an inch B) The same as a pixel C) The same as one character D) 1/36 of an inch

Computer Science & Information Technology

Which of the following commands should you use to specify that you want to use Git for version tracking on them?

A. git version B. git track C. git add D. git new

Computer Science & Information Technology

Most C compilers require a program file to have either the extension c or cc.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Explain how to change the case of a variable's value ?using the toLowerCase() or toUpperCase() method.

What will be an ideal response?

Computer Science & Information Technology