Skip to content

Commit 8b80b42

Browse files
DDA and Bresenham‘s algorithm
Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel class and Use function overloading.
1 parent 162dfa4 commit 8b80b42

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

Diff for: linedraw.cpp

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel
2+
class and Use function overloading.*/
3+
#include<iostream>
4+
#include<graphics.h>
5+
using namespace std;
6+
class pixel
7+
{
8+
public:
9+
int x0,x1,y0,y1;
10+
void getdata();
11+
};
12+
void pixel ::getdata()
13+
{
14+
cout<<" Enter the starting coordinates of line "<<"\n";
15+
cin>>x0>>y0;
16+
cout<<" Enter the end coordinates of line "<<"\n";
17+
cin>>x1>>y1;
18+
}
19+
class linedraw :public pixel
20+
{
21+
public:
22+
void line();
23+
void line(int c);
24+
int sign(int a,int b);
25+
};
26+
void linedraw ::line()
27+
{
28+
float dx,dy,x,y,steps;
29+
dx=x1-x0;
30+
dy=y1-y0;
31+
if(fabs(dx)>fabs(dy))
32+
steps=fabs(dx);
33+
else
34+
steps=fabs(dy);
35+
dx=dx/steps;
36+
dy=dy/steps;
37+
x=x0;
38+
y=y0;
39+
for(int i=0;i<steps;i++)
40+
{
41+
putpixel(x,y,RED);
42+
x+=dx;
43+
y+=dy;
44+
}
45+
46+
}
47+
int linedraw :: sign(int a,int b)
48+
{
49+
if(a<b)
50+
return 1;
51+
else
52+
return -1;
53+
}
54+
55+
void linedraw ::line(int c)
56+
{
57+
float x,y,dx,dy,s1,s2,temp,inter,e;
58+
x=x0 ;
59+
y=y0;
60+
dx=x1-x0;
61+
dy=y1-y0;
62+
s1=sign(x0,x1);
63+
s2=sign(y0,y1);
64+
if(dy>dx)
65+
{
66+
temp=dx;
67+
dx=dy;
68+
dy=temp;
69+
inter=1;
70+
71+
}
72+
else
73+
{
74+
inter =0;
75+
}
76+
e=2*dy-dx;
77+
for(int i=0;i<dx;i++)
78+
{
79+
putpixel(x,y,RED);
80+
while(e>0)
81+
{
82+
if(inter==1)
83+
{
84+
x=x+s1;
85+
86+
}
87+
else
88+
{
89+
y=y+s2;
90+
}
91+
e=e-2*dx;
92+
93+
94+
}
95+
if(inter==1)
96+
{
97+
y=y+s2;
98+
}
99+
else
100+
{
101+
x=x+s1;
102+
}
103+
e=e+2*dy;
104+
105+
}
106+
107+
108+
}
109+
110+
int main()
111+
{
112+
int gdriver=DETECT,gmode;
113+
initgraph(&gdriver,&gmode,NULL);
114+
115+
linedraw obj;
116+
setbkcolor(WHITE);
117+
int x;
118+
119+
do
120+
{
121+
cout<<"1.DDA\n2.Breshanam\n3.Exit"<<endl;
122+
cout<<"Enter your choice:";
123+
cin>>x;
124+
switch(x)
125+
{
126+
case 1 :
127+
obj.getdata();
128+
obj.line();
129+
break;
130+
case 2 :
131+
obj.getdata();
132+
obj.line(2);
133+
break;
134+
case 3 : exit(1);
135+
}
136+
}
137+
while(x=3);
138+
return 0;
139+
}
140+
141+
142+
143+
144+
145+

0 commit comments

Comments
 (0)