Skip to content

Commit 3e7d631

Browse files
authored
Add files via upload
1 parent fee6767 commit 3e7d631

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// C++ program for DDA Line Drawing algorithm.
2+
3+
#include <iostream>
4+
#include <cmath>
5+
#include <graphics.h>
6+
7+
// Function to implement DDA Line Drawing Algorithm
8+
void drawLineDDA(int x1, int y1, int x2, int y2) {
9+
int dx = x2 - x1;
10+
int dy = y2 - y1;
11+
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
12+
13+
float xIncrement = dx / (float)steps;
14+
float yIncrement = dy / (float)steps;
15+
16+
float x = x1;
17+
float y = y1;
18+
19+
putpixel(round(x), round(y), WHITE);
20+
21+
for (int i = 1; i <= steps; i++) {
22+
x += xIncrement;
23+
y += yIncrement;
24+
putpixel(round(x), round(y), WHITE);
25+
}
26+
}
27+
28+
int main() {
29+
int gd = DETECT, gm;
30+
initgraph(&gd, &gm, NULL);
31+
32+
int x1, y1, x2, y2;
33+
34+
std::cout << "Enter the coordinates of the first point (x1, y1): ";
35+
std::cin >> x1 >> y1;
36+
37+
std::cout << "Enter the coordinates of the second point (x2, y2): ";
38+
std::cin >> x2 >> y2;
39+
40+
drawLineDDA(x1, y1, x2, y2);
41+
42+
delay(5000); // Display the line for 5 seconds
43+
closegraph();
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)