# C곡선

아래와 같은 모양의 곡선을 C 곡선이라 한다. 모양이 영문자 'C' 와 닮았기 때문이다. 언뜻 아래와 같은 프랙탈을 만드는 것은 아주 어려울 것 같은 생각이 들 수도 있겠으나 거북이 그래픽과 재귀호출을 사용하면 믿어지지 않을 정도로 간단히 만들 수 있다.

![](https://2144407692-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M3x1x4RYzUAA01pfyth%2F-M3x1z5BX5jQshBXYC40%2F-M3x20m0wblGgdZesRvB%2Fim01.png?generation=1585867152143733\&alt=media)

## 알고리즘

1. 왼쪽으로 45 도 회전하여 선을 그린다.
2. 오른쪽으로 90 도 회전하여 선을 그린다.
3. 왼쪽으로 45 도 회전하여 전진 방향을 원래대로 돌려놓는다.

위의 알고리즘을 코드로 표현하면 아래와 같다.

```cpp
void RenderWindow::c_curve(int n)
{
    if(n == 0)
    {
        turtle.Forward(1.0f);
    }
    else
    {
        turtle.Left(45); c_curve(n-1);
        turtle.Right(90); c_curve(n-1);
        turtle.Left(45); 
    }
}
```

C 곡선의 알고리즘은 단순하다. 왼쪽으로 45 도 돌고 전진하고 오른쪽으로 90 도 돌고 전진하고 왼쪽으로 45 도 돈다. 이 단순한 알고리즘이 재귀적으로 반복하여 위와 같은 그림이 그려진다니 믿어지지 않을 정도이다. 재귀호출의 마력이다. 아래는 C 곡선을 구현한 프로그램의 실행 결과와 전체 코드다. 코드 중에 C 곡선 함수 구현 중 glFlush() 와 SwapBuffer() 함수가 있는데 이는 재귀 함수의 수행이 너무 길어질 경우 결과를 보려면 많은 시간을 기다려야 한다. 하지마 위의 함수를 호출해 주면 프랙탈이 그려지는 과정을 바로바로 볼 수 있게 된다.

![](https://2144407692-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M3x1x4RYzUAA01pfyth%2F-M3x1z5BX5jQshBXYC40%2F-M3x20m30W045xvb8j2C%2Fim03.jpg?generation=1585867152337475\&alt=media)

```cpp
#include "lib\egl.h"
#include "turtle.h"

using namespace egl;

class RenderWindow : public Window
{
private:
    Turtle turtle;
public:
    void c_curve(int n);
    virtual void RenderGLScene(void);
    virtual void OnSize(WPARAM wParam, LPARAM lParam);
};

void RenderWindow::OnSize(WPARAM wParam, LPARAM lParam)
{
    GLsizei width = LOWORD(lParam);
    GLsizei height = HIWORD(lParam);

    if (height == 0)
        height = 1; 

    glViewport( 0, 0, width, height ); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    gluPerspective(54.0f, GLfloat(width)/GLfloat(height), 1.0f, 1000.0f);

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
}

void RenderWindow::c_curve(int n)
{
    if(n == 0)
    {
        turtle.Forward(1.0f);
        glFlush();
        SwapBuffers(mDC);
    }
    else
    {
        turtle.Left(45); c_curve(n-1);
        turtle.Right(90); c_curve(n-1);
        turtle.Left(45); 
    }
}

void RenderWindow::RenderGLScene(void)
{
    Window::RenderGLScene();

    glTranslatef(0.0f, 0.0f, -400.0f);

    turtle.SetStartPoint(-90.0f, 0.0f, 0.0f);
    c_curve(15);
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    RenderWindow app;
    if(!app.Create(FALSE, "Fractal - C curve"))
        return EXIT_FAILURE;
    return app.Run();
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sungcheol-kim.gitbook.io/fractal-graphics-with-opengl/chapter7.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
