人机交互.docx
- 文档编号:2126350
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:42
- 大小:23.07KB
人机交互.docx
《人机交互.docx》由会员分享,可在线阅读,更多相关《人机交互.docx(42页珍藏版)》请在冰点文库上搜索。
人机交互
1.Marching-Square程序
/*generatescontoursusingmarchingsquares*/
/*regionsize*/
#defineX_MAX1.0
#defineY_MAX1.0
#defineX_MIN-1.0
#defineY_MIN-1.0
/*numberofcells*/
#defineN_X50
#defineN_Y50
/*contourvalue*/
#defineTHRESHOLD0.0
#include
voiddisplay()
{
doublef(double,double);
intcell(double,double,double,double);
voidlines(int,int,int,double,double,double,double);
doubledata[N_X][N_Y];
inti,j;
intc;
glClear(GL_COLOR_BUFFER_BIT);
/*formdataarrayfromfunction*/
for(i=0;i for(j=0;j data[i][j]=f(X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0), Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0)); /*processeachcell*/ for(i=0;i for(j=0;j { c=cell(data[i][j],data[i+1][j],data[i+1][j+1],data[i][j+1]); lines(c,i,j,data[i][j],data[i+1][j],data[i+1][j+1],data[i][j+1]); } glFlush(); } /*definefunctionf(x,y)*/ doublef(doublex,doubley) { doublea=0.49,b=0.5; /*ovalsofCassini*/ return(x*x+y*y+a*a)*(x*x+y*y+a*a)-4*a*a*x*x-b*b*b*b; } /*definecellvertices*/ intcell(doublea,doubleb,doublec,doubled) { intn=0; if(a>THRESHOLD)n+=1; if(b>THRESHOLD)n+=8; if(c>THRESHOLD)n+=4; if(d>THRESHOLD)n+=2; returnn; } /*drawlinesegmentsforeachcase*/ voidlines(intnum,inti,intj,doublea,doubleb,doublec,doubled) { voiddraw_one(int,int,int,double,double,double,double); voiddraw_adjacent(int,int,int,double,double,double,double); voiddraw_opposite(int,int,int,double,double,double,double); switch(num) { case1: case2: case4: case7: case8: case11: case13: case14: draw_one(num,i,j,a,b,c,d); break; case3: case6: case9: case12: draw_adjacent(num,i,j,a,b,c,d); break; case5: case10: draw_opposite(num,i,j,a,b,c,d); break; case0: case15: break; } } voiddraw_one(intnum,inti,intj,doublea,doubleb,doublec,doubled) { doublex1,y1,x2,y2; doubleox,oy; doubledx,dy; dx=(X_MAX-X_MIN)/(N_X-1.0); dy=(Y_MAX-Y_MIN)/(N_Y-1.0); ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0); oy=Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0); switch(num) { case1: case14: x1=ox; y1=oy+dy*(THRESHOLD-a)/(d-a); x2=ox+dx*(THRESHOLD-a)/(b-a); y2=oy; break; case2: case13: x1=ox; y1=oy+dy*(THRESHOLD-a)/(d-a); x2=ox+dx*(THRESHOLD-d)/(c-d); y2=oy+dy; break; case4: case11: x1=ox+dx*(THRESHOLD-d)/(c-d); y1=oy+dy; x2=ox+dx; y2=oy+dy*(THRESHOLD-b)/(c-b); break; case7: case8: x1=ox+dx*(THRESHOLD-a)/(b-a); y1=oy; x2=ox+dx; y2=oy+dy*(THRESHOLD-b)/(c-b); break; } glBegin(GL_LINES); glVertex2d(x1,y1); glVertex2d(x2,y2); glEnd(); } voiddraw_adjacent(intnum,inti,intj,doublea,doubleb,doublec,doubled) { doublex1,y1,x2,y2; doubleox,oy; doubledx,dy; dx=(X_MAX-X_MIN)/(N_X-1.0); dy=(Y_MAX-Y_MIN)/(N_Y-1.0); ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0); oy=Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0); switch(num) { case3: case12: x1=ox+dx*(THRESHOLD-a)/(b-a); y1=oy; x2=ox+dx*(THRESHOLD-d)/(c-d); y2=oy+dy; break; case6: case9: x1=ox; y1=oy+dy*(THRESHOLD-a)/(d-a); x2=ox+dx; y2=oy+dy*(THRESHOLD-b)/(c-b); break; } glBegin(GL_LINES); glVertex2d(x1,y1); glVertex2d(x2,y2); glEnd(); } voiddraw_opposite(intnum,inti,intj,doublea,doubleb,doublec,doubled) { doublex1,y1,x2,y2,x3,y3,x4,y4; doubleox,oy; doubledx,dy; dx=(X_MAX-X_MIN)/(N_X-1.0); dy=(Y_MAX-Y_MIN)/(N_Y-1.0); ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0); oy=Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0); switch(num) { case5: x1=ox; y1=oy+dy*(THRESHOLD-a)/(d-a); x2=ox+dx*(THRESHOLD-a)/(b-a); y2=oy; x3=ox+dx*(THRESHOLD-d)/(c-d); y3=oy+dy; x4=ox+dx; y4=oy+dy*(THRESHOLD-b)/(c-b); break; case10: x1=ox; y1=oy+dy*(THRESHOLD-a)/(d-a); x2=ox+dx*(THRESHOLD-d)/(c-d); y2=oy+dy; x3=ox+dx*(THRESHOLD-d)/(c-d); y3=oy; x4=ox+dx; y4=oy+dy*(THRESHOLD-b)/(c-b); break; } glBegin(GL_LINES); glVertex2d(x1,y1); glVertex2d(x2,y2); glVertex2d(x3,y3); glVertex2d(x4,y4); glEnd(); } voidmyReshape(intw,inth) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) gluOrtho2D(X_MIN,X_MAX,Y_MIN*(GLfloat)h/(GLfloat)w, Y_MAX*(GLfloat)h/(GLfloat)w); else gluOrtho2D(X_MIN*(GLfloat)w/(GLfloat)h, X_MAX*(GLfloat)w/(GLfloat)h,Y_MIN,Y_MAX); glMatrixMode(GL_MODELVIEW); } voidmain(intargc,char**argv) { glutInit(&argc,argv); glutInitWindowSize(500,500); glutCreateWindow("contourplot"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glClearColor(0.0,0.0,0.0,1.0); glColor3f(1.0,1.0,1.0); glutMainLoop(); } 2.生成正方形的程序 /*ThisprogramillustratestheuseoftheGLUTlibraryfor interfacingwithaWindowSystem*/ /*Theprogramopensawindow,clearsittoblack, thendrawsaboxatthelocationofthemouseeachtimethe leftbuttonisclicked.Therightbuttonexitstheprogram. Theprogramalsoreactscorrectlywhenthewindowis movedorresizedbyclearingthenewwindowtoblack.*/ #include #include /*globals*/ GLsizeiwh=500,ww=500;/*initialwindowsize*/ GLfloatsize=3.0;/*halfsidelengthofsquare*/ voiddrawSquare(intx,inty) { y=wh-y; glColor3ub((char)rand()%256,(char)rand()%256,(char)rand()%256); glBegin(GL_POLYGON); glVertex2f(x+size,y+size); glVertex2f(x-size,y+size); glVertex2f(x-size,y-size); glVertex2f(x+size,y-size); glEnd(); glFlush(); } /*reshapingroutinecalledwheneverwindowisresized ormoved*/ voidmyReshape(GLsizeiw,GLsizeih) { /*adjustclippingbox*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,(GLdouble)w,0.0,(GLdouble)h,-1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /*adjustviewportandclear*/ glViewport(0,0,w,h); glClear(GL_COLOR_BUFFER_BIT); glFlush(); /*setglobalsizeforusebydrawingroutine*/ ww=w; wh=h; } voidmyinit(void) { glViewport(0,0,ww,wh); /*Pick2Dclippingwindowtomatchsizeofscreenwindow. Thischoiceavoidshavingtoscaleobjectcoordinates eachtimewindowisresized.*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,(GLdouble)ww,0.0,(GLdouble)wh,-1.0,1.0); /*setclearcolortoblackandclearwindow*/ glClearColor(0.0,0.0,0.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } voidmouse(intbtn,intstate,intx,inty) { if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)exit(0) } /*displaycallbackrequiredbyGLUT*/ vioddisplay() {} intmain(intargc,char**argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutCreateWindow("square"); myinit(); glutReshapeFunc(myReshape); glutMouseFunc(mouse) glutMotionFunc(drawSqure); glutDisplayFunc(display); glutMainLoop(); } 3.画图程序 /*simplepaintingprogramwithtext,lines,triangles, rectangles,andpoints*/ #defineNULL0 #defineLINE1 #defineRECTANGLE2 #defineTRIANGLE3 #definePOINTS4 #defineTEXT5 #include #include voidmouse(int,int,int,int); voidkey(unsignedchar,int,int); voiddisplay(void); voiddrawSquare(int,int); voidmyReshape(GLsizei,GLsizei); voidmyinit(void); voidscreen_box(int,int,int); voidright_menu(int); voidmiddle_menu(int); voidcolor_menu(int); voidpixel_menu(int); voidfill_menu(int); intpick(int,int); /*globals*/ GLsizeiwh=500,ww=500;/*initialwindowsize*/ GLfloatsize=3.0;/*halfsidelengthofsquare*/ intdraw_mode=0;/*drawingmode*/ intrx,ry;/*rasterposition*/ GLfloatr=1.0,g=1.0,b=1.0;/*drawingcolor*/ intfill=0;/*fillflag*/ voiddrawSquare(intx,inty) { y=wh-y; glColor3ub((char)rand()%256,(char)rand()%256,(char)rand()%256); glBegin(GL_POLYGON); glVertex2f(x+size,y+size); glVertex2f(x-size,y+size); glVertex2f(x-size,y-size); glVertex2f(x+size,y-size); glEnd(); } /*rehapingroutinecalledwheneverwindowisresized ormoved*/ voidmyReshape(GLsizeiw,GLsizeih) { /*adjustclippingbox*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,(GLdouble)w,0.0,(GLdouble)h,-1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /*adjustviewportandclear*/ glViewport(0,0,w,h); glClearColor(0.8,0.8,0.8,1.0); glClear(GL_COLOR_BUFFER_BIT); display(); glFlush(); /*setglobalsizeforusebydrawingroutine*/ ww=w; wh=h; } voidmyinit(void) { glViewport(0,0,ww,wh); /*Pick2DclippingwindowtomatchsizeofXwindow. Thischoiceavoidshavingtoscaleobjectcoordinates eachtimewindowisresized.*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,(GLdouble)ww,0.0,(GLdouble)wh,-1.0,1.0); /*setclearcolortoblackandclearwindow*/ glClearColor(0.8,0.8,0.8,1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } voidmouse(intbtn,intstate,intx,inty) { staticintcount; intwhere; staticintxp[2],yp[2]; if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN) { glPushAttrib(GL_ALL_ATTRIB_BITS); where=pick(x,y); glColor3f(r,g,b); if(where! =0) { count=0; draw_mode=where; } elseswitch(draw_mode) { case(LINE): if(count==0) { count++; xp[0]=x; yp[0]=y; } else { glBegin(GL_LINES); glVertex2i(x,wh-y); glVertex2i(xp[0],wh-yp[0]); glEnd(); draw_mode=0; count=0; } break; case(RECTANGLE): if(count==0) { count++; xp[0]=x; yp[0]=y; } else { if(fill)glBegin(GL_POLYGON); elseglBegin(GL_LINE_LOOP); glVertex2i(x,wh-y); glVertex2i(x,wh-yp[0]); glVertex2i(xp[0],wh-yp[0]); glVertex2i(xp[0],wh-y); glEnd(); draw_mode=0; cou
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 人机交互