-
Notifications
You must be signed in to change notification settings - Fork 3
/
koch_anti_snow_flake.cpp
116 lines (99 loc) · 2.73 KB
/
koch_anti_snow_flake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// Author: NagaChaitanya Vellanki
//
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
static float LINE_LENGTH = 5.0f;
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
// constants : + −
// axiom = F++F++F
// rule = F → F+F--F+F
// angle = 60°
// + turn left by 60 degrees
// - turn right by 60 degrees
std::string kochSnowflakeLSystemRepresentation() {
std::string current("F++F++F");
int generation = 0;
while(generation < 5) {
std::string next;
for(std::string::iterator it = current.begin(); it != current.end(); it++) {
switch(*it) {
case 'F':
next.append("F+F--F+F");
break;
case '-':
next.append("-");
break;
case '+':
next.append("+");
break;
};
}
current = next;
generation++;
//std::cout << "Generation: " << " " << current << std::endl;
}
return current;
}
int main(int argc, char *argv[]) {
GLFWwindow* window;
if(!glfwInit()) {
return -1;
}
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* vidmode = glfwGetVideoMode(monitor);
window = glfwCreateWindow(vidmode->width, vidmode->height, "Koch Antisnowflake", monitor, NULL);
if(!window) {
glfwTerminate();
return -1;
}
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
float x = 0.0f;
float y = 0.0f;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, width, 0.0f, height, 0.0, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
std::string lrep = kochSnowflakeLSystemRepresentation();
while(!glfwWindowShouldClose(window)) {
x = width / 3.5f;
y = height / 8.0f;
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
for(std::string::iterator it = lrep.begin(); it != lrep.end(); it++) {
if(*it == 'F') {
glColor4f(0.8588f, 1.0f, 1.0f, 1.0f);
glLineWidth(4.0f);
GLfloat vertices[] = {x, y, x + LINE_LENGTH, y};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(GL_LINES, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
x = x + LINE_LENGTH;
}
if(*it == '-' || *it == '+') {
glTranslatef(x, y, 0.0f);
x = 0.0f;
y = 0.0f;
float angle = ((*it == '-') ? -60.0f : 60.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
}
}
glPopMatrix();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}