week14
1.讀入(fopen開啟檔案、fprintf寫檔、fclose關檔)
#include <stdio.h>
int main()
{ fout 檔案指標 開啟檔案(檔名, write模式)
FILE * fout = fopen("file.txt","w+");
printf("Hello\n");
fprintf(fout,"Hello\n");
fclose(fout); 關閉檔案
}
2.讀入+印出
#include <stdio.h>
int main()
{
FILE * fout = fopen("file.txt","w+"); output
fprintf(fout,"3.1415926\n");
fclose(fout);
float angle=0;
FILE * fin = fopen("file.txt","r"); input
fscanf(fin, "%f", &angle); %f => 小數後6位,第7由第6取概
printf("讀到了角度:%f",angle);
fclose(fin);
}
4.這樣執行時小黑就會在轉時顯示角度
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleid=0;///0右手 1右手關節 2左手 3左手關節
FILE * fout = NULL;
void write()
{
if(fout = NULL) fout = fopen("file.txt","w+");
for(int i=0;i<20;i++)
{
printf("%.1f",angle[i]);///印出黑視窗
fprintf(fout,"%.1f",angle[i]);///印出檔案
}///沒有fclose
}
void key(unsigned char key,int x,int y)
{
if(key=='0') angleid=0;
if(key=='1') angleid=1;
if(key=='2') angleid=2;
if(key=='3') angleid=3;
}
void mouse(int button,int state,int x,int y)
{
oldX=x;
}
void motion(int x,int y)
{
angle[angleid]+=(x-oldX);
write();
oldX=x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);///手臂掛回身體
glRotatef(angle[0],0,0,1);///對z軸旋轉
glTranslatef(-0.3,-0.4,0);///手臂旋轉軸放中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0);///手臂掛回身體
glRotatef(angle[1],0,0,1);///對z軸旋轉
glTranslatef(-0.7,-0.4,0);///手臂旋轉軸放中心
glColor3f(0,1,0);
glRectf(0.7,0.5,1.0,0.3);///下手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0);///手臂掛回身體
glRotatef(angle[2],0,0,1);///對z軸旋轉
glTranslatef(0.3,-0.4,0);///手臂旋轉軸放中心
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);
glPushMatrix();
glTranslatef(-0.7,0.4,0);///手臂掛回身體
glRotatef(angle[3],0,0,1);///對z軸旋轉
glTranslatef(0.7,-0.4,0);///手臂旋轉軸放中心
glColor3f(0,1,0);
glRectf(-0.7,0.5,-1.0,0.3);///下手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week13 rectangle");
glutMouseFunc(mouse);
glutKeyboardFunc(key);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
glutTimerFunc() 計時器
1.void timer (int t) 寫timer,glutTimerFunc( 等多久 , timer , t參數 );
沒有留言:
張貼留言