#include<graphics.h>
#include<iostream>usingnamespacestd;classPolygon{intpo[1][10],edges;public:Polygon(){edges=0;po[1][10]=0;}voidsetedge();voidsetpo(int);intgetedata();intgetx(int);intgety(int);};classDrawPolygon:publicPolygon{public:voidlines(int,int,int,int);}p;voidDrawPolygon::lines(intx0,inty0,intxEnd,intyEnd){line(x0,y0,xEnd,yEnd);}voidPolygon::setedge(){cout<<"\nEnter the number of edges of polygon ";cin>>edges;}voidPolygon::setpo(intindex){cout<<"Enter the coordinate x"<<index+1<<" ";cin>>po[0][index];cout<<"Enter the coordinate y"<<index+1<<" ";cin>>po[1][index];}intPolygon::getedata(){returnedges;}intPolygon::getx(intindex){returnpo[0][index];}intPolygon::gety(intindex){returnpo[1][index];}intmain(){p.setedge();Polygon();intch,ch1,ch2;intx0,y0,xEnd,yEnd;intgd,gm;gd=DETECT;ch=p.getedata()-1;for(inti=0;i<=ch;i++){p.setpo(i);}initgraph(&gd,&gm,NULL);for(inti=0;i<ch;i++){x0=p.getx(i);y0=p.gety(i);xEnd=p.getx(i+1);yEnd=p.gety(i+1);p.lines(x0,y0,xEnd,yEnd);}x0=xEnd;y0=yEnd;xEnd=p.getx(0);yEnd=p.gety(0);p.lines(x0,y0,xEnd,yEnd);delay(7000);return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp -lgraph kodingwindow@kw:~$ ./a.out
Enter the number of edges of polygon 5
Enter the coordinate x1 100
Enter the coordinate y1 100
Enter the coordinate x2 200
Enter the coordinate y2 100
Enter the coordinate x3 200
Enter the coordinate y3 200
Enter the coordinate x4 100
Enter the coordinate y4 200
Enter the coordinate x5 150
Enter the coordinate y5 150
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.