-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDDA.cpp
More file actions
67 lines (53 loc) · 1.5 KB
/
DDA.cpp
File metadata and controls
67 lines (53 loc) · 1.5 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
#include <stdio.h>
#include <graphics.h>
// Function for finding absolute value
int abs(int n)
{
return ((n > 0) ? n : (n * (-1)));
}
// DDA Function for line generation
void DDA(float X0, float Y0, float X1, float Y1)
{
float h = (float)getwindowheight();
float w = (float)getwindowwidth();
// calculate dx & dy
float dx = X1 - X0;
float dy = Y1 - Y0;
// calculate steps required for generating pixels
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
// calculate increment in x & y for each steps
float Xinc = dx / (float)steps;
float Yinc = dy / (float)steps;
// Put pixel for each step
float X = X0;
float Y = Y0;
for (int i = 0; i <= steps; i++)
{
putpixel(X + w / 2, (h / 2) - Y, GREEN); // put pixel at (X,Y)
X += Xinc; // increment in x at each step
Y += Yinc; // increment in y at each step
}
}
// Driver program
int main()
{
int gd = DETECT, gm;
// Initialize graphics function
initgraph(&gd, &gm, "");
float h = (float)getwindowheight();
float w = (float)getwindowwidth();
DDA(-w / 2, 0, w / 2, 0);
DDA(0, h / 2, 0, -h / 2);
// Input values
int p[2][18] =
{
{50, 50, 50, 60, 60, 60, 60, 50, 60, 200, 200, 200, 200, 60, 60, 120, 120, 60},
{50, 200, 200, 200, 200, 50, 50, 50, 200, 200, 200, 120, 120, 120, 120, 160, 160, 200},
};
for (int i = 0; i < 18; i = i + 2)
{
DDA(p[0][i], p[1][i], p[0][i + 1], p[1][i + 1]);
}
getch();
return 0;
}