You are on page 1of 3

EXPERIMENT 4

Program To Construct A Polygon Using DDA


Algorithm
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int i,j;

int round(float value)


{
int vall=value;
int v1=value*100;
v1%100;
if(v1>=50)
return vall+1;
else
return vall;
}
void linedda(int x1,int x2,int y1,int y2)
{
int dx=x2-x1;
int dy=y2-y1,steps;
float xin,yin,x=x1,y=y1;
if(abs(dx)>abs(dy))
steps=abs(dy);
xin=dx/(float)steps;
yin=dy/(float)steps;
putpixel(round(x),round(y),CYAN);
for(int k=0;k<steps;k++)
{
x=x+xin;
y=y+yin;
putpixel(round(x),round(y),CYAN);
}
}
void main()
{
clrscr();
int n,a[100],b[100];
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
do
{
cout<<endl<<"enter the no of sides of polygon";
cin>>n;
if(n<=2)
cout<<"enter the sides>=3";
}
while(n<=2);
for(int i=0;i<n;i++)
{
cout<<endl<<"enter the"<<i+1<<"coordinates";
cin>>a[i]>>b[i];
}
for(int j=0;j<n-1;j++)
{
int x1=a[j];
int x2=a[j+1];
int y1=b[j];
int y2=b[j+1];
linedda(x1,x2,y1,y2);
if(x2==a[n-1])
{
x1=a[0];
x2=a[j+1];
y1=b[0];
y2=b[j+1];
linedda(x1,x2,y1,y2);
}
}
getch();
closegraph();
}

OUTPUT:
enter the no of sides of polygon4
enter the1 coordinates200
200
enter the2 coordinates250
210
enter the3 coordinates300
260
enter the4 coordinates350
300

You might also like