Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Parth1906
GitHub Repository: Parth1906/SPPU-2019-Pattern-SE-COMP-Computer-Graphics-Practicals
Path: blob/main/4. DDA and Bresenhams Line Drawing Algorithm.cpp
724 views
1
/*
2
4. Write a c++ program for drawing a line using
3
DDA and Bresahnams Line Drawing Algorithm
4
*/
5
6
7
#include<iostream.h>
8
#include<graphics.h>
9
#include<math.h>
10
11
int sign(int x)
12
{
13
if(x<0)
14
return -1;
15
else if(x>0)
16
return 1;
17
else
18
return 0;
19
}
20
void bline(int x1,int y1,int x2,int y2,int col)
21
{
22
int dx,dy,e,x,y,i=1;
23
dx=x2-x1;
24
dy=y2-y1;
25
x=x1;
26
y=y1;
27
e=2*dy-dx;
28
while(i<=dx)
29
{
30
while(e>=0)
31
{
32
y++;
33
e=e-2*dx;
34
}
35
36
x++;
37
e=e+2*dy;
38
putpixel(x,y,col);
39
i++;
40
}
41
}
42
void ddaline(int x1,int y1,int x2,int y2,int
43
col)
44
{
45
int x,y,len,i;
46
float dx,dy;
47
if(x1==x2 && y1==y2)
48
putpixel(x1,y1,col);
49
else
50
{
51
dx=abs(x2-x1);
52
dy=abs(y2-y1);
53
if(dx&gt;dy)
54
len=dx;
55
else
56
len=dy;
57
dx=(x2-x1)/len;
58
dy=(y2-y1)/len;
59
x=x1+0.5*sign(dx);
60
y=y1+0.5*sign(dy);
61
i=1;
62
while(i<len)
63
{
64
putpixel(x,y,col);
65
x=x+dx;
66
y=y+dy;
67
i++;
68
}
69
}
70
}
71
int main()
72
73
{
74
int ch,col,x1,x2,y1,y2;
75
cout<<"\n------------MENU------------\n"
76
cout<<"1.USING DDA\n";
77
cout<<"2.Using Bresahnams\n";
78
cout<<"\nEnter your choice : \n";
79
cin>>ch;
80
cout<<"\nEnter points x1,y1,x2,y2 : \n";
81
cin>>x1>>y1>>x2>>y2;
82
cout<<"\nEnter colour 1-15 : \n";
83
cin>>col;
84
if(col>15||col<1)
85
col=1;
86
int gd=DETECT,gm;
87
initgraph(&gd,&gm,”c:\\turboc3\\bgi”);
88
switch(ch)
89
{
90
case 1:
91
ddaline(x1,y1,x2,y2,col);
92
ddaline(300,300,400,300,col);
93
ddaline(300,300,300,400,col);
94
ddaline(300,400,400,400,col);
95
ddaline(400,400,400,300,col);
96
break;
97
case 2:
98
bline(x1,y1,x2,y2,col);
99
ddaline(300,300,400,300,col);
100
ddaline(300,300,300,400,col);
101
ddaline(300,400,400,400,col);
102
ddaline(400,400,400,300,col);
103
break;
104
default :
105
cout<<"\nEnter valid choice :\n";
106
}
107
108
getch();
109
closegraph();
110
return 0;
111
}
112
113