计算机图形学Bresenham完整算法 画直线、椭圆和圆_第1页
计算机图形学Bresenham完整算法 画直线、椭圆和圆_第2页
计算机图形学Bresenham完整算法 画直线、椭圆和圆_第3页
计算机图形学Bresenham完整算法 画直线、椭圆和圆_第4页
计算机图形学Bresenham完整算法 画直线、椭圆和圆_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include <>#include <gl/>#include ""int m_PointNumber = 0; /动画时绘制点的数目int m_DrawMode = 1; /绘制模式 1 DDA算法画直线 / 2 中点Bresenham算法画直线 / 3 改进Bresenham算法画直线 / 4 八分法绘制圆 / 5 四分法绘制椭圆/绘制坐标线void DrawCordinateLine(void)int i = -250 ;/坐标线为黑色glColor3f(0.0f, 0.0f ,0.0f);glBegin(GL_LINES); for (i

2、=-250;i<=250;i=i+10)glVertex2f(float)(i), -250.0f);glVertex2f(float)(i), 250.0f);glVertex2f(-250.0f, (float)(i);glVertex2f(250.0f, (float)(i);glEnd();/绘制一个点,这里用一个正方形表示一个点void putpixel(GLsizei x, GLsizei y)glRectf(10*x,10*y,10*x+10,10*y+10);/DDA画线算法 / / / / / / void DDACreateLine(GLsizei x0, GLsiz

3、ei y0, GLsizei x1, GLsizei y1, GLsizei num)/设置颜色glColor3f(1.0f,0.0f,0.0f);/对画线动画进行控制if(num = 1)printf("DDA画线算法:各点坐标n");else if(num=0)return;/画线算法的实现 GLsizei dx,dy,epsl,k;GLfloat x,y,xIncre,yIncre;dx = x1-x0;dy = y1-y0;x = x0;y = y0;if(abs(dx) > abs(dy) epsl = abs(dx);else epsl = abs(dy)

4、;xIncre = (float)dx / epsl ;yIncre = (float)dy / epsl ;for(k = 0; k<=epsl; k+)putpixel(int)(x+0.5), (int)(y+0.5);if (k>=num-1) printf("x=%f , y=%f,取整后 x=%d,y=%dn", x, y, (int)(x+0.5),(int)(y+0.5);break;x += xIncre;y += yIncre;if(x >= 25 | y >= 25) break;/中点Bresenham算法画直线(0<=

5、k<=1) / / / / / / void BresenhamLine(GLsizei x0, GLsizei y0, GLsizei x1, GLsizei y1, GLsizei num)glColor3f(1.0f,0.0f,0.0f);if(num = 1)printf("中点Bresenham算法画直线各点坐标及判别式的值n");else if(num=0)return;/画线算法的实现GLsizei p=0;GLfloatUpIncre,DownIncre,x,y,d,k,dx,dy;if(x0>x1)x=x1;x1=x0;x0=x;y=y1;y1

6、=y0;y0=y;x=x0;y=y0;dx=x1-x0;dy=y1-y0;k=dy/dx;if(k>=0&&k<=1) d=dx-2*dy; UpIncre=2*dx-2*dy; DownIncre=-2*dy; while(x<=x1) putpixel(x,y); if (p>=num-1) printf("x=%d,y=%dn", x, y);break; p+; x+; if(d<0) y+; d+=UpIncre; else d+=DownIncre; if(k>1) d=dy-2*dx; UpIncre=2*d

7、y-2*dx; DownIncre=-2*dx; while(y<=y1) putpixel(x,y);if (p>=num-1) printf("x=%d,y=%dn", x, y); break; p+;y+; if(d<0) x+; d+=UpIncre; else d+=DownIncre; if(k<0&&k>=-1) d=dx-2*dy; UpIncre=-2*dy; DownIncre=-2*dx-2*dy; while(x<=x1) putpixel(x,y); if (p>=num-1) print

8、f("x=%d,y=%dn", x, y);break; p+; x+; if(d>0) y-; d+=DownIncre; else d+=UpIncre; if(k<-1) d=-dy-2*dx; UpIncre=-2*dx-2*dy; DownIncre=-2*dx; while(y>=y1) putpixel(x,y);if (p>=num-1) printf("x=%d,y=%dn", x, y); break; p+;y-; if(d<0) x+; d+=UpIncre;else d+=DownIncre; /改

9、进的Bresenham算法画直线(0<=k<=1) / / / x1,y1 终点坐标 / / / void Bresenham2Line(GLsizei x0, GLsizei y0, GLsizei x1, GLsizei y1, GLsizei num)glColor3f(1.0f,0.0f,0.0f);GLsizei x,y,dx,dy,e,k;if(num = 1)printf("改进的Bresenham算法画直线各点坐标及判别式的值n");else if(num=0)return;/画线算法的实现GLsizei p=0;if(x0>x1)x=x1

10、;x1=x0;x0=x;y=y1;y1=y0;y0=y;dx=x1-x0;dy=y1-y0;k=dy/dx;if(k>=0&&k<=1) e=-dx;x=x0;y=y0; while(x<=x1) putpixel(x,y); if (p>=num-1) printf("x=%d,y=%dn", x, y); break; p+; x+; e=e+2*dy; if(e>0) y+; e=e-2*dx; if(k>1) e=-dy;x=x0;y=y0; while(y<=y1) putpixel(x,y); if (p

11、>=num-1) printf("x=%d,y=%dn", x, y); break; p+; y+; e=e+2*dx; if(e>0) x+; e=e-2*dy; if(k<0&&k>=-1) e=-dx;x=x0;y=y0; while(x<=x1) putpixel(x,y); if (p>=num-1) printf("x=%d,y=%dn", x, y); break; p+; x+; e=e+2*dy; if(e<0) y-; e=e+2*dx; if(k<-1) e=-dy;

12、x=x0;y=y0; while(y>=y1) putpixel(x,y); if (p>=num-1) printf("x=%d,y=%dn", x, y); break; p+; y-; e=e-2*dx; if(e<0) x+; e=e-2*dy; /Bresenham算法画圆 / / / / / /void CirclePoint(GLsizei x,GLsizei y) putpixel(x,y); putpixel(x,-y); putpixel(y,-x); putpixel(-y,-x); putpixel(-x,-y); putpixel

13、(-x,y); putpixel(-y,x); putpixel(y,x);void BresenhamCircle(GLsizei x, GLsizei y, GLsizei R, GLsizei num)glColor3f(1.0f,0.0f,0.0f);GLsizei d;x=0;y=R;d=1-R;if(num = 1)printf("Bresenham算法画圆:各点坐标及判别式的值n");else if(num=0)return;while(x<=y)CirclePoint(x,y); if (x>=num-1) printf("x=%d,y

14、=%d,d=%dn", x, y,d); break; if(d<0)d+=2*x+3;else d+=2*(x-y)+5; y-;x+;void Bresenham2Circle(GLsizei a,GLsizei b,GLsizei num)glColor3f(1.0f,0.0f,0.0f);if(num=1)printf("Bresenham算法画椭圆:各点坐标及判别式的值n");else if(num=0)return;GLsizei x,y;float d1,d2;x=0;y=b;d1=b*b+a*a*(-b+0.5);putpixel(x,y);

15、 putpixel(-x,-y);putpixel(-x,y);putpixel(x,-y);while(b*b*(x+1)<a*a*(y-0.5)if (x>=num-1) printf("x=%d,y=%d,d1=%dn", x, y,d1); break; if(d1<=0)d1+=b*b*(2*x+3);x+;elsed1+=b*b*(2*x+3)+a*a*(-2*y+2);x+;y-;putpixel(x,y); putpixel(-x,-y);putpixel(-x,y);putpixel(x,-y);/while上半部分)+a*a*(y-1)

16、*(y-1)-a*a*b*b;while(y>0)if (x>=num-1) printf("x=%d,y=%d,d2=%dn", x, y,d2); break; if(d2<=0)d2+=b*b*(2*x+2)+a*a*(-2*y+3);x+;y-;elsed2+=a*a*(-2*y+3);y-;putpixel(x,y); putpixel(-x,-y);putpixel(-x,y);putpixel(x,-y);/初始化窗口void Initial(void) / 设置窗口颜色为蓝色 glClearColor(0.0f, 0.0f, 1.0f, 1

17、.0f);/ 窗口大小改变时调用的登记函数void ChangeSize(GLsizei w, GLsizei h)if(h = 0)h = 1;/ 设置视区尺寸 glViewport(0,0, w, h);/ 重置坐标系统glMatrixMode(GL_PROJECTION);glLoadIdentity();/ 建立修剪空间的范围 if (w <= h) glOrtho (-250.0f, 250.0f, -250.0f, 250.0f*h/w, 1.0, -1.0); else glOrtho (-250.0f, 250.0f*w/h, -250.0f, 250.0f, 1.0,

18、-1.0);/ 在窗口中绘制图形void ReDraw(void)/用当前背景色填充窗口glClear(GL_COLOR_BUFFER_BIT);/画出坐标线DrawCordinateLine();switch(m_DrawMode)case 1:DDACreateLine(0,0,20,15,m_PointNumber);break;case 2:BresenhamLine(0,0,-20,15,m_PointNumber);break;case 3:Bresenham2Line(1,1,8,6,m_PointNumber);break;case 4:BresenhamCircle(0,0,20,m_PointNumber);break;case 5:Bresenham2Circle(10,8,m_PointNumber);default:break; glFlush();/设置时间回调函数void TimerFunc(int value)if(m_PointNumber = 0)value = 1;m_PointNumber = value;glutPostRedisplay();glutTimerFunc(500, TimerFunc, value+1);/设置键盘回调函数void Keyboard(unsigned char key, int x, int y) if (

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论