计算机图形学
姓名:李倩倩
班级:硕研10-14
学号:
第一题:
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 { if(d<0) { x++; y++; d+=delta2; } else { x++; d+=delta1; } SetPixel(hDC,x,y,color); } } 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 { SetPixel(pdc,x,y,color); y+=yinc; } } else if(dy==0) { for(i=0;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 { 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 { 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; } } } v oid drawCircle(HDC pdc,int xc,int yc,int radius,COLORREF color) { int x,y,p; x=0; y=radius; p=3-2*radius; while (x 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]); int len=int(strlen(arg)); arg[len]='\"'; HWND hWnd=FindWindow(NULL,arg); HDC hDC=GetDC(hWnd); unsigned long color=0xffffff; MidpintLine(hDC,10,30,100,100,color); bresenham(hDC,10,10,600,600,color);M idpointCircle(hDC,50,50,30,color); drawCircle(hDC,100,100,30,color); return 0; } 第二题: void init() { glClearColor(, , , ); glMatrixMode(GL_PROJECTION); gluOrtho2D(, , , ); } 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)}; //---------------------------------平移 void translatePolygon(wcPt2D* verts, GLint nVerts, GLfloat tx, GLfloat ty) { GLint k = 0; for(k = 0;k verts[k].x = verts[k].x + tx; verts[k].y = verts[k].y + ty; } glBegin(GL_POLYGON); for(k = 0;k glVertex2f(verts[k].x, verts[k].y); glEnd(); } wcPt2D vertsRot[3] = {wcPt2D(0,0), wcPt2D(0,0), wcPt2D(0,0)}; //---------------------------------2D旋转void rotatePolygon(wcPt2D* verts, GLint nVerts, wcPt2D pivPt, GLdouble theta) { GLint k = 0; for(k = 0;k } glBegin(GL_POLYGON); for(k = 0;k glVertex2f(vertsRot[k].x, vertsRot[k].y); glEnd(); } wcPt2D vertsNew[3] = {wcPt2D(0,0), wcPt2D(0,0), wcPt2D(0,0)}; //-----------------------------------2D缩放void scalePolygon(wcPt2D *verts, GLint nVerts, wcPt2D fixedPt, GLfloat sx, GLfloat sy) { GLint k = 0; for(k = 0;k } glBegin(GL_POLYGON); for(k = 0;k glVertex2f(vertsNew[k].x, vertsNew[k].y); glEnd(); } void Render() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(, , ); 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; } 第三题: 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; 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 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; } 第四题: 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]; { 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(); } 第五题: 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}}}; void myInit(void) { glClearColor(0,0,0,0);/ glMaterialfv(GL_FRONT,GL_AMBIENT,ma t_ambient); glMaterialfv(GL_FRONT,GL_DIFFUSE,mat _diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,m at_specular); glLightfv(GL_LIGHT0,GL_AMBIENT,light_ ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,light_d iffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,light _specular); glLightfv(GL_LIGHT0,GL_POSITION,light_ position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA,GL_ONE_M INUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT,GL_DO NT_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_DE PTH_BUFFER_BIT); glColor3f glPushMatrix(); glEnable(GL_MAP2_VERTEX_3); glMap2f(GL_MAP2_VERTEX_3,0,1,3,5,0,1,1 5,5,&ctrlpoints[0][0][0]); glMapGrid2f glPopMatrix(); glutSwapBuffers(); } void myReshape(GLsizei w,GLsizei h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc,char ** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT _RGB|GLUT_DEPTH); glutInitWindowSize(400,400); glutInitWindowPosition(200,200); glutCreateWindow("lighted Bezier surface"); myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return(0);