You are on page 1of 3

PROGRAM 1:program to implement DDA algorithm, for drawing a line .

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gdriver=DETECT,gmode;
float x1,x2,y1,y2,m,x,y;
initgraph (&gdriver,&gmode,D:\\TC\\BGI);
cout<<enter the co-ordinates of first point;
cin>>x1>>y1;
cout<<enter the co-ordinates of second point;
cin>>x2>>y2;
clrscr();
cout<<***************THE RESULTANT LINE IS DRAWN BELOW***********;
x=x1;
y=y1;
m=(y2-y1)/(x2-x1);
while(x<=x2||y<=y2)
{
If(m>1)
{
putpixel(x,y,50);
y=y+1;

x=x+(1/m);
}
else
{
putpixel(x,y,50);
y=y+m;
x=x+1;
}
}
getch();
closegraph();
}

PROGRAM 2:program to implement Bresenham line drawing algorithm.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"D:\\TC\\BGI");
int x1,x2,y1,y2,p,i;
cout<<"*****************BRESENHAM ALGORITHM**********************\n";
cout<<"Enter the starting point:";
cin>>x1>>y1;

cout<<"\nEnter the end point:";


cin>>x2>>y2;
p=2*(y2-y1)-(x2-x1);
for(i=x1;i<x2;i++)
{
if(p<0)
{
x1=x1+1;
y1=y1;
p=p+2*(y2-y1);
putpixel(x1,y1,12);
}
else
{
x1=x1+1;
y1=y1+1;
p=p+2*(y2-y1)-2*(x2-x1);
putpixel(x1,y1,12);
}
}
getch();
closegraph();
}

You might also like