C++ menu-driven program to draw the convex polygons
kw.cpp
#include<graphics.h>
#include<iostream>usingnamespacestd;classpolygon{private:intx1,y1,x2,y2,x,y,k;floatdx,dy,step,xinc,yinc;public:voidSRtri(intx1,inty1,intx2,inty2){dx=x2-x1;dy=y2-y1;if(abs(dx)>=abs(dy)){step=abs(dx);}else{step=abs(dy);}xinc=dx/step;yinc=dy/step;x=x1;y=y1;putpixel(x,y,WHITE);for(k=0;k<step;k++){x=x+xinc;y=y+yinc;putpixel(x,y,WHITE);}}};intmain(){intx1,x2,y1,y2,ch,x3,y3;intgd=DETECT,gm;polygonp;do{cout<<"———————————————————————————————————————————";cout<<"\nProgram to draw the convex polygons";cout<<"\n———————————————————————————————————————————";cout<<"\n1.Square\n2.Rectangle\n3.Triangle\n4.Exit";cout<<"\n———————————————————————————————————————————";cout<<"\nEnter your choice ";cin>>ch;switch(ch){case1:cout<<"Enter the x1 coordinate ";cin>>x1;cout<<"Enter the y1 coordinate ";cin>>y1;cout<<"Enter the x2 coordinate ";cin>>x2;cout<<"Enter the y2 coordinate ";cin>>y2;initgraph(&gd,&gm,NULL);p.SRtri(x1,y1,x1,y2);p.SRtri(x1,y1,x2,y1);p.SRtri(x1,y2,x2,y2);p.SRtri(x2,y1,x2,y2);delay(3000);closegraph();break;case2:cout<<"Enter the x1 coordinate ";cin>>x1;cout<<"Enter the y1 coordinate ";cin>>y1;cout<<"Enter the x2 coordinate ";cin>>x2;cout<<"Enter the y2 coordinate ";cin>>y2;initgraph(&gd,&gm,NULL);p.SRtri(x1,y1,x1,y2);p.SRtri(x1,y1,x2,y1);p.SRtri(x1,y2,x2,y2);p.SRtri(x2,y1,x2,y2);delay(3000);closegraph();break;case3:cout<<"Enter the x1 coordinate ";cin>>x1;cout<<"Enter the y1 coordinate ";cin>>y1;cout<<"Enter the x2 coordinate ";cin>>x2;cout<<"Enter the y2 coordinate ";cin>>y2;cout<<"Enter the x3 coordinate ";cin>>x3;cout<<"Enter the y3 coordinate ";cin>>y3;initgraph(&gd,&gm,NULL);p.SRtri(x1,y1,x2,y2);p.SRtri(x2,y2,x3,y3);p.SRtri(x3,y3,x1,y1);delay(3000);closegraph();break;case4:exit(0);default:cout<<"\nWrong Choice Entered...Please Try Again\n";}}while(ch!=4);return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp -lgraph kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Program to draw the convex polygons
———————————————————————————————————————————
1.Square
2.Rectangle
3.Triangle
4.Exit
———————————————————————————————————————————
Enter your choice 1
Enter the x1 coordinate 30
Enter the y1 coordinate 30
Enter the x2 coordinate 150
Enter the y2 coordinate 150
———————————————————————————————————————————
Program to draw the convex polygons
———————————————————————————————————————————
1.Square
2.Rectangle
3.Triangle
4.Exit
———————————————————————————————————————————
Enter your choice 2
Enter the x1 coordinate 30
Enter the y1 coordinate 30
Enter the x2 coordinate 150
Enter the y2 coordinate 100
———————————————————————————————————————————
Program to draw the convex polygons
———————————————————————————————————————————
1.Square
2.Rectangle
3.Triangle
4.Exit
———————————————————————————————————————————
Enter your choice 3
Enter the x1 coordinate 100
Enter the y1 coordinate 20
Enter the x2 coordinate 20
Enter the y2 coordinate 100
Enter the x3 coordinate 180
Enter the y3 coordinate 100
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.