MFC实现直线算法

视频教学地址

DDA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void CTest1View::OnDda()
{
// TODO: 在此添加命令处理程序代码
int x0=0, y0=0, x1=300, y1=400, color = 255;
int dx, dy, cou, k;
CClientDC cc(this);
double x, y,increase_x,increase_y;
dx = abs(x0 - x1);
dy = abs(y0 - y1);
x = x0, y = y0;
cou = max(dx, dy);
increase_x = (double)dx / (double)cou;
increase_y = (double)dy / (double)cou;
for (int i=0;i<=cou;i++) {
cc.SetPixel((int)(x+0.5),(int)(y+0.5),color);
x += increase_x;
y += increase_y;
}

}

Bresenham

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
void CTest1View::Onbresenham()
{
// TODO: 在此添加命令处理程序代码
int x0 = 0, y0 = 0, x1 = 300, y1 = 600, color = 159;
int dx, dy, cou, k,f;
CClientDC cc(this);
int x, y, increase0, increase1;
dx = abs(x0 - x1);
dy = abs(y0 - y1);
f = 2 * dy - dx;
x = x0, y = y0;
cou = max(dx, dy);
increase0 = 2 * (dy - dx);
increase1 = 2 * dy;
for (int i = 0; i <= cou; i++) {
cc.SetPixel(x, y, color);
if (f < 0) {
f += increase1;
}
else {
y++;
f += increase0;
}
x++;
}
}