-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidpointLine.cpp
More file actions
93 lines (84 loc) · 1.33 KB
/
MidpointLine.cpp
File metadata and controls
93 lines (84 loc) · 1.33 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "stdafx.h"
#include "MidpointLine.h"
MidpointLine::MidpointLine()
{
}
MidpointLine::~MidpointLine()
{
}
void MidpointLine::SetDc(CDC * dc)
{
this->pdc = dc;
}
void MidpointLine::Moveline(CPoint start, CPoint end)
{
int x0 = start.x, x1 = end.x, y0 = start.y, y1 = end.y;
int a, b, d2, x, y, flag = 0;
if (abs(x1 - x0) < abs(y1 - y0))
{
swap_value(x0,y0);
swap_value(x1, y1);
flag = 1;
}
if (x0 > x1) {//保证x0<x1,方便判别斜率
swap_value(x0, x1);
swap_value(y0, y1);
}
a = y0 - y1;
b = x1 - x0;
d2 = 2*a + b;//摆脱小数点,提高效率
if (y0 < y1) {//k>0
x = x0; y = y0;
this->pdc->SetPixel(x,y,RGB(0,0,0));
while (x < x1)
{
if (d2 < 0)
{
x++;
y++;
d2 =d2+ 2*a + 2*b;
}
else {
x++;
d2 =d2+ 2 * a;
}
if(flag)//|k|>1
this->pdc->SetPixel(y, x, RGB(0, 0, 0));
else
this->pdc->SetPixel(x, y, RGB(0, 0, 0));
}
}
else {//k<0
x = x1;
y = y1;
this->pdc->SetPixel(x, y, RGB(0, 0, 0));
while (x >x0)
{
if (d2 < 0)
{
x--;
y++;
d2 = d2-2 * a + 2 * b;
}
else {
x--;
d2 =d2- 2 * a;
}
if (flag)//|k|>1
this->pdc->SetPixel(y, x, RGB(0, 0, 0));
else
this->pdc->SetPixel(x, y, RGB(0, 0, 0));
}
}
}
void MidpointLine::swap_value(int & a, int & b)
{
/*
a ^= b;
b ^= a;
a ^= b;
*/
int temp = a;
a = b;
b = temp;
}