|
1 |
| -#include <iostream> |
| 1 | +// LAB task #1 DDA line drawing algorithm |
2 | 2 | #include <graphics.h>
|
| 3 | +#include <iostream> |
3 | 4 | #include <cmath>
|
4 |
| -//#define Round(a) (a + 0.5) Roundoff function can be represented as this also |
5 | 5 |
|
6 |
| -inline int Round(float a) |
7 |
| -{ |
8 |
| - return static_cast<int>(a); |
9 |
| -} |
| 6 | +using namespace std; |
10 | 7 |
|
11 |
| -void DDA(float x1, float y1, float x2, float y2) |
| 8 | + |
| 9 | +void ddalinedraw(int x1,int y1,int x2,int y2) |
12 | 10 | {
|
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 | + |
35 | 34 | }
|
36 | 35 |
|
| 36 | + |
| 37 | + |
| 38 | + |
37 | 39 | int main()
|
38 | 40 | {
|
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; |
46 | 48 | }
|
| 49 | + |
0 commit comments