计算机图形学姓名:李倩倩 班级:硕研10-14学号:第一题:#include <> #include <> void MidpintLine( HDC hDC,int x0,int y0,int x1,int y1,unsigned long color) { int a,b,delta1,delta2,d,x,y; a=y0-y1; b=x1-x0; d=2*a+b; delta1=2*a; delta2=2*(a+b); x=x0; y=y0; SetPixel(hDC,x,y,color); while(x<x1) { if(d<0) { x++; y++; d+=delta2; } else { x++; d+=delta1; } SetPixel(hDC,x,y,color); } } #define ROUND(a) ((int) (a+) void bresenham(HDC pdc,int xs,int ys,int xe,int ye,COLORREF color=0xFF) { int dx=xe-xs; int dy=ye-ys; int xinc,yinc; if(dx>0) xinc=1; else xinc=-1; if(dy>0) yinc=1; else yinc=-1; dx=abs(dx);dy=abs(dy); int x=xs,y=ys; int i=0; if(dx==0&&dy==0) SetPixel(pdc,x,y,color); SetPixel(hDC,x,y,color); else if(dx==0) { for(i=0;i<dy;i++) { SetPixel(pdc,x,y,color); y+=yinc; } } else if(dy==0) { for(i=0;i<dx;i++) { SetPixel(pdc,x,y,color); x+=xinc; } }else if(dx>dy){int p=2*dy-dx;int inc1=2*dy,inc2=2*(dy-dx);for(i=0;i<dx;i++){SetPixel(pdc,x,y,color);x+=xinc;if(p<0)p+=inc1;else{y+=yinc;p+=inc2;}}}else{int p=2*dx-dy;int inc1=2*dx,inc2=2*(dx-dy);for(i=0;i<dy;i++){SetPixel(pdc,x,y,color);y+=yinc;if(p<0)p+=inc1;else{x+=xinc;p+=inc2;}}}}void circlePlotPoints(HDC pdc, int xc,int yc,int x,int y,COLORREF color) {SetPixel(pdc,xc+x,yc+y,color);SetPixel(pdc,xc+x,yc-y,color);SetPixel(pdc,xc-x,yc+y,color);SetPixel(pdc,xc-x,yc-y,color);SetPixel(pdc,xc+y,yc+x,color);SetPixel(pdc,xc+y,yc-x,color); SetPixel(pdc,xc-y,yc+x,color);SetPixel(pdc,xc-y,yc-x,color);}void MidpointCircle(HDC pdc,int xc,int yc,int r,COLORREF color){int x=0;int y=r;int p=1-r;while(x<=y){circlePlotPoints(pdc,xc,yc,x,y,color);x++;if(p<0)p+=2*x+1;else{y--;p+=2*(x-y)+1;}}}void drawCircle(HDC pdc,intxc,int yc,int radius,COLORREFcolor){int x,y,p;x=0;y=radius;p=3-2*radius;while (x<y){circlePlotPoints(pdc,xc,yc,x,y,color);if (p<0) p=p+4*x+6;else{p=p+4*(x-y)+10;y-=1;}x+=1;}if(x==y)circlePlotPoints(pdc,xc,yc,x,y,color);}int main(int argc, char* argv[]){char arg[200]={0};arg[0]='\"';strcpy(arg+1,argv[0]);intlen=int(strlen(arg));arg[len]='\"';HWNDhWnd=FindWindow(NULL,arg);HDChDC=GetDC(hWnd);unsigned long color=0xffffff; MidpintLine(hDC,10,30,100,100,color );bresenham(hDC,10,10,600,600,color); MidpointCircle(hDC,50,50,30,color); drawCircle(hDC,100,100,30,color); return 0;}第二题:#include <GL/>#include <>#include <>void init(){glClearColor(1.0f, 1.0f, 1.0f, 1.0f);glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0f, 800.0f, 0.0f, 600.0f);}class wcPt2D {public:wcPt2D(float _x, float _y) {x = _x, y = _y;}GLfloat x, y;};wcPt2D pt[] = {wcPt2D(0,0), wcPt2D(100,0), wcPt2D(50,50)};= verts[k].x + tx; verts[k].y = verts[k].y + ty;}glBegin(GL_POLYGON);for(k = 0;k<nVerts; k++)glVertex2f(verts[k].x,verts[k].y);glEnd();}wcPt2D vertsRot[3] = {wcPt2D(0,0), wcPt2D(0,0), wcPt2D(0,0)};= + (verts[k].x - * cos(theta) - (verts[k].y - * sin(theta); vertsRot[k].y = + (verts[k].x - * sin(theta) + (verts[k].y - * cos(theta);}glBegin(GL_POLYGON);for(k = 0;k<nVerts;k++)glVertex2f(vertsRot[k].x,vertsRot[k].y);glEnd();}wcPt2D vertsNew[3] = {wcPt2D(0,0), wcPt2D(0,0), wcPt2D(0,0)};= verts[k].x * sx + * (1 - sx); vertsNew[k].y = verts[k].y * sy + * (1 - sy);}glBegin(GL_POLYGON);for(k = 0;k<nVerts;k++) glVertex2f(vertsNew[k].x,vertsNew[k].y);glEnd();}void Render(){glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); translatePolygon(pt, 3, 200,20);//rotatePolygon(pt, 3, pt[0], 10); scalePolygon(pt, 3, pt[0], 5,5); glFlush();}int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(800,600); glutInitWindowPosition(10,10); glutCreateWindow("2D Transformation"); init(); glutDisplayFunc(Render); glutMainLoop(); return 0; }第三题:#include <> #include <> #include <> int sign(float x) { if(x>0) return 1; else if(x<0) return -1; else return 0; } int Int(float x) { return (int) x; } void DDA(int x1,int y1,int x2,int y2,int color) { int length,i; float ax,ay,x,y; length=abs(x2-x1); if(abs(y2-y1)>length) length=abs(x2-x1); ax=(float)(x2-x1)/length; ay=(float)(y2-y1)/length; x=x1+*sign(ax); y=y1+*sign(ay); for(i=1;i<=length;i++) { putpixel(Int(x),Int(y),color); x=x+ax; y=y+ay; } } void main() { float re[8][2]; int gmode,gdriver=DETECT; int i,j,row; float sum; int p1[10]; int p2[10]; float array[8][4]={{0,0,0,1},{1,0,0,1},{1,1,0,1},{0,1,0,1},{0,0,1,1},{1,0,1,1},{1,1,1,1},{0,1,1,1}}; float t[4][3]={{1,0,0},{0,1,0},{0,0,0},{0,0,0}}; float l=2,m=4,n=6,k=; float result[8][3]; t[2][2]=1/k; t[3][0]=l; t[3][1]=m; t[3][2]=(n/k)+1; for(i=0;i<8;i++) { row=row%3; for(row=0;row<3;row++){ sum=0; for(j=0;j<4;j++) {sum=sum+array[i][j]*t[j][row]; } result[i][row]=sum; } } for(i=0;i<8;i++) { re[i][0]=result[i][0]/result[i][2]; re[i][1]=result[i][1]/result[i][2]; } for(i=0;i<4;i++) for(j=0;j<2;j++) p1[i*2+j]=(int)(re[i][j]*150);for(i=4;i<8;i++)for(j=0;j<2;j++)p2[(i-4)*2+j]=(int)(re[i][j]*100); p1[8]=p1[0];p1[9]=p1[1];p2[8]=p2[0];p2[9]=p2[1];for(i=0;i<10;i++)initgraph(&gdriver,&gmode," "); DDA(p1[0],p1[1],p2[0],p2[1],12); DDA(p1[2],p1[3],p2[2],p2[3],12); DDA(p1[4],p1[5],p2[4],p2[5],12); DDA(p1[6],p1[7],p2[6],p2[7],12); drawpoly(5,p1);drawpoly(5,p2);getch();closegraph();return;}第四题:#include<>#include<>char msg[1];float px[6]={10,20,40,50,70,90};float py[6]={10,30,50,40,30,20};main(){float a0,a1,a2,a3,b0,b1,b2,b3;int k,x,y,w;float i,t,dt,n=6;int graphDriver=DETECT;int graphMode=0;initgraph(&graphDriver,&graphMode,""); setcolor(BLUE);setcolor(YELLOW);dt=1/n;for(k=0;k<10-1;k++){moveto(px[k],py[k]);lineto(px[k+1],py[k+1]);}setlinestyle(0,0,3);for(k=0;k<10-3;k+=3) {a0=px[k];a1=-3*px[k]+3*px[k+1];a2=3*px[k]-6*px[k+1]+3*px[k+2];a3=-px[k]+3*px[k+1]-3*px[k+2]+px[k=3]; b0=py[k];b1=-3*py[k]+3*py[k+1];b2=3*py[k]-6*py[k+1]+3*py[k+2];b3=-py[k]+3*py[k+1]-3*py[k+2]+py[k+3]; for(i=0;i<n;i+={t=i*dt;x=a0+a1*t+a2*t*t+a3*t*t*t;y=b0+b1*t+b2*t*t+b3*t*t*t;if(i==0)moveto(x,y);lineto(x,y);}}getch();closegraph();}第五题:#include <GL/>#include <GL/>#include <>GLfloat ctrlpoints[5][5][3] = {{{-2,0,0},{-1,1,0},{0,0,0},{1,-1,0},{2,0,0}},{{-2,0,-1},{-1,1,-1},{0,0,-1},{1,-1,-1},{2, 0,-1}},{{-2,0,-2},{-1,1,-2},{0,0,-2},{1,-1,-2},{2,0,-2}},{{-2,0,-3},{-1,1,-3},{0,0,-3},{1,-1,-3},{2, 0,-3}},{{-2,0,-4},{-1,1,-4},{0,0,-4},{1,-1,-4},{2, 0,-4}}};GLfloat mat_ambient[] = {,,,};GLfloat mat_diffuse[] = {,,,};GLfloat mat_specular[] = {,,,};GLfloat light_ambient[] = {,,,};GLfloat light_diffuse[] = {,,,};GLfloat light_specular[] = {,,,};GLfloat light_position[] = {,,,};void myInit(void){glClearColor(0,0,0,0);/glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambien t);glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffus e);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specu lar);glMaterialf(GL_FRONT,GL_SHININESS,;glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambien t);glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffus e);glLightfv(GL_LIGHT0,GL_SPECULAR,light_specu lar);glLightfv(GL_LIGHT0,GL_POSITION,light_posit ion);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_DEPTH_TEST);glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_A LPHA);glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE); glEnable(GL_BLEND);glEnable(GL_AUTO_NORMAL);glEnable(GL_NORMALIZE);glFrontFace(GL_CW);glShadeModel(GL_SMOOTH);glEnable(GL_LINE_SMOOTH); }void myDisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glColor3f,,;glTranslatef,,;glRotatef,,,;glPushMatrix();glEnable(GL_MAP2_VERTEX_3);glMap2f(GL_MAP2_VERTEX_3,0,1,3,5,0,1,15,5,& ctrlpoints[0][0][0]);glMapGrid2f,,,,,;glEvalMesh2(GL_FILL,0,,0,;glPopMatrix();glutSwapBuffers();}void myReshape(GLsizei w,GLsizei h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective,(GLfloat)w/(GLfloat)h,,;glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef,,;}int main(int argc,char ** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GL UT_DEPTH);glutInitWindowSize(400,400);glutInitWindowPosition(200,200);glutCreateWindow("lighted Bezier surface"); myInit();glutReshapeFunc(myReshape);glutDisplayFunc(myDisplay);glutMainLoop();return(0);。