计算机图形学Bresenham完整算法画直线椭圆和圆.docx
- 文档编号:27214401
- 上传时间:2023-06-28
- 格式:DOCX
- 页数:18
- 大小:17.27KB
计算机图形学Bresenham完整算法画直线椭圆和圆.docx
《计算机图形学Bresenham完整算法画直线椭圆和圆.docx》由会员分享,可在线阅读,更多相关《计算机图形学Bresenham完整算法画直线椭圆和圆.docx(18页珍藏版)》请在冰豆网上搜索。
计算机图形学Bresenham完整算法画直线椭圆和圆
#include
#include
#include"stdio.h"
intm_PointNumber=0;//动画时绘制点的数目
intm_DrawMode=1;//绘制模式1DDA算法画直线
//2中点Bresenham算法画直线
//3改进Bresenham算法画直线
//4八分法绘制圆
//5四分法绘制椭圆
//绘制坐标线
voidDrawCordinateLine(void)
{
inti=-250;
//坐标线为黑色
glColor3f(0.0f,0.0f,0.0f);
glBegin(GL_LINES);
for(i=-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();
}
//绘制一个点,这里用一个正方形表示一个点
voidputpixel(GLsizeix,GLsizeiy)
{
glRectf(10*x,10*y,10*x+10,10*y+10);
}
///////////////////////////////////////////////////////////////////
//DDA画线算法//
////
////
////
///////////////////////////////////////////////////////////////////
voidDDACreateLine(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum)
{
//设置颜色
glColor3f(1.0f,0.0f,0.0f);
//对画线动画进行控制
if(num==1)
printf("DDA画线算法:
各点坐标\n");
elseif(num==0)
return;
//画线算法的实现
GLsizeidx,dy,epsl,k;
GLfloatx,y,xIncre,yIncre;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
if(abs(dx)>abs(dy))epsl=abs(dx);
elseepsl=abs(dy);
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=%d\n",x,y,(int)(x+0.5),(int)(y+0.5));
break;
}
x+=xIncre;
y+=yIncre;
if(x>=25||y>=25)break;
}
}
///////////////////////////////////////////////////////////////////
//中点Bresenham算法画直线(0<=k<=1)//
////
////
////
///////////////////////////////////////////////////////////////////
voidBresenhamLine(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum)
{
glColor3f(1.0f,0.0f,0.0f);
if(num==1)
printf("中点Bresenham算法画直线各点坐标及判别式的值\n");
elseif(num==0)
return;
//画线算法的实现
GLsizeip=0;
GLfloatUpIncre,DownIncre,x,y,d,k,dx,dy;
if(x0>x1){
x=x1;x1=x0;x0=x;
y=y1;y1=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=%d\n",x,y);
break;
}
p++;
x++;
if(d<0){
y++;
d+=UpIncre;
}
elsed+=DownIncre;
}
}
if(k>1){
d=dy-2*dx;
UpIncre=2*dy-2*dx;
DownIncre=-2*dx;
while(y<=y1){
putpixel(x,y);
if(p>=num-1){
printf("x=%d,y=%d\n",x,y);
break;
}
p++;
y++;
if(d<0){
x++;
d+=UpIncre;
}
elsed+=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){
printf("x=%d,y=%d\n",x,y);
break;
}
p++;
x++;
if(d>0){
y--;
d+=DownIncre;
}
elsed+=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=%d\n",x,y);
break;
}
p++;
y--;
if(d<0){
x++;
d+=UpIncre;
}
elsed+=DownIncre;
}
}
}
///////////////////////////////////////////////////////////////////
//改进的Bresenham算法画直线(0<=k<=1)//
////
//x1,y1终点坐标//
////
///////////////////////////////////////////////////////////////////
voidBresenham2Line(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum)
{
glColor3f(1.0f,0.0f,0.0f);
GLsizeix,y,dx,dy,e,k;
if(num==1)
printf("改进的Bresenham算法画直线各点坐标及判别式的值\n");
elseif(num==0)
return;
//画线算法的实现
GLsizeip=0;
if(x0>x1){
x=x1;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=%d\n",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>=num-1){
printf("x=%d,y=%d\n",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=%d\n",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>=num-1){
printf("x=%d,y=%d\n",x,y);
break;
}
p++;
y--;
e=e-2*dx;
if(e<0){
x++;
e=e-2*dy;
}
}
}
}
/////////////////////////////////////////////////////////
//Bresenham算法画圆//
////
////
////
/////////////////////////////////////////////////////////
voidCirclePoint(GLsizeix,GLsizeiy)
{putpixel(x,y);
putpixel(x,-y);
putpixel(y,-x);
putpixel(-y,-x);
putpixel(-x,-y);
putpixel(-x,y);
putpixel(-y,x);
putpixel(y,x);
}
voidBresenhamCircle(GLsizeix,GLsizeiy,GLsizeiR,GLsizeinum)
{
glColor3f(1.0f,0.0f,0.0f);
GLsizeid;
x=0;y=R;d=1-R;
if(num==1)
printf("Bresenham算法画圆:
各点坐标及判别式的值\n");
elseif(num==0)
return;
while(x<=y){
CirclePoint(x,y);
if(x>=num-1){
printf("x=%d,y=%d,d=%d\n",x,y,d);
break;
}
if(d<0)d+=2*x+3;
else{
d+=2*(x-y)+5;
y--;
}
x++;
}
}
voidBresenham2Circle(GLsizeia,GLsizeib,GLsizeinum)
{
glColor3f(1.0f,0.0f,0.0f);
if(num==1)
printf("Bresenham算法画椭圆:
各点坐标及判别式的值\n");
elseif(num==0)
return;
GLsizeix,y;
floatd1,d2;
x=0;y=b;
d1=b*b+a*a*(-b+0.5);
putpixel(x,y);putpixel(-x,-y);
putpixel(-x,y);putpixel(x,-y);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 图形学 Bresenham 完整 算法 直线 椭圆