Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write this python code in C++ without the plotting. # coding: utf-8 # In[1]: # T

ID: 3838885 • Letter: W

Question

write this python code in C++ without the plotting.


# coding: utf-8

# In[1]:


# The Polygon class represents a regular polygon
# with a specified location, radius, and number of sides
# TODO: add rotation

import math # for sin, cos, and pi
import time # for sleep
import procgraphics as pg # for graphics

# a general regular polygon class
class Polygon:
   
    # initialize the location, number of sides, and radius
    def __init__(self,x,y,n,r):
        self.x = x
        self.y = y
        self.n = n
        self.radius = r
        self.I = self.getInteriorAngle()
        self.a = None
        self.a = self.getSideLength()
       
    # determine length of side based on radius and interior angle   
    def getSideLength(self):
        if self.a is None:
            return (math.sqrt((self.radius * math.cos(self.I) -
                               self.radius)**2 +
                              (self.radius * math.sin(self.I))**2))
        else:
            return self.a
   
    # interiorAngle is circle size / number of sides
    def getInteriorAngle(self):
        return math.pi*2/self.n
   
    # perimeter is the number of sides times side length
    def getPerimeter(self):
        return self.n*self.a
   
    # vertex Angle is 180 degrees - interior angle
    def getVertexAngle(self):
        return math.pi-self.I
   
    # use procgraphics to plot
    def plot(self):
        pg.beginShape()
        for i in range(self.n+1):
            v = self.getVertex(i)
            pg.vertex(v[0],v[1])
        pg.endShape()
   
    # return a sequence [x,y] for vertex at index
    # no rotation in this version
    def getVertex(self,index):
        return [self.x + self.radius*math.cos(index*self.I),
                self.y + self.radius* math.sin(index*self.I)]
           
   
   
   


# In[2]:

# create the window
pg.size(500,500)


# In[10]:

# an example triangle
poly3 = Polygon(pg.width/2,pg.height/2,3, 100)
poly3.plot()


# In[8]:

# an example square
poly4 = Polygon(pg.width/2,pg.height/2,4, 100)
poly4.plot()


# In[11]:

# polygons on top of each other from size 3 to size 99
pg.background(255)
pg.noFill()
for i in range(3,100):
    # change the amount of red by a little bit each time
    pg.fill(i*255//100,0,0,3) # (Red,Green,Blue,Alpha)
    # create a polygon object with i sides and radius 100 in the
    # center of the window
    poly = Polygon(pg.width/2,pg.height/2,i, 100)
    # plot it
    poly.plot()
    # sleep for 0.1 seconds
    time.sleep(0.1)


# In[ ]:


# In[ ]:

#

Explanation / Answer

//* These are the header files used for this program. <iostream.h> and <conio.h> are the header files used for input and out functions. <graphics.h> and <math.h> are the header files used to support the drawing of polygon and angle calculation.*//

# include <iostream.h>

# include <conio.h>

# include <graphics.h>

# include <math.h>

char IncFlag;

//* Hard coded the value for polygon*//

int PolygonPoints[4][2] =

{{20,110},{120,110},{120,210},{20,210}};

int RefX = 10;

int RefY = 100;

void PolyLine()         //*Function to draw the polygon with reference to X and Y coordinates *//

{

int Cnt;

cleardevice();

line(0,240,640,240);

line(320,0,320,480);

for (Cnt=0; Cnt<4; Cnt++)

{

line(PolygonPoints[Cnt][0],PolygonPoints[Cnt][1],

PolygonPoints[(Cnt+1)%4][0],PolygonPoints[(Cnt+1)%4][1]);

}

}

void Translate(int Direction)

{

int Cnt;

for (Cnt=0; Cnt<4; Cnt++)

{

PolygonPoints[Cnt][0] += Direction*RefX;

PolygonPoints[Cnt][1] -= Direction*RefY;

}

}

void Rotate()        //*Function to rotate the polygon. The polygon rotates on the click of keyboard*//

{

float Angle;

int Cnt;

int Tx,Ty;

Translate(-1);

cout<<endl;

Angle = 30.0*(22.0/7.0)/180.0;          //Angle*PI/180*//

for (Cnt=0; Cnt<4; Cnt++)

{

Tx = PolygonPoints[Cnt][0];

Ty = PolygonPoints[Cnt][1];

PolygonPoints[Cnt][0] = (Tx - 320)*cos(Angle) -

(Ty - 240)*sin(Angle) + 320;

PolygonPoints[Cnt][1] = (Tx - 320)*sin(Angle) +

(Ty - 240)*cos(Angle) + 240;

}

Translate(1);

}

void main()

{

int gDriver = DETECT, gMode;

int iCnt;

initgraph(&gDriver, &gMode, "C:\TC\BGI");

for (iCnt=0; iCnt<4; iCnt++)

{

PolygonPoints[iCnt][0] += 320;

PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];

}

PolyLine();

getch();

Rotate();

PolyLine();

getch();

}

If you dosent want to hard code the value to draw polygon you can ask user to input the values which can be used to draw polygon using cout and cin.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Updated Program with class Polygon

# include <iostream.h>           //* Header filese *//
# include <conio.h>
# include <graphics.h>
# include <math.h>

int PolygonPoints[4][2];       //*Initialising Polygon points and X & Y coordinates*//

int RefX = 10;
int RefY = 100;
char IncFlag;

class Polygon               //* Class Polygon created with call to all function *//
{
public:
void PolyLine(void);
void Translate(int direction);
void Rotate(void);
Polygon()
{
cout << endl;
}
private: int direction;
};

void Polygon :: PolyLine()       //* Function to draw Polygon *//
{
int iCnt;
cleardevice() ;
line(0,240,640,240);
line(320,0,320,480);

for (iCnt=0; iCnt<4; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%4][0],PolygonPoints[(iCnt+1)%4][1]);
}
}

void Polygon :: Translate(int Direction)
{
int iCnt;
for (iCnt=0; iCnt<4; iCnt++)
{
PolygonPoints[iCnt][0] += Direction*RefX;
PolygonPoints[iCnt][1] -= Direction*RefY;
}
}

void Polygon :: Rotate()               //* Function to rotate polygon *//
{
float Angle;
int iCnt;
int Tx,Ty;
Translate(-1);
cout<<endl;
Angle = 30.0*(22.0/7.0)/180.0;
for (iCnt=0; iCnt<4; iCnt++)
{
Tx = PolygonPoints[iCnt][0];
Ty = PolygonPoints[iCnt][1];
PolygonPoints[iCnt][0] = (Tx - 320)*cos(Angle) -
(Ty - 240)*sin(Angle) + 320;
PolygonPoints[iCnt][1] = (Tx - 320)*sin(Angle) +
(Ty - 240)*cos(Angle) + 240;
}
Translate(1);
}


int main()
{
Polygon p1;
cout<<"Please enter the four set of coordinate points:";
int i=0,j=0;
for (i=0;i<4;i++)
{
for(j=0;j<2;j++)
{
cin >> PolygonPoints[i][j];
}
}

int gDriver = DETECT, gMode;
int iCnt;
initgraph(&gDriver, &gMode, "C:\TURBOC3\BGI");
for (iCnt=0; iCnt<4; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 -PolygonPoints[iCnt][1];
}
p1.PolyLine();
getch();
p1.Rotate();
p1.PolyLine();
getch();
return 0;
}