编译环境VS2017+EasyX
#include "stdafx.h"
#include"graphics.h" void DDALine(int x0, int y0, int x1, int y1);
void main()
{ int x0, y0, x1, y1;
initgraph(, ); //初始化图形界面,窗口分辨率设置为640*480
MOUSEMSG m; // 定义鼠标消息 while (true)
{ flag: //重置开始
m = GetMouseMsg();// 获取一次鼠标消息
switch (m.uMsg)
{
case WM_LBUTTONDOWN:x0 = m.x; y0 = m.y; //鼠标第一次按下时,得到坐标(x0,y0)
while (true)
{
m = GetMouseMsg();// 获取一条鼠标消息
switch (m.uMsg)
{
case WM_LBUTTONDOWN:x1 = m.x; y1 = m.y;//鼠标第二次按下时,得到坐标(x1,y1)
DDALine(x0, y0, x1, y1);
goto flag; //跳回到flag处进行重置
break;
case WM_RBUTTONUP:
return; // 按鼠标右键退出程序
}
}
break;
case WM_RBUTTONUP:
return; // 按鼠标右键退出程序
}
} } void DDALine(int x0, int y0, int x1, int y1) {
int dx = x1 - x0, dy = y1 - y0, k;
double x = x0, y = y0, xIncre, yIncre, espl;
espl = abs(dy);
if (abs(dx) > abs(dy))
espl = abs(dx);
xIncre = 1.0*dx / espl;
yIncre = 1.0*dy / espl;
for (k = ; k < espl; k++)
{
putpixel((int)x, (int)y,WHITE);
x += xIncre;
y += yIncre;
}
}
实验结果