Answered step by step
Verified Expert Solution
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 int fields with getterssetters Name the fields width and height.
Add vector field called lines.
Add 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 Lines and add them to the lines field.
The 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 points of the rectangle which youll use to create the lines of the rectangle. p and p top of rectangle p and p 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 of the menu switch.
Generate a random PointD point with an xy anywhere in the console. This point will be the topleft 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
POINTD startPt;
ConsoleColor color;
public:
first constructor
ShapePOINTD startPtpoint, ConsoleColor colorpoint : startPtstartPtpoint colorcolorpoint
nd constructor
Shapeint x int y ConsoleColor colorpoint : startPtx y colorcolorpoint
Getters for PointD & ConsoleColor
POINTD GetStartPt const
return startPt;
ConsoleColor GetColor const
return color;
Setters for PointD & ConsoleColor
void SetStartPtPOINTD startPtpoint
startPt startPtpoint;
void SetColorConsoleColor colorpoint
color colorpoint;
virtual void draw
SET BACKGROUND COLOR
Console::SetBackgroundColorMagenta;
MOVE CURSOR TO POINTD POSITION
Console::SetCursorPositionstartPtx startPt.y;
PRINT A SPACE
Console::Write;
RESET THE COLOR. CALL THE RESET METHOD ON THE CONSOLE CLASS.
Console::Reset;
;
POINTDh code
struct POINTD
public:
int x;
int y;
POINTDint xpoint, int ypoint : xxpoint yypoint
;
Line.h code
class Line : public Shape
private:
POINTD endPt;
public:
Constructor
LinePOINTD startPt, POINTD endPt, ConsoleColor color : ShapestartPt color endPtendPt
GetterSetter for EndPt
POINTD GetEndPt const
return endPt;
void SetEndPtPOINTD endPtpoint
endPt endPtpoint;
override draw method
void draw override
set background color
Console::SetBackgroundColorGetColor;
Plot line
PlotLineGetStartPtx GetStartPty endPt.x endPt.y;
Reset the color
Console::Reset;
private:
pseudocode implementation for PlotLine
void PlotLineint x int y int x int y
int dx absx x;
int sx x x : ;
int dy absy y;
int sy y y : ;
int error dx dy;
while true
Plotx y;
if x x && y y
break;
int e error;
if e dy
if x x
break;
error error dy;
x x sx;
if e dx
if y y
break;
error error dx;
y y sy;
Plot a single point for x y
void Plotint x int y
Console::SetCursorPositionx y;
Console::Write;
;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started