-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_LineDrawingAlgorithms.txt
More file actions
164 lines (126 loc) · 3 KB
/
Copy path1_LineDrawingAlgorithms.txt
File metadata and controls
164 lines (126 loc) · 3 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
Aim : Draw a line using DDA and Bresenham Algorithm
Code :
#include<graphics.h>
#include<bits/stdc++.h>
using namespace std;
#define callInitgraph int gd=DETECT,gm; initgraph(&gd,&gm,NULL);
class LineDrawingAlgorithms
{
public :
int X1,Y1,X2,Y2;
void getCoordinates();
/*line alignment on the terminal screen is exact mirror image of our regular co-ordinate axes,hence we are taking the absolute values of dx and dy in calculation of DDALine and Bresenham Algorithm;*/
void DDALine();
void bresenhamLine();
};
void LineDrawingAlgorithms::bresenhamLine()
{
callInitgraph;
float xin, yin, dx, dy, step, x, y, pk;
dx = X2 - X1;
dy = Y2 - Y1;
x = X1;
y = Y1;
//this is the case when slope(m) < 1
if(abs(dx) > abs(dy))
{
putpixel(x,y,5);
pk = (2 * abs(dy)) - abs(dx);
for(int i = 0; i < abs(dx) ; i++)
{
x = x + 1;
if(pk < 0)
pk = pk + (2 * abs(dy));
else
{
y = y + 1;
pk = pk + (2 * abs(dy)) - (2 * abs(dx));
}
putpixel(x,y,5);
delay(10);
}
delay(30);
}
else
{
//when slope is greater than 1
putpixel(x,y,5);
pk = (2 * abs(dx)) - abs(dy);
for(int i = 0; i < abs(dy) ; i++)
{
y = y + 1;
if(pk < 0)
pk = pk + (2 * abs(dx));
else
{
x = x + 1;
pk = pk + (2 * abs(dx)) - (2 *abs(dy));
}
putpixel(x,y,5);
delay(10);
}
delay(30);
}
}
void LineDrawingAlgorithms::DDALine()
{
callInitgraph;
float xin, yin, dx, dy, step, x, y, pk;
dx = X2 - X1;
dy = Y2 - Y1;
if(abs(dx) > abs(dy))
step = abs(dx);
else
step = abs(dy);
xin = (dx) / step;
yin = (dy) / step;
x = X1;
y = Y1;
putpixel((x), (y),GREEN);
for(int i = 1; i <= step; ++i)
{
x = x + xin;
y = y + yin;
putpixel(round(x), round(y),GREEN);
}
delay(1000);
}
void LineDrawingAlgorithms::getCoordinates()
{
cout << "\nEnter the First Point Co-ordinates : " << endl;
cin >> X1 >> Y1;
cout << "\nEnter the Second Point Co-ordinates : "<< endl;
cin >> X2 >> Y2;
}
int main()
{
LineDrawingAlgorithms l1;
int n,c;
do
{
c = 1;
cout << "\n(1). Enter Co-ordinates";
cout << "\n(2). DDALine";
cout << "\n(3). BRESENHAM";
cout << "\n(4). To Exit";
cin>>n;
switch(n)
{
case 1:
l1.getCoordinates();
break;
case 2:
l1.DDALine();
closegraph();
break;
case 3:
l1.bresenhamLine();
closegraph();
break;
case 4:
c = 0;
break;
}
}while(c);
return 0;
}