2016年5月31日 星期二

Week15

Class Work 1 內插法
Step 1. 開啟Excel
Step 2. 設定新舊值(New:40, Old:20)

Step 3. 設定公式(New*Alpha+Old*(1-Alpha))
            ($號可鎖定格子)

Step 4.用Ctrl鍵選取兩格,開個圖表 
The End!

Class Work 2 動作
Code:(Debug 中)
#include <iostream>
#include <stdio.h>
#include <GL/glut.h>
FILE *fout=NULL, *fin=NULL;
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};///陣列
int rotID=1, oldX=0;///利用ID操控要動關節
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutWireCube(0.3);
        glPushMatrix();
            glTranslatef(0,0.2,0);
            glutWireCube(0.1);
        glPopMatrix();

        glPushMatrix();
            glTranslatef(0.15,0.1,0);
            glRotatef(rot[1],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(0.05,0,0);
            glRotatef(rot[2],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(0.05,0,0);
            glRotatef(rot[3],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);
        glPopMatrix();

        glPushMatrix();
            glTranslatef(-0.15,0.1,0);
            glRotatef(-rot[4],0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(-0.05,0,0);
            glRotatef(-rot[5],0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(-0.05,0,0);
            glRotatef(-rot[6],0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);
        glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN){
        oldX=x;
    }
}void motion(int x, int y)
{
    rot[rotID] += (x-oldX);
    oldX=x;
    glutPostRedisplay();

    /*if(fout==NULL) fout=fopen("motion.txt","w+");
    for(int i=0;i<10;i++){
        fprintf( fout, "%.1f ", rot[i]);;
        printf( "%.1f ", rot[i]);;
    }
    fprintf(fout, "\n");
    printf("\n");*/
}
void timer(int t)
{
    float alpha=(t%10)/10;
    if(t%10==0)
    {
        for(int i=0;i<10;i++){
            rotOld[i]=rotNew[i];
            fprintf( fout, "%.1f ", &rotNew[i]);;
            printf( "%.1f ", rotNew[i]);;
        }
        fprintf(fout, "\n");
        printf("\n");
    }
    for(int i=0;i<10;i++)
    {
        rotOld[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
    }
    glutTimerFunc(100,timer,t+1);
    glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') rotID=1;
    if(key=='2') rotID=2;
    if(key=='3') rotID=3;
    if(key=='4') rotID=4;
    if(key=='5') rotID=5;
    if(key=='6') rotID=6;///利用按鍵控制關節號碼

    if(key=='r'){
        if(fin==NULL) fin=fopen("motion.txt","r");
        for(int i=0;i<10;i++){
            fscanf( fin, "%f ", &rot[i]);;
            printf( "%f ", rot[i]);;
        }
        fprintf(fin, "\n");
        printf("\n");
    }
    if(key=='s')
    {
    if(fout==NULL) fout=fopen("motion.txt","w+");
    for(int i=0;i<10;i++){
        fprintf( fout, "%.1f ", &rot[i]);;
        printf( "%.1f ", rot[i]);;
    }
    fprintf(fout, "\n");
    printf("\n");
    }
    if(key=='t')
    {
        if(fout==NULL) fout=fopen("motion.txt","r");
        for(int i=0;i<10;i++){
            fprintf( fout, "%.1f ", &rotNew[i]);;
            printf( "%.1f ", rotNew[i]);;
        }
        fprintf(fout, "\n");
        printf("\n");
       glutTimerFunc(100,timer,0);
    }
    glutPostRedisplay();///請電腦貼個公告,有空就重新display
}

int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMainLoop();

}

沒有留言:

張貼留言