-
Notifications
You must be signed in to change notification settings - Fork 1
/
blda.cpp
80 lines (65 loc) · 1.23 KB
/
blda.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Write a program to implement Bresenham's line drawing algorithm.
*
*/
#include <cmath>
#include <cstdlib>
#include <graphics.h>
#include <iostream>
using namespace std;
void bresenhamLine(int x0, int y0, int x1, int y1, int val)
{
if (x0 == x1 && y0 == y1)
{
putpixel(x1, y1, val);
}
else
{
int dx = x1 - x0;
int dy = y1 - y0;
float m = float(dy) / (float)(dx);
if (m >= 1 || m <= 0)
{
cout << "ERROR: Slope must be between 0 and 1." << endl;
exit(1);
}
int d = 2 * dy - dx;
int del_E = 2 * dy;
int del_NE = 2 * (dy - dx);
int x = x0;
int y = y0;
putpixel(x, y, val);
while (x < x1)
{
if (d <= 0)
{
d += del_E;
x += 1;
}
else
{
d += del_NE;
x += 1;
y += 1;
}
putpixel(x, y, val);
}
}
return;
}
int main(void)
{
int x0, y0, x1, y1;
cout << "Enter Left Endpoint (x0 y0): ";
cin >> x0 >> y0;
cout << "Enter Right Endpoint (x1 y1): ";
cin >> x1 >> y1;
cout << "Drawing Line..." << endl;
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
bresenhamLine(x0, y0, x1, y1, WHITE);
delay(5e4);
closegraph();
cout << "Finished..." << endl;
return 0;
}