Brsenham画线

环境:VS2019+下载EasyX(需要用到画图的库)
程序:

include

include <graphics.h>

include<conio.h>

using namespace std;
int main()
{
int x1=100, x2=300, y1=100, y2=300;
initgraph(400, 400, SHOWCONSOLE);
setbkcolor(WHITE);
cleardevice();
int x, y, dx, dy, p;
x = x1;
y = y1;
dx = x2 - x1;
dy = y2 - y1;
p = 2 * dy - dx;
for (; x <= x2; x++) {
setcolor(BLACK);//设置绘图的颜色
line(x, y, x, y);//画点(x,y)
if (p >= 0) {
y++;
p += 2 * (dy - dx);
}
else {
p += 2 * dy;
}
dx = x2 - x;
dy = y2 - y;
p = 2 * dy - dx;
}
setcolor(RED);//设置绘图的颜色
line(x1, y1, x2, y2);//画线
_getch();
closegraph;//关闭绘图程序界面
//return 0;
}

上一篇:路径规划 - A*算法及其优化


下一篇:L1-048 矩阵A乘以B