//This program is to draw polygon using C
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>
#include<math.h>
void line(float x1,float y1,float x2,float y2);
void main()
{
int gdriver = DETECT, gmode,i,no;
float x[100],y[100];
initgraph(&gdriver, &gmode, "..\\bgi");
cout<<"how many sides you want";
cin>>no;
for(i=0;i<no;i++)
{
cout<<"enter x"<<i+1;
cin>>x[i];
cout<<"enter y"<<i+1;
cin>>y[i];
}
x[no]=x[0];
y[no]=y[0];
for(i=0;i<no;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
getch();
}
void line(float x1,float y1,float x2,float y2)
{
int i;
float sign,dx,dy;
if(abs(x2-x1)>abs(y2-y1))
sign=abs(x2-x1);
else
sign=abs(y2-y1);
dx=x2-x1;
dy=y2-y1;
for(i=0;i<=sign;i++)
{
putpixel(x1,y1,15);
x1+=dx/sign;
y1+=dy/sign;
}
}
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>
#include<math.h>
void line(float x1,float y1,float x2,float y2);
void main()
{
int gdriver = DETECT, gmode,i,no;
float x[100],y[100];
initgraph(&gdriver, &gmode, "..\\bgi");
cout<<"how many sides you want";
cin>>no;
for(i=0;i<no;i++)
{
cout<<"enter x"<<i+1;
cin>>x[i];
cout<<"enter y"<<i+1;
cin>>y[i];
}
x[no]=x[0];
y[no]=y[0];
for(i=0;i<no;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
getch();
}
void line(float x1,float y1,float x2,float y2)
{
int i;
float sign,dx,dy;
if(abs(x2-x1)>abs(y2-y1))
sign=abs(x2-x1);
else
sign=abs(y2-y1);
dx=x2-x1;
dy=y2-y1;
for(i=0;i<=sign;i++)
{
putpixel(x1,y1,15);
x1+=dx/sign;
y1+=dy/sign;
}
}