码迷,mamicode.com
首页 > 其他好文 > 详细

图形学_画线算法(DDA、Bresenham)

时间:2014-04-27 20:51:40      阅读:777      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   com   img   java   div   http   javascript   style   初始化   

1. DDA算法实现直线绘制(需先安装easyx,百度下载即可)

 

mamicode.com,码迷
 1 #include "easyx.h"
 2 #include "math.h"
 3 #include "windows.h"
 4 #include "stdio.h"
 5 #include "stdlib.h"
 6 #include "conio.h"
 7 #include "graphics.h"
 8 
 9 void dda(int x1,int y1,int x2,int y2,int color)
10 {
11     int steps;
12     double xin,yin,dx,dy,x,y;
13     dx = x2 - x1;
14     dy = y2 - y1;
15     if(fabs(dx) > fabs(dy))
16         steps = fabs(dx);
17     else
18         steps = fabs(dy);
19     xin = (double)(dx/steps);
20     yin = (double)(dy/steps);  //possible loss of data
21     x = x1;
22     y = y1;
23     putpixel(x,y,color);
24     for(int i = 0;i <= steps;i ++)
25     {
26         x = x + xin;
27         y = y + yin;
28         putpixel(x,y,color);
29     }
30 
31 }
32 
33 int main()
34 { 
35    //硬件测试,将gd装入图形驱动器,gm置入最大图形模式。 
36     int gd=DETECT,gm,x1,y1,x2,y2,color;
37     printf("input x1,y1,x2,y2,color is:");
38     //输入直线参数。
39     scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&color);
40     //图形初始化
41     initgraph(&gd,&gm,"c:\\tc");
42     //设置兰背景。
43     setbkcolor(BLUE);
44     cleardevice();
45     dda(x1,y1,x2,y2,color);
46     getch(); 
47     closegraph(); 
48     
49     return 0;
50 }
mamicode.com,码迷

 

2.Bresenham算法绘制直线

 

mamicode.com,码迷
 1 #include "easyx.h"
 2 #include "math.h"
 3 #include "windows.h"
 4 #include "stdio.h"
 5 #include "stdlib.h"
 6 #include "conio.h"
 7 #include "graphics.h"
 8 
 9 void Bresenham(int x1,int y1,int x2,int y2,int color)
10 {
11     int x,y,d,dx,dy;
12     dx = x2 - x1;
13     dy = y2 - y1;
14     d = -dx; //误差
15     x = x1;
16     y = y1;
17 
18     while(x <= x2)
19     {
20         putpixel(x,y,color);
21         x = x + 1;
22         if(d >= 0)
23         {
24             y = y + 1;
25             d = d - 2 * dx;
26         }
27         d = d + 2 * dy;
28     }
29 }
30 
31 int main()
32 { 
33    //硬件测试,将gd装入图形驱动器,gm置入最大图形模式。 
34     int gd=DETECT,gm,x1,y1,x2,y2,color;
35     printf("input x1,y1,x2,y2,color is:");
36     //输入直线参数。
37     scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&color);
38     //图形初始化
39     initgraph(&gd,&gm,"c:\\tc");
40     //设置兰背景。
41     setbkcolor(BLUE);
42     cleardevice();
43     Bresenham(x1,y1,x2,y2,color);
44     getch(); 
45     closegraph(); 
46     
47     return 0;
48 }
mamicode.com,码迷

 

 

3.改进的Bresenham画线算法

改进后的算法可用于绘制任意斜率,任意方向的直线

mamicode.com,码迷
  1 #include "easyx.h"
  2 #include "math.h"
  3 #include "windows.h"
  4 #include "stdio.h"
  5 #include "stdlib.h"
  6 #include "conio.h"
  7 #include "graphics.h"
  8 
  9 int Bresenham(int x1,int y1,int x2,int y2,int color)
 10 {
 11     int tempx,tempy;
 12     if(x1 > x2)
 13     {
 14         tempx = x1;
 15         x1 = x2;
 16         x2 = tempx;
 17         tempy = y1;
 18         y1 = y2;
 19         y2 = tempy;
 20     }
 21 
 22     int x,y,dx,dy;
 23     double e,k;
 24     dx = x2 - x1;
 25     dy = y2 - y1;
 26     
 27     if(dx == 0)
 28     {
 29         if(y1 > y2)
 30         {
 31             tempy = y1;
 32             y1 = y2;
 33             y2 = tempy;
 34         }
 35         for(int p = 0;p <= fabs(dy);p++)
 36         {
 37             putpixel(x1,y1,color);
 38             y1 = y1+1;
 39         }
 40         return 1;
 41     }
 42 
 43     if(dy == 0)
 44     {
 45         for(int q = 0;q <= fabs(dx);q++)
 46         {
 47             putpixel(x1,y1,color);
 48             x1 = x1 + 1;
 49         }
 50         return 1;
 51     }
 52 
 53     k = (double)dy/dx;
 54     e = 0;
 55     x = x1;
 56     y = y1;
 57 
 58 
 59     for(int i = 0;i <= dx;i++)
 60     {
 61         if(k > 0)
 62         {
 63             putpixel(x,y,color);
 64             x++;
 65             e = e + k;
 66             if(e - int(e) >= 0.5)
 67             {
 68                 y = y + int(e) + 1;
 69                 e = e - int(e) - 1;
 70             }            
 71             else
 72             {
 73                 y = y + int(e);
 74                 e = e - int(e);
 75             }
 76         }
 77         
 78         else
 79         {
 80             putpixel(x,y,color);
 81             x++;
 82             e = e + k;
 83             if(e - int(e) <= -0.5)
 84             {
 85                 y = y + int(e) - 1;
 86                 e = e - int(e) + 1;
 87             }
 88             else
 89             {
 90                 y = y + int(e);
 91                 e = e - int(e);
 92             }
 93             
 94         }
 95         
 96         
 97     }
 98     return 1;
 99 }
100 
101 int main()
102 { 
103    //硬件测试,将gd装入图形驱动器,gm置入最大图形模式。 
104     int gd=DETECT,gm,x1,y1,x2,y2,color;
105     printf("input x1,y1,x2,y2,color is:");
106     //输入直线参数。
107     scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&color);
108     //图形初始化
109     initgraph(&gd,&gm,"c:\\tc");
110     //设置兰背景。
111     setbkcolor(BLUE);
112     cleardevice();
113     int tempx,tempy;
114     if(x1 > x2)
115     {
116         tempx = x1;
117         x1 = x2;
118         x2 = tempx;
119         tempy = y1;
120         y1 = y2;
121         y2 = tempy;
122     }
123     Bresenham(x1,y1,x2,y2,color);
124     getch(); 
125     closegraph(); 
126     
127     return 0;
128 }
mamicode.com,码迷

 

4.Bresenham画线算法绘制任意多边形

 

mamicode.com,码迷
  1 #include "easyx.h"
  2 #include "math.h"
  3 #include "windows.h"
  4 #include "stdio.h"
  5 #include "stdlib.h"
  6 #include "conio.h"
  7 #include "graphics.h"
  8 
  9 int Bresenham(int x1,int y1,int x2,int y2,int color)
 10 {
 11     int tempx,tempy;
 12     if(x1 > x2)
 13     {
 14         tempx = x1;
 15         x1 = x2;
 16         x2 = tempx;
 17         tempy = y1;
 18         y1 = y2;
 19         y2 = tempy;
 20     }
 21 
 22     int x,y,dx,dy;
 23     double e,k;
 24     dx = x2 - x1;
 25     dy = y2 - y1;
 26     
 27     if(dx == 0)
 28     {
 29         if(y1 > y2)
 30         {
 31             tempy = y1;
 32             y1 = y2;
 33             y2 = tempy;
 34         }
 35         for(int p = 0;p <= fabs(dy);p++)
 36         {
 37             putpixel(x1,y1,color);
 38             y1 = y1+1;
 39         }
 40         return 1;
 41     }
 42 
 43     if(dy == 0)
 44     {
 45         for(int q = 0;q <= fabs(dx);q++)
 46         {
 47             putpixel(x1,y1,color);
 48             x1 = x1 + 1;
 49         }
 50         return 1;
 51     }
 52 
 53     k = (double)dy/dx;
 54     e = 0;
 55     x = x1;
 56     y = y1;
 57 
 58 
 59     for(int i = 0;i <= dx;i++)
 60     {
 61         if(k > 0)
 62         {
 63             putpixel(x,y,color);
 64             x++;
 65             e = e + k;
 66             if(e - int(e) >= 0.5)
 67             {
 68                 y = y + int(e) + 1;
 69                 e = e - int(e) - 1;
 70             }            
 71             else
 72             {
 73                 y = y + int(e);
 74                 e = e - int(e);
 75             }
 76         }
 77         
 78         else
 79         {
 80             putpixel(x,y,color);
 81             x++;
 82             e = e + k;
 83             if(e - int(e) <= -0.5)
 84             {
 85                 y = y + int(e) - 1;
 86                 e = e - int(e) + 1;
 87             }
 88             else
 89             {
 90                 y = y + int(e);
 91                 e = e - int(e);
 92             }
 93             
 94         }
 95         
 96         
 97     }
 98     return 1;
 99 }
100 
101 int main()
102 { 
103    //硬件测试,将gd装入图形驱动器,gm置入最大图形模式。 
104     int gd=DETECT,gm;
105     int x[100],y[100],color;
106     int n;   //代表边的个数
107     printf("请输入多边形边的个数: ");
108     scanf("%d",&n);
109     printf("\n请依次输入多边形每一个点的坐标:\n");
110     for(int i = 0;i < n;i ++)
111         scanf("%d%d",&x[i],&y[i]);
112     scanf("%d",&color);
113    
114     //图形初始化
115     initgraph(&gd,&gm,"c:\\tc");
116     //设置兰背景。
117     setbkcolor(BLUE);
118     cleardevice();
119 
120     for(int j = 1;j < n;j ++)
121        Bresenham(x[j - 1],y[j - 1],x[j],y[j],color);
122     
123     Bresenham(x[0],y[0],x[n-1],y[n-1],color);
124 
125     getch(); 
126     closegraph(); 
127     
128     return 0;
129 }
mamicode.com,码迷

 

 

 

 

 

图形学_画线算法(DDA、Bresenham),码迷,mamicode.com

图形学_画线算法(DDA、Bresenham)

标签:blog   class   code   com   img   java   div   http   javascript   style   初始化   

原文地址:http://www.cnblogs.com/baichenjia/p/3694692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!