2023年5月3日 星期三

Lisa的電腦圖學 - week12

 ➤ Week12-1_printf_fprintf.cpp

     1. 開啟CodeBlocks ,新增File ➝ New ➝ Empty file,令存新檔為week12-1_printf_fprintf.cpp

    2. 輸入程式碼,
#include <stdio.h>
int main()
{
    FILE * fout = fopen("file.txt", "w");

    printf("Hello World\n");
    fprintf(fout, "Hellow World在檔案裡");
}

 ➤ Week12-2_scanf_fscanf.cpp

     1. 開啟CodeBlocks ,新增File ➝ New ➝ Empty file,令存新檔為week12-2_scanf_fscanf.cpp

     2. 輸入程式碼,

#include <stdio.h>
int main()
{
    FILE * fin = fopen("file.txt", "r");

    char line[100];
    fscanf(fin, "%s", line);
    printf("從file.txt讀到了: %s\n", line);

    fscanf(fin, "%s", line);
    printf("從file.txt讀到了: %s\n", line);
}





 ➤ Week12-3_many_numbers_scanf__printf_fscanf_fprintf.cpp
     1. 開啟CodeBlocks ,新增File ➝ New ➝ Empty file,令存新檔為week12-3_many_numbers_scanf__printf_fscanf_fprintf.cpp
     2. 輸入程式碼,
#include <stdio.h>
int main()
{
    int a[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
    int b[10] = {};

    FILE * fout = fopen("file3.txt", "w");
    for(int i=0; i<10; i++){
        printf("%d ", a[i]);
        fprintf(fout, "%d ", a[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
    fclose(fout);
    FILE * fin = fopen("file3.txt", "r");
    for(int i=0; i<10; i++){
        fscanf(fin, "%d", &b[i]);
        printf("b%d:%d ", i, b[i]);
    }
    fclose(fin);
}




 ➤ Week12-4_keyboard_mouse
        1. 開啟CodeBlocks ,新增OpenGL專案,File ➝ New ➝ Project



     2. 選取GLUT Project ➝ Go ➝ Next



    3. 點選三個點,點選目錄在桌面,加上專案名字 week12-4_keyboard_mouse,Next ➝ Finish



     4. 點選 week12-4_keyboard_mouse ➝ Sources ➝ main.c,刪除所有程式碼,並輸入
#include <stdio.h>
#include <GL/glut.h>
FILE*fout=NULL;
FILE*fin=NULL;
float teapotX=0,teapotY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslated(teapotX,teapotY,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN)
    {
        teapotX=(x-150)/150.0;
        teapotY=(150-y)/150.0;
        if(fout==NULL) fout=fopen("file4.txt","w");
        fprintf(fout,"%f %f\n",teapotX,teapotY);
    }
    display();
}
void keyboard(unsigned char key,int x,int y)
{
    if(fin==NULL)
    {
        fclose(fout);
        fin=fopen("file4.txt","r");
    }
    fscanf(fin,"%f %f",&teapotX,&teapotY);
    display();
}
int main(int argc,char** argv)
{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week12");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}



     5. 右鍵點選 week12-4_keyboard_mouse ➝ Properties ➝ Build targets,將Execution working dir 改為小數點 .


     6. 將freeglut ➝ bin裡的freeglut.dull複製到week12-4_keyboard_mouse裡



     7.  點選File ➝ Save everything ,儲存所有設定與檔案





沒有留言:

張貼留言