C++ program to draw a convex polygon with programmable edges
kw.cpp
#include<graphics.h>
#include<iostream>usingnamespacestd;classline{protected:intx1,x2,y1,y2;public:voiddrawl(){floatx,y,dx,dy,step;inti;dx=abs(x2-x1);dy=abs(y2-y1);if(dx>=dy){step=dx;}elsestep=dy;dx=(x2-x1)/step;dy=(y2-y1)/step;x=x1+0.5;y=y1+0.5;i=1;while(i<=step){putpixel(x,y,15);x=x+dx;y=y+dy;i=i+1;}putpixel(x,y,15);}};classpolygon:publicline{inta[10][2];public:voidsetpts(inti){intj,k;for(j=0;j<i;j++){cout<<"Enter the coordinate x"<<j<<" ";cin>>a[j][0];cout<<"Enter the coordinate y"<<j<<" ";cin>>a[j][1];}}voiddrawpoly(inti){intj;x1=a[0][0];y1=a[0][1];x2=a[1][0];y2=a[1][1];line::drawl();for(j=1;j<i-1;j++){x1=a[j][0];y1=a[j][1];x2=a[j+1][0];y2=a[j+1][1];line::drawl();}x1=a[j][0];y1=a[j][1];x2=a[0][0];y2=a[0][1];line::drawl();}};intmain(){intn,gd=DETECT,gm;cout<<"Enter the number of sides of polygon ";cin>>n;polygonP;P.setpts(n);initgraph(&gd,&gm,NULL);P.drawpoly(n);delay(5000);return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp -lgraph kodingwindow@kw:~$ ./a.out
Enter the number of sides of polygon 6
Enter the coordinate x0 100
Enter the coordinate y0 100
Enter the coordinate x1 150
Enter the coordinate y1 150
Enter the coordinate x2 150
Enter the coordinate y2 200
Enter the coordinate x3 100
Enter the coordinate y3 250
Enter the coordinate x4 50
Enter the coordinate y4 200
Enter the coordinate x5 50
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.