Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c + + not sure how to approach this. Add a Rectangle class to graphics. The Rectangle class should derive from Shape. Add 2 int

c++ not sure how to approach this.
Add a Rectangle class to graphics. The Rectangle class should derive from Shape.
Add 2 int fields with getters/setters. Name the fields width and height.
Add 1 vector field called lines.
Add 1 constructor with the following parameters: width, height, startPt, color. Pass the startPt and color to the base constructor. Use width and height to set the fields. The constructor should create 4 Lines and add them to the lines field.
The 4 Lines:
top left to top right
top right to bottom right
bottom left to bottom right
top left to bottom left
Override the draw method of the Shape class (that means you need to mark the base as virtual). Do not call the base. Instead, call the draw method of each Line in the lines
vector.
The rectangle constructor should be given a start point, a width, and a height. Use that information to create the 4 points of the rectangle which youll use to create the 4 lines of the rectangle. p1 and p2 top of rectangle p3 and p4 are bottom of rectangle - start x, start y. X is positive left to right. Y is positive top to bottom.
In main (which is in Graphics), add code to case 3 of the menu switch.
Generate a random Point2D point with an x,y anywhere in the console. This point will be the top-left position of the Rectangle.
Calculate a random width and height ensure that it will NOT extend the Rectangle beyond the bounds of the console.
Use the point, width, and height to create a Rectangle instance with any color you want.
Call draw on the Rectangle instance.
Shape.h code
class Shape
{
private:
// fields
POINT2D startPt;
ConsoleColor color;
public:
// first constructor
Shape(POINT2D startPt_point, ConsoleColor color_point) : startPt(startPt_point), color(color_point){}
//2nd constructor
Shape(int x, int y, ConsoleColor color_point) : startPt(x, y), color(color_point){}
// Getters for Point2D & ConsoleColor
POINT2D GetStartPt() const {
return startPt;
}
ConsoleColor GetColor() const {
return color;
}
// Setters for Point2D & ConsoleColor
void SetStartPt(POINT2D startPt_point){
startPt = startPt_point;
}
void SetColor(ConsoleColor color_point){
color = color_point;
}
virtual void draw(){
// SET BACKGROUND COLOR
Console::SetBackgroundColor(Magenta);
// MOVE CURSOR TO POINT2D POSITION
Console::SetCursorPosition(startPt.x, startPt.y);
// PRINT A SPACE
Console::Write("");
// RESET THE COLOR. CALL THE RESET METHOD ON THE CONSOLE CLASS.
Console::Reset();
}
};
POINT2D.h code
struct POINT2D
{
public:
int x;
int y;
POINT2D(int x_point, int y_point) : x(x_point), y(y_point){}
};
Line.h code
class Line : public Shape {
private:
POINT2D endPt;
public:
// Constructor
Line(POINT2D startPt, POINT2D endPt, ConsoleColor color) : Shape(startPt, color), endPt(endPt){}
// Getter/Setter for EndPt
POINT2D GetEndPt() const {
return endPt;
}
void SetEndPt(POINT2D endPt_point){
endPt = endPt_point;
}
// override draw method
void draw() override {
// set background color
Console::SetBackgroundColor(GetColor());
// Plot line
PlotLine(GetStartPt().x, GetStartPt().y, endPt.x, endPt.y);
// Reset the color
Console::Reset();
}
private:
// pseudocode implementation for PlotLine
void PlotLine(int x0, int y0, int x1, int y1){
int dx = abs(x1- x0);
int sx = x0< x1?1 : -1;
int dy =-abs(y1- y0);
int sy = y0< y1?1 : -1;
int error = dx + dy;
while (true){
Plot(x0, y0);
if (x0== x1 && y0== y1)
break;
int e2=2* error;
if (e2>= dy){
if (x0== x1)
break;
error = error + dy;
x0= x0+ sx;
}
if (e2<= dx){
if (y0== y1)
break;
error = error + dx;
y0= y0+ sy;
}
}
}
// Plot a single point for (x, y)
void Plot(int x, int y){
Console::SetCursorPosition(x, y);
Console::Write("");
}
};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions