Sem 4‎ > ‎CG LAB‎ > ‎

P2: WAP to draw line using DDA Algorithm

posted Dec 21, 2011, 9:29 AM by Neil Mathew


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Functions used:
 
1. putpixel()
2. outtextxy()
3. setbkcolor()
 
*/ 
 
 
 
#include <iostream.h>  //for cout,cin
#include <graphics.h>  //for putpixel()
#include <math.h>      //for round()
#include <conio.h>     //for getch(),clrscr()
 
void LINE(int x1,int y1,int x2,int y2)
{
int dx,dy;
float step;
float xincrement,yincrement;
float x,y;
 
 
dx=x2-x1;
dy=y2-y1;
 
if( abs(dx) > abs(dy) )
step=abs(dx);
else
step=abs(dy);
 
xincrement=dx/step;
yincrement=dy/step;
 
x=x1;
y=y1;
 
putpixel(x,y,15);
outtextxy(x-15,y-15,"START");
 
for(int i=1; i<=step; i++)
{
x+=xincrement;
y+=yincrement;
 
putpixel(x,y,15);
}
outtextxy(x+10,y+10,"END");
 
}
 
int main()
{
clrscr();
 
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
 
int x1,y1,x2,y2;
setbkcolor(DARKGRAY);
 
cout<<" Enter initial x and y coordinate: ";
cin>>x1>>y1;
 
cout<<"\n Enter final x and y coordinate: ";
cin>>x2>>y2;
 
cout<<"\n Press enter to draw line. ";
getch();
 
LINE(x1,y1,x2,y2);
 
getch();
 
closegraph();
 
return 2;
}


OUTPUT:




Comments