DDA画线算法
1 #include<stdio.h> 2 3 #include"graphics.h" 4 5 #include<math.h> 6 7 #include<stdlib.h> 8 9 10 11 void DDALine(int x0, int y0, int x1, int y1); 12 13 14 15 int main() 16 17 { 18 19 int gdriver = DETECT, gmode; 20 21 22 23 int x0, y0, x1, y1; 24 25 printf("please input the start point:\n"); 26 27 scanf_s("%d%d", &x0, &y0); 28 29 printf("please input the end point:\n"); 30 31 scanf_s("%d%d", &x1, &y1); 32 33 34 35 initgraph(&gdriver, &gmode, ""); //初始化图形界面 36 37 38 39 DDALine(x0, y0, x1, y1); 40 41 42 43 system("pause"); //暂停函数 44 45 46 47 closegraph(); //关闭图形界面 48 49 } 50 51 52 53 void DDALine(int x0, int y0, int x1, int y1) 54 55 { 56 57 int dx=x1-x0, dy=y1-y0,k; 58 59 double x = x0, y = y0, xIncre, yIncre, espl; 60 61 espl = abs(dy); 62 63 64 65 if (abs(dx) > abs(dy)) 66 67 espl = abs(dx); 68 69 70 71 xIncre = 1.0*dx / espl; 72 73 yIncre = 1.0*dy / espl; 74 75 76 77 for (k = 0; k < espl;k++) 78 79 { 80 81 putpixel((int)x, (int)y, RED); 82 83 x += xIncre; 84 85 y += yIncre; 86 87 } 88 89 }