-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDA algorithm.cpp
More file actions
39 lines (29 loc) · 916 Bytes
/
DDA algorithm.cpp
File metadata and controls
39 lines (29 loc) · 916 Bytes
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
#include <graphics.h>
#include <iostream>
#include <cmath>
void drawLineDDA(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float Xinc = dx / (float)steps;
float Yinc = dy / (float)steps;
float X = x1;
float Y = y1;
// Draw pixels
for (int i = 0; i <= steps; i++) {
putpixel(round(X), round(Y), BLACK); // Draw pixel in Yellow
X += Xinc;
Y += Yinc;
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
setbkcolor(WHITE); // Set background color to White
cleardevice(); // Apply the background color
int x1 = 100, y1 = 100, x2 = 200, y2 = 210;
drawLineDDA(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}