5 23
14-1
///開檔寫檔關檔
///開檔讀檔關檔
#include <stdio.h>
int main()
{
FILE * fout = fopen("file.txt", "w+");
printf("Hello World\n");
fprintf(fout,"Hello World\n");
fclose(fout);
}
14-2
///開檔寫檔關檔
///開檔讀檔關檔
#include <stdio.h>
int main()
{
FILE * fout = fopen("file.txt", "w+");
fprintf(fout, "angle1 %d\n", 999);
fclose(fout);
char line(200);
int a;
FILE * fin = fopen("file2.txt","r");
fscanf(fin, "%s %d",line,&a);
printf("讀到了字串:%s 及整數%d\n",line,a);
fclose(fout);
}
14-3
///week14rect TRT
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX = 0;
int angleID=0;///第0個關節
FILE * fout = NULL;
void myWrite(){
if(fout==NULL) fout = fopen("file.txt","w+");
for(int i=0;i<20;i++){
fprintf(fout, "%.2f ",angle[i]);
}
}
void keyboard(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;
}///用keyboard按鍵決定要改哪個關節
void mouse(int button,int state,int x,int y){
oldX = x;
}
void motion(int x,int y){
angle[angleID] += (x-oldX);
myWrite();
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.2);///身體
glPushMatrix();///右半邊
glTranslatef(0.3,0.5,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);///旋轉
glColor3f(1,0,0);///紅色
glRectf(0.3,0.5,0.8,0.3);///手臂
glPushMatrix();
glTranslatef(0.8,0.4,0);///3把下手肘掛在關節
glRotatef(angle[1],0,0,1);///2旋轉
glTranslatef(-0.8,-0.4,0);///1把下手肘的旋轉中心放政中心
glColor3f(0,1,0);///綠色
glRectf(0.8,0.5,1.1,0.3);///在畫下手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();///左半邊
glTranslatef(-0.3,0.5,0);
glRotatef(angle[2],0,0,1);
glTranslatef(+0.3,-0.4,0);///旋轉
glColor3f(1,0,0);///紅色
glRectf(-0.3,0.5,-0.8,0.3);///手臂
glPushMatrix();
glTranslatef(-0.8,0.4,0);///3把下手肘掛在關節
glRotatef(angle[3],0,0,1);///2旋轉
glTranslatef(+0.8,-0.4,0);///1把下手肘的旋轉中心放政中心
glColor3f(0,1,0);///綠色
glRectf(-0.8,0.5,-1.1,0.3);///在畫下手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
//glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
第二節課ㄌ
///week14rect TRT
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX = 0;
int angleID=0;///第0個關節
FILE * fout = NULL;
void myWrite(){
if(fout==NULL) fout = fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf( "%.2f ",angle[i]);
fprintf(fout, "%.2f ",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
///少了fclose
}
void keyboard(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;
}///用keyboard按鍵決定要改哪個關節
void mouse(int button,int state,int x,int y){
oldX = x;
}
void motion(int x,int y){
angle[angleID] += (x-oldX);
myWrite();
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.2);///身體
glPushMatrix();///右半邊
glTranslatef(0.3,0.5,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);///旋轉
glColor3f(1,0,0);///紅色
glRectf(0.3,0.5,0.8,0.3);///手臂
glPushMatrix();
glTranslatef(0.8,0.4,0);///3把下手肘掛在關節
glRotatef(angle[1],0,0,1);///2旋轉
glTranslatef(-0.8,-0.4,0);///1把下手肘的旋轉中心放政中心
glColor3f(0,1,0);///綠色
glRectf(0.8,0.5,1.1,0.3);///在畫下手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();///左半邊
glTranslatef(-0.3,0.5,0);
glRotatef(angle[2],0,0,1);
glTranslatef(+0.3,-0.4,0);///旋轉
glColor3f(1,0,0);///紅色
glRectf(-0.3,0.5,-0.8,0.3);///手臂
glPushMatrix();
glTranslatef(-0.8,0.4,0);///3把下手肘掛在關節
glRotatef(angle[3],0,0,1);///2旋轉
glTranslatef(+0.8,-0.4,0);///1把下手肘的旋轉中心放政中心
glColor3f(0,1,0);///綠色
glRectf(-0.8,0.5,-1.1,0.3);///在畫下手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
//glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
二-2
///week14rect TRT
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX = 0;
int angleID=0;///第0個關節
FILE * fout = NULL, *fin = NULL;
void myWrite(){
if(fout==NULL) fout = fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf( "%.2f ",angle[i]);
fprintf(fout, "%.2f ",angle[i]);
}
printf("\n");
fprintf(fout,"\n");///少了fclose
}
void myRead(){
if(fout!=NULL) { fclose(fout);fout=NULL;}
if(fin==NULL) fin = fopen("file.txt","r");
for(int i=0;i<20;i++){
fscanf(fin,"%f",&angle[i]);
}
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y){
if( key=='r'){
myRead();
}
if( key=='0') angleID=0;///預設
if( key=='1') angleID=1;
if( key=='2') angleID=2;
if( key=='3') angleID=3;
}///用keyboard按鍵決定要改哪個關節
void mouse(int button,int state,int x,int y){
oldX = x;
}
void motion(int x,int y){
angle[angleID] += (x-oldX);
myWrite();
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.2);///身體
glPushMatrix();///右半邊
glTranslatef(0.3,0.5,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);///旋轉
glColor3f(1,0,0);///紅色
glRectf(0.3,0.5,0.8,0.3);///手臂
glPushMatrix();
glTranslatef(0.8,0.4,0);///3把下手肘掛在關節
glRotatef(angle[1],0,0,1);///2旋轉
glTranslatef(-0.8,-0.4,0);///1把下手肘的旋轉中心放政中心
glColor3f(0,1,0);///綠色
glRectf(0.8,0.5,1.1,0.3);///在畫下手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();///左半邊
glTranslatef(-0.3,0.5,0);
glRotatef(angle[2],0,0,1);
glTranslatef(+0.3,-0.4,0);///旋轉
glColor3f(1,0,0);///紅色
glRectf(-0.3,0.5,-0.8,0.3);///手臂
glPushMatrix();
glTranslatef(-0.8,0.4,0);///3把下手肘掛在關節
glRotatef(angle[3],0,0,1);///2旋轉
glTranslatef(+0.8,-0.4,0);///1把下手肘的旋轉中心放政中心
glColor3f(0,1,0);///綠色
glRectf(-0.8,0.5,-1.1,0.3);///在畫下手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
//glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言