编译环境VS2017+EasyX
1 #include "stdafx.h" 2 #include"graphics.h" 3 4 void DDALine(int x0, int y0, int x1, int y1); 5 void main() 6 { 7 8 int x0, y0, x1, y1; 9 initgraph(640, 480); //初始化图形界面,窗口分辨率设置为640*480 10 MOUSEMSG m; // 定义鼠标消息 11 12 while (true) 13 { 14 15 flag: //重置开始 16 m = GetMouseMsg();// 获取一次鼠标消息 17 switch (m.uMsg) 18 { 19 case WM_LBUTTONDOWN:x0 = m.x; y0 = m.y; //鼠标第一次按下时,得到坐标(x0,y0) 20 while (true) 21 { 22 m = GetMouseMsg();// 获取一条鼠标消息 23 switch (m.uMsg) 24 { 25 case WM_LBUTTONDOWN:x1 = m.x; y1 = m.y;//鼠标第二次按下时,得到坐标(x1,y1) 26 DDALine(x0, y0, x1, y1); 27 goto flag; //跳回到flag处进行重置 28 break; 29 case WM_RBUTTONUP: 30 return; // 按鼠标右键退出程序 31 } 32 } 33 break; 34 case WM_RBUTTONUP: 35 return; // 按鼠标右键退出程序 36 } 37 } 38 39 } 40 41 void DDALine(int x0, int y0, int x1, int y1) 42 43 { 44 int dx = x1 - x0, dy = y1 - y0, k; 45 double x = x0, y = y0, xIncre, yIncre, espl; 46 espl = abs(dy); 47 if (abs(dx) > abs(dy)) 48 espl = abs(dx); 49 xIncre = 1.0*dx / espl; 50 yIncre = 1.0*dy / espl; 51 for (k = 0; k < espl; k++) 52 { 53 putpixel((int)x, (int)y,WHITE); 54 x += xIncre; 55 y += yIncre; 56 } 57 }
实验结果