计算机图形学实验C++代码.doc

上传人:b****2 文档编号:1646629 上传时间:2022-10-23 格式:DOC 页数:11 大小:70KB
下载 相关 举报
计算机图形学实验C++代码.doc_第1页
第1页 / 共11页
计算机图形学实验C++代码.doc_第2页
第2页 / 共11页
计算机图形学实验C++代码.doc_第3页
第3页 / 共11页
计算机图形学实验C++代码.doc_第4页
第4页 / 共11页
计算机图形学实验C++代码.doc_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

计算机图形学实验C++代码.doc

《计算机图形学实验C++代码.doc》由会员分享,可在线阅读,更多相关《计算机图形学实验C++代码.doc(11页珍藏版)》请在冰豆网上搜索。

计算机图形学实验C++代码.doc

一、bresenham算法画直线

#include

#include

#include

voiddraw_pixel(intix,intiy)

{

glBegin(GL_POINTS);

glVertex2i(ix,iy);

glEnd();

}

voidBresenham(intx1,inty1,intxEnd,intyEnd)

{

intdx=abs(xEnd-x1),dy=abs(yEnd-y1);

intp=2*dy-dx;

inttwoDy=2*dy,twoDyMinusDx=2*dy-2*dx;

intx,y;

if(x1>xEnd)

{

x=xEnd;y=yEnd;

xEnd=x1;

}

else

{

x=x1;

y=y1;

}

draw_pixel(x,y);

while(x

{

x++;

if(p<0)

p+=twoDy;

else

{

y++;

p+=twoDyMinusDx;

draw_pixel(x,y);

}

}

}

voiddisplay()

{

glClear(GL_COLOR_BUFFER_BIT);

Bresenham(0,0,400,400);

glFlush();

}

voidmyinit()

{

glClearColor(0.8,1.0,1.0,1.0);

glColor3f(0.0,0.0,1.0);

glPointSize(1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,500.0,0.0,500.0);

}

voidmain(intargc,char**argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(200.0,200.0);

glutCreateWindow("CG_test_Bresenham_Lineexample");

glutDisplayFunc(display);

myinit();

glutMainLoop();

}

二、中点法绘制椭圆

#include

#include

#include

inlineintround(constfloata){returnint(a+0.5);}

voidsetPixel(GLintxCoord,GLintyCoord)

{

glBegin(GL_POINTS);

glVertex2i(xCoord,yCoord);

glEnd();

}

voidellipseMidpoint(intxCenter,intyCenter,intRx,intRy)

{

intRx2=Rx*Rx;

intRy2=Ry*Ry;

inttwoRx2=2*Rx2;

inttwoRy2=2*Ry2;

intp;

intx=0;

inty=Ry;

intpx=0;

intpy=twoRx2*y;

voidellipsePlotPoints(int,int,int,int);

ellipsePlotPoints(xCenter,yCenter,x,y);

p=round(Ry2-(Rx2*Ry)+(0.25*Rx2));

while(px

x++;

px+=twoRy2;

if(p<0)

p+=Ry2+px;

else{

y--;

py-=twoRx2;

p+=Ry2+px-py;

}

ellipsePlotPoints(xCenter,yCenter,x,y);

}

p=round(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);

while(y>0){

y--;

py-=twoRx2;

if(p>0)

p+=Rx2-py;

else{

x++;

px+=twoRy2;

p+=Rx2-py+px;

}

ellipsePlotPoints(xCenter,yCenter,x,y);

}

}

voidellipsePlotPoints(intxCenter,intyCenter,intx,inty)

{

setPixel(xCenter+x,yCenter+y);

setPixel(xCenter-x,yCenter+y);

setPixel(xCenter+x,yCenter-y);

setPixel(xCenter-x,yCenter-y);

}

voiddisplay()

{

glClear(GL_COLOR_BUFFER_BIT);

ellipseMidpoint(200,200,50,30);

glFlush();

}

voidmyinit()

{

glClearColor(0.8,1.0,1.0,1.0);

glColor3f(0.0,0.0,1.0);

glPointSize(1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,300.0,0.0,300.0);

}

voidmain(intargc,char**argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(300,300);

glutInitWindowPosition(200.0,200.0);

glutCreateWindow("circleMIdexample");

glutDisplayFunc(display);

myinit();

glutMainLoop();

}

三、抛物线

#include

#include

#include

inlineintround(constfloata){returnint(a+0.5);}

voidsetPixel(GLintxCoord,GLintyCoord)

{

glBegin(GL_POINTS);

glVertex2i(xCoord,yCoord);

glEnd();

}

voidellipseMidpoint(intxCenter,intyCenter,inta,intb)

{

intp;

intx=xCenter;

inty=yCenter;

intpx=0,py=0;

voidellipsePlotPoints(int,int,int,int);

ellipsePlotPoints(xCenter,yCenter,px,py);

p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;

do{

if(p<0)

{

x=x+1;

y=y;

p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;

}

else{

x=x+1;

y=y-1;

p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;

}

px=x-xCenter;

py=y-yCenter;

ellipsePlotPoints(xCenter,yCenter,px,py);

}while(px

for(;;)

{

if(p<0)

{

x=x-1;

y=y+1;

p=yCenter+a*(x+0.5-xCenter)*(x+0.5-xCenter)+b*(x+0.5-xCenter)-y-1;

}

else{

x=x;

y=y+1;

p=yCenter+a*(x+0.5-xCenter)*(x+0.5-xCenter)+b*(x+0.5-xCenter)-y-1;

}

px=x-xCenter;

py=y-yCenter;

ellipsePlotPoints(xCenter,yCenter,px,py);

};

}

voidellipsePlotPoints(intxCenter,intyCenter,intx,inty)

{

setPixel(xCenter+x,yCenter+y);

setPixel(xCenter-x,yCenter+y);

}

voiddisplay()

{

glClear(GL_COLOR_BUFFER_BIT);

ellipseMidpoint(150,150,1,0);

glFlush();

}

voidmyinit()

{

glClearColor(0.8,1.0,1.0,1.0);

glColor3f(0.0,0.0,1.0);

glPointSize(1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,300.0,0.0,300.0);

}

voidmain(intargc,char**argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(200.0,200.0);

glutCreateWindow("circleMIdexample");

glutDisplayFunc(display);

myinit();

glutMainLoop();

}

四、基本图元输出

#include

#include

#include

voidPolygon(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6)

{

glBegin(GL_POLYGON);

glVertex2iv(p1);

glVertex2iv(p2);

glVertex2iv(p3);

glVertex2iv(p4);

glVertex2iv(p5);

glVertex2iv(p6);

glEnd();

}

voidTriangles(int*p1,int

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 公务员考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1