Skip to content

Commit a33808b

Browse files
authored
Update dda line drawing algorithm.cpp
1 parent 18d1190 commit a33808b

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

dda line drawing algorithm.cpp

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
1-
#include <iostream>
1+
// LAB task #1 DDA line drawing algorithm
22
#include <graphics.h>
3+
#include <iostream>
34
#include <cmath>
4-
//#define Round(a) (a + 0.5) Roundoff function can be represented as this also
55

6-
inline int Round(float a)
7-
{
8-
return static_cast<int>(a);
9-
}
6+
using namespace std;
107

11-
void DDA(float x1, float y1, float x2, float y2)
8+
9+
void ddalinedraw(int x1,int y1,int x2,int y2)
1210
{
13-
float x, y, dx, dy, step, xincr, yincr;
14-
dx = x1-x2;
15-
dy = y1-y2;
16-
17-
if(abs(dx) > abs(dy))
18-
step = abs(dx);
19-
else
20-
step = abs(dy);
21-
22-
xincr = dx/static_cast<float>(dx);
23-
yincr = dy/static_cast<float>(dy);
24-
putpixel(Round(x), Round(y), WHITE);
25-
26-
int i = 1;
27-
while(i <= step)
28-
{
29-
x += xincr;
30-
y += yincr;
31-
i++;
32-
delay(10);
33-
putpixel(Round(x), Round(y), WHITE);
34-
}
11+
12+
13+
int dx=x2-x1,dy=y2-y1,step;
14+
float xincr,yincr,x=x1,y=y1;
15+
16+
if(abs(dx)>abs(dy))
17+
step = abs(dx);
18+
else
19+
step = abs(dy);
20+
21+
xincr = dx/(float)step;
22+
yincr = dy/(float)step;
23+
putpixel(round(x),round(y),WHITE);
24+
25+
int i=1;
26+
while(i<=step){
27+
x=x+xincr;
28+
y=y+yincr;
29+
i++;
30+
delay(10);
31+
putpixel(round(x),round(y),WHITE);
32+
}
33+
3534
}
3635

36+
37+
38+
3739
int main()
3840
{
39-
initwindow(600, 600, "DDA");
40-
//question points
41-
DDA(100, 100, 200, 250);
42-
DDA(300, 100, 100, 250);
43-
DDA(300, 400, 100, 250);
44-
getch();
45-
closegraph();
41+
initwindow(500,500,"DDA Line Drawing Algorithm");
42+
ddalinedraw(100,100,200,250);
43+
ddalinedraw(300,100,100,250);
44+
ddalinedraw(300,400,100,250);
45+
getch();
46+
closegraph();
47+
return 0;
4648
}
49+

0 commit comments

Comments
 (0)