《图形学》实验四:中点Bresenham算法画直线

开发环境:

VC++6.0,OpenGL

实验内容:

使用中点Bresenham算法画直线。

实验结果:

代码:

  1 //中点Bresenham算法生成直线
  2 #include <gl/glut.h>
  3 #include <math.h>
  4 
  5 #define WIDTH    500        //窗口宽度
  6 #define HEIGHT    500        //窗口高度
  7 #define DRAWLINE1 MidpointBresenham(100,200,200,100);    //画直线
  8 #define DRAWLINE2 MidpointBresenham(200,100,450,400);    //画直线
  9 
 10 #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")        //取消控制台
 11 
 12 void Init()    //初始化
 13 {
 14     glClearColor(1.0f,1.0f,1.0f,1.0f);    //设置背景颜色,完全不透明
 15     glColor3f(1.0f,0.0f,0.0f);    //设置画笔颜色
 16     
 17     glMatrixMode(GL_PROJECTION);            //设置投影
 18     gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT);    //设置投影区域
 19 }
 20 
 21 void MidpointBresenham(int x0,int y0,int x1,int y1)    //中点Bresenham算法画线
 22 {
 23     int dx,dy,d,UpIncre,DownIncre,x,y;
 24     if(x0>x1){
 25         x=x1;x1=x0;x0=x;
 26         y=y1;y1=y0;y0=y;
 27     }
 28     x = x0,y = y0;
 29     dx = x1-x0;
 30     dy = y1-y0;
 31     if(dy>0&&dy<=dx){    //0<k<=1
 32         d = dx-2*dy;
 33         UpIncre = 2*dx-2*dy;
 34         DownIncre = -2*dy;
 35         while(x<=x1){
 36             glBegin(GL_POINTS);
 37             glVertex2i(x,y);
 38             glEnd();
 39             x++;
 40             if(d<0){
 41                 y++;
 42                 d+=UpIncre;
 43             }
 44             else
 45                 d+=DownIncre;
 46         }
 47     }
 48     else if((dy>=(-dx))&&dy<=0) //-1<=k<=0
 49     { 
 50         d=dx-2*dy;
 51         UpIncre=-2*dy;
 52         DownIncre=-2*dx-2*dy;
 53         while(x<=x1)
 54         {
 55             glBegin(GL_POINTS);
 56             glVertex2i(x,y);
 57             glEnd();
 58             x++;
 59             if(d>0)
 60             {  
 61                 y--;
 62                 d+=DownIncre;
 63             }   
 64             else d+=UpIncre;
 65         }   
 66     }
 67     else if(dy<(-dx)) //k<-1
 68     {
 69         d=-dy-2*dx;
 70         UpIncre=2*dx+2*dy;
 71         DownIncre=2*dx;
 72         while(y>=y1)
 73         {
 74             glBegin(GL_POINTS);
 75             glVertex2i(x,y);
 76             glEnd();
 77             y--;
 78             if(d<0)
 79             {  
 80                 x++;
 81                 d-=UpIncre;
 82             }   
 83             else d-=DownIncre;
 84         }   
 85     }
 86     
 87     else //k>1和k不存在
 88     { 
 89         d=dy-2*dx;
 90         UpIncre=2*dy-2*dx;
 91         DownIncre=-2*dx;
 92         while(y<=y1)
 93         {
 94             glBegin(GL_POINTS);
 95             glVertex2i(x,y);
 96             glEnd();
 97             y++;
 98             if(d<0)
 99             {  
100                 x++;
101                 d+=UpIncre;
102             }   
103             else d+=DownIncre;
104         }   
105     }
106 }
107 
108 void Display()    //显示函数
109 {
110     glClear(GL_COLOR_BUFFER_BIT);    //清空颜色堆栈
111     
112     DRAWLINE1    //画直线
113     DRAWLINE2    //画直线
114     
115     glFlush();    //清空缓冲区指令
116 }
117 
118 int main(int argc,char** argv)
119 {
120     glutInit(&argc,argv);
121     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);    //初始化显示模式
122     glutInitWindowSize(WIDTH,HEIGHT);    //设置窗口尺寸
123     glutInitWindowPosition(200,100);    //设置窗口位置
124     glutCreateWindow("画直线");    //创建窗口
125     
126     glutDisplayFunc(Display);    //注册显示函数
127     Init();        //初始化
128     glutMainLoop();    //进入程序循环
129     return 0;
130 }

 

Freecode : www.cnblogs.com/yym2013

posted @ 2015-07-03 17:26  Freecode#  阅读(1955)  评论(0编辑  收藏  举报