-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathlinedraw.cpp
145 lines (127 loc) · 1.9 KB
/
linedraw.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel
class and Use function overloading.*/
#include<iostream>
#include<graphics.h>
using namespace std;
class pixel
{
public:
int x0,x1,y0,y1;
void getdata();
};
void pixel ::getdata()
{
cout<<" Enter the starting coordinates of line "<<"\n";
cin>>x0>>y0;
cout<<" Enter the end coordinates of line "<<"\n";
cin>>x1>>y1;
}
class linedraw :public pixel
{
public:
void line();
void line(int c);
int sign(int a,int b);
};
void linedraw ::line()
{
float dx,dy,x,y,steps;
dx=x1-x0;
dy=y1-y0;
if(fabs(dx)>fabs(dy))
steps=fabs(dx);
else
steps=fabs(dy);
dx=dx/steps;
dy=dy/steps;
x=x0;
y=y0;
for(int i=0;i<steps;i++)
{
putpixel(x,y,RED);
x+=dx;
y+=dy;
}
}
int linedraw :: sign(int a,int b)
{
if(a<b)
return 1;
else
return -1;
}
void linedraw ::line(int c)
{
float x,y,dx,dy,s1,s2,temp,inter,e;
x=x0 ;
y=y0;
dx=x1-x0;
dy=y1-y0;
s1=sign(x0,x1);
s2=sign(y0,y1);
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
inter=1;
}
else
{
inter =0;
}
e=2*dy-dx;
for(int i=0;i<dx;i++)
{
putpixel(x,y,RED);
while(e>0)
{
if(inter==1)
{
x=x+s1;
}
else
{
y=y+s2;
}
e=e-2*dx;
}
if(inter==1)
{
y=y+s2;
}
else
{
x=x+s1;
}
e=e+2*dy;
}
}
int main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,NULL);
linedraw obj;
setbkcolor(WHITE);
int x;
do
{
cout<<"1.DDA\n2.Breshanam\n3.Exit"<<endl;
cout<<"Enter your choice:";
cin>>x;
switch(x)
{
case 1 :
obj.getdata();
obj.line();
break;
case 2 :
obj.getdata();
obj.line(2);
break;
case 3 : exit(1);
}
}
while(x=3);
return 0;
}