OpenGL-海龜作圖法

一月 16th, 2007

海龜作圖法(Turtle Graphics),意思是模仿海龜移動的方式,只能前進及左右轉,而將海龜的尾巴當作畫筆,可以放下及抬起。
本範例為二維空間,使用 OpenGL 搭配 C++ 實做。

程式功能介紹:
1、由 turtle.txt 檔案讀入作圖指令。
2、INIT - 設定海龜初始位置。(x 軸, y 軸, 龜頭角度)
3、PEN - 設定尾巴狀態。(0 抬起 / 1放下)
4、FW - 海龜向前移動。(移動長度)
5、RIGHT - 海龜右轉角度。(角度)
6、LEFT - 海龜左轉角度。(角度)


#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <GL/glut.h>
using namespace std;

const GLfloat PI = 3.141593;

class Turtle {
private:
    bool penStatus, a;
    GLfloat x1, y1, x2, y2; //座標
    GLfloat cAngle; //角度
public:
    Turtle() //constractor
    {
        penStatus = 0;
        x1 = y1 = x2 = y2 = 0.0;
        cAngle = 0.0;
        a = 0;
    }

    /* 設定海龜位置 */
    void InitPoint(GLfloat x, GLfloat y, GLfloat angle)
    {
        x1 = x; y1 = y; cAngle = angle; a = 1;
        if (penStatus) //如果 pen down 應該畫出一點
        {
            glBegin(GL_POINTS);
                glVertex2f(x, y);
            glEnd();
        }
    }
    /* 海龜向前移動 */
    void forward(GLfloat L)
    {
        x2 = x1 + L * cos(cAngle*PI/180);
        y2 = y1 + L * sin(cAngle*PI/180);

        if (penStatus)
        {
            glBegin(GL_LINES);
                glVertex2f(x1, y1);
                glVertex2f(x2, y2);
            glEnd();
        }

        x1 = x2; y1 = y2;
    }
    /* 海龜右轉角度 */
    void right(GLfloat angle)
    {
        cAngle = (a) ? cAngle - angle : angle;
        a = 1;
    }
    /* 海龜左轉角度 */
    void left(GLfloat angle)
    {
        cAngle = (a) ? cAngle + angle : angle;
        a = 1;
    }
    /* 設定畫筆狀態(0 up/1 down) */
    void setPen(bool value)
    {
        penStatus = value;
		if (value) //pen down 畫出一點
		{
			glBegin(GL_POINTS);
				glVertex2f(x1, y1);
			glEnd();
		}
    }

};

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */

    char cmd[10];
    float x, y, angle;
    bool p;
    ifstream fp("turtle.txt");
    Turtle t;

    while (!fp.eof())
    {
        fp >> cmd;
        if (!strcmp(cmd, "INIT")) {
            fp >> x >> y >> angle;
            t.InitPoint(x, y, angle);
        } else if (!strcmp(cmd, "FW")) {
            fp >> x;
            t.forward(x);
        } else if (!strcmp(cmd, "RIGHT")) {
            fp >> x;
            t.right(x);
        } else if (!strcmp(cmd, "LEFT")) {
            fp >> x;
            t.left(x);
        } else if (!strcmp(cmd, "PEN")) {
            fp >> p;
            t.setPen(p);
        }
    }

    glFlush(); /* clear buffers */
}

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

/* Standard GLUT initialization */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500, 500); /* 500 x 500 pixel window */
    glutInitWindowPosition(0, 0); /* place window top left on display */
    glutCreateWindow("Turtle Graphics"); /* window title */
    glutDisplayFunc(display); /* display callback invoked when window opened */

/* attributes */
    glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
    glColor3f(1.0, 0.0, 0.0); /* draw in red */
/* set up viewing */
/* 500 x 500 window with origin lower left */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 500.0, 0.0, 500.0);
    glMatrixMode(GL_MODELVIEW);

    glutMainLoop(); /* enter event loop */

    return 0;
}

turtle.txt

C/C++, 程式筆記 ,

  1. 網頁設計
    八月 4th, 2009 at 10:58 | #1

    謝謝分享,我試看看喔~~

  2. hckuo
    三月 1st, 2010 at 20:42 | #2

    excellent!!

  3. victorinoxknivesset.info
    十一月 30th, 2011 at 05:18 | #3

    Well, finally a post that puts a smile on my face....good input and I hope you can keep the level of upcoming post high.

  4. keurig-b60.info
    十二月 1st, 2011 at 02:51 | #4

    Good post...glad to see you put some thought into it. Hope that we get more input than what we have so far.

  1. 二月 5th, 2007 at 08:22 | #1