2021年5月31日 星期一

橘貓(電圖小筆記_week15)

檔案讀入讀出 

第一節課程式碼:

#include<stdio.h>///使用外掛printf() fprintf() fscanf() fopen()

int main(int argc,char**argv){

    FILE * fout=NULL;///檔案指標file output變數

    fout=fopen("hello.txt","w+");///開啟檔案hello.txt 模式write+

    printf("Hello World\n");

    fprintf(fout,"Hello World\n");///printf變fprintf

}

hello.txt原先是空白的文件 小黑跑出的Hello World為printf中的文字

hello.txt跑出來的Hello World為fprintf的(1-1)

接下來用notepad++開啟專案檔把working_dir改成"."(1-2~1-3)

完成後執行 會發現在codeblocks的專案檔裡面生成一個hello.txt(1-4)

(1-1)

(1-2)
(1-3)
(1-4)


讀入檔案印出字串

第二節課程式碼:

#include <stdio.h>

int main(int argc , char** argv){

    FILE * fin = NULL;///讀檔

    fin = fopen("hello.txt","r");///開啟剛剛的檔案,讀入

    char line[100];///宣告一個字串line

    fscanf(fin , "%s", line);///讀入字串

    printf("現在讀到的是 %s \n", line);/// 印出字串

    fscanf(fin, "%s", line);///讀入字串

    printf("現在讀到的是 %s \n", line);///印出字串

}

(2-1)

喝杯茶休息一下

#include <stdio.h>
#include <GL/glut.h>///使用glut外掛(需要將freeglut.dll檔案放入專案檔中)
void display(){
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc , char** argv){
   glutInit(&argc, argv);
   glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
   glutCreateWindow("week15 teaPOT");
   glutDisplayFunc(display);
   glutMainLoop();
}
(2-2.5)

茶壺手臂

第三節程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle=0, diff=2;///上週程式,會增加、會減少!!!
///float angle[20]={};//都是0的陣列 angle
void timer(int t)///上週程式,會增加、會減少!!!
{
glutTimerFunc( 30, timer, t+1 );///上週程式 設定新的timer
angle += diff;///上週程式,會增加、會減少!!!
if(angle>90) diff = -2;///上週程式,會增加、會減少!!!
if(angle<0) diff = +2;///上週程式,會增加、會減少!!!
glutPostRedisplay();///上週教display();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();///Step05-2
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///Step05-2
glTranslatef(-0.3,0,0);
glRotatef(angle, 0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
glPushMatrix();
glTranslatef(-0.3, 0,0);
glRotatef(angle, 0,0,1);
glTranslatef(-0.3, 0,0);
glutSolidTeapot( 0.3 );///左手肘
glPopMatrix();
glPopMatrix();///Step05-2
glPushMatrix();///Step05-2
glutSolidTeapot( 0.3 );///右手臂
glutSolidTeapot( 0.3 );///右手肘
glPopMatrix();///Step05-2
glPopMatrix();///Step05-2
glutSwapBuffers();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow( "week15 many angles" );

glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0);
glutMainLoop();
}///先不放貼圖, 先不放打光
單側茶壺手(3-1)
(3-1)
加入右手臂的程式碼:
     glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle, 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle, 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();
(3-2)

這樣就得到茶壺手臂了



沒有留言:

張貼留言

Week18期末作業(橘貓的跳舞熊熊)

 期末作業(橘貓的跳舞熊熊) 影片: https://youtu.be/R89tptMaQZw 程式碼: #include <opencv/highgui.h> #include <opencv/cv.h> #include <GL/glut.h...