算法代码实现

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
81
82
83
84
85
86
void Line_by_Bresenham(HDC hdc, int x0, int y0, int xEnd, int yEnd) {
int x, y, dx, dy, e;
dx = xEnd - x0;
dy = yEnd - y0;
x = x0;
y = y0;
if ((dx >= 0 && dy >= 0) || (dx <= 0 && dy <= 0)) {
if ((dx <= 0) || (dx == 0 && dy < 0)) {
dx = -dx;
x = xEnd;
dy = -dy;
y = yEnd;
}
if (dy < dx) {
e = -dx;
for (int i = 0; i < dx; i++) {
SetPixel(hdc, x, y, RGB(0, 0, 0));
x++;
e = e + 2 * dy;
if (e >= 0) {
y++;
e = e - dx - dx;
}
}
}
else {
e = -dy;
for (int i = 0; i < dy; i++) {
SetPixel(hdc, x, y, RGB(0, 0, 0));
y++;
e = e + dx + dx;
if (e >= 0) {
x++;
e = e - dy - dy;
}
}
}
}
else {
int tempx, tempy;
if (dx < 0) {
tempx = -dx;
tempy = dy;
}
if (dy < 0) {
tempx = dx;
tempy = -dy;
}
if (tempx > tempy) {
if (dx < 0) {
dx = -dx;
x = xEnd;
dy = -dy;
y = yEnd;
}
e = -dx;
for (int i = 0; i < dx; i++) {
SetPixel(hdc, x, y, RGB(0, 0, 0));
x++;
e = e - dy - dy;
if (e >= 0) {
y--;
e = e - dx - dx;
}
}
}
else {
if (dy < 0) {
dx = -dx;
x = xEnd;
dy = -dy;
y = yEnd;
}
e = -dy;
for (int i = 0; i < dy; i++) {
SetPixel(hdc, x, y, RGB(0, 0, 0));
y++;
e = e - dx - dx;
if (e >= 0) {
x--;
e = e - dy - dy;
}
}
}
}
}