-
Notifications
You must be signed in to change notification settings - Fork 0
/
koch3d.cpp
297 lines (294 loc) · 6.83 KB
/
koch3d.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include<iostream>
#include<cmath>
#if defined(__gnu_linux__)||defined(__linux__)
#include<unistd.h>
#endif
#if defined(_WIN32)||defined(__WINDOWS__)||defined(_WIN64)
#include<windows.h>
#endif
#include<cstdlib>
#include<ctime>
#include<cstdio>
#include"graph.h"
#include"printw.h"
using namespace std;
int s,choice=1,g;
static int power=s;
float init_size=1;
int cx, cy;
static GLfloat scaling = 1.0f;
static int lasty = 0;
typedef float point[3];
point init_tetr[]={{1,1,1},{1,-1,-1},{-1,1,-1},{-1,-1,1}};
static GLfloat theta[]={0.0,0.0,0.0,0.0};
static GLint axis=2;
point color[] = {{1,0,0},{0,1,0},{0,0,1},{1,1,0},{0,1,1},{1,0,1},{0.5,0.5,1},{0,0.5,0.5},{0.8,0,0.7}};
void spread(point a, point b, point c,point d, int m);
void triangle(point a, point b, point c, int m)
{
glColor3fv(color[(choice+g-m)%9]);
//glColor3fv(color[choice]);
glBegin(GL_TRIANGLES);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
void tetra(point a, point b, point c, point d, int m)
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(b);
glVertex3fv(c);
glVertex3fv(c);
glVertex3fv(d);
glVertex3fv(d);
glVertex3fv(a);
glVertex3fv(a);
glVertex3fv(c);
glVertex3fv(b);
glVertex3fv(d);
glEnd();
triangle(a,b,c,m);
triangle(a,b,d,m);
triangle(b,c,d,m);
triangle(c,a,d,m);
}
void findvertex(point a, point b, point c, int m)
{
point u,v,w,centroid,normal,p,q,peak;
float scale,size2,size;
for(int i=0;i<3;i++)
{
u[i]=(a[i]+b[i])/2;
v[i]=(b[i]+c[i])/2;
w[i]=(c[i]+a[i])/2;
}
for(int i=0;i<3;i++)
{
centroid[i]=(a[i]+b[i]+c[i])/3;
p[i]=c[i]-a[i];
q[i]=b[i]-a[i];
//cout << "p "<<i<<" : "<<p[i]<< endl;
//cout << "q "<<i<<" : "<<q[i]<< endl;
}
for(int i=0;i<3;i++)
{
u[i]=(a[i]+centroid[i])/2;
v[i]=(b[i]+centroid[i])/2;
w[i]=(c[i]+centroid[i])/2;
}
normal[0]=p[1]*q[2]-p[2]*q[1];
normal[1]=p[2]*q[0]-p[0]*q[2];
normal[2]=p[0]*q[1]-p[1]*q[0];
for(int i=0;i<3;i++)
{
//normal[i]=normal[i]*sqrt(2/3)*size/scale;
//cout << "normal "<< i <<" : "<<(float)normal[i]<< endl;
//peak[i]=centroid[i]-normal[i];
}
scale = sqrt(pow(normal[0],2)+pow(normal[1],2)+pow(normal[2],2));
size2 = pow((u[0]-v[0]),2)+pow((u[1]-v[1]),2)+pow((u[2]-v[2]),2);
size = sqrt(size2);
//cout << "size "<<" : "<<size<< endl;
//cout << "scale "<<" : "<<scale<< endl;
for(int i=0;i<3;i++)
{
normal[i]=normal[i]*0.816496;
//cout << "normal "<< i <<" : "<<(float)normal[i]<< endl;
normal[i]=normal[i]*size;
//cout << "normal "<< i <<" : "<<(float)normal[i]<< endl;
normal[i]/=scale;
//normal[i]*=init_size;
//cout << "normal "<< i <<" : "<<(float)normal[i]<< endl;
peak[i]=centroid[i]-normal[i];
}
spread(u,v,w,peak,m-1);
sleep(0.1);
/*
if(m>1)
{
findvertex(a,u,w,m-1);
findvertex(v,u,b,m-1);
findvertex(c,w,v,m-1);
}
*/
}
void spread(point a, point b, point c, point d, int m)
{
//point v1,v2,v3;
tetra(a,b,c,d,m);
//glColor3f(1.0,1.0,1.0);
//glBegin(GL_LINES);
// glVertex3fv(a);
// glVertex3fv(b);
//glEnd();
int j;
if(m>0)
{
findvertex(a,b,d,m-1);
findvertex(b,c,d,m-1);
findvertex(c,a,d,m-1);
}
else
return;
}
void koch(int m)
{
//glColor3fv(color[choice]);
tetra(init_tetr[0],init_tetr[1],init_tetr[2],init_tetr[3],g);
/*glBegin(GL_POINTS);
glPointSize(5.0);
glColor3f(0.5,0.5,0.5);
glVertex3fv(init_tetr[0]);
glVertex3fv(init_tetr[1]);
glVertex3fv(init_tetr[2]);
glVertex3fv(init_tetr[3]);
glEnd();*/
glFlush();
if(m>0)
{
//glColor3fv(color[(choice++)%3]);
findvertex(init_tetr[1],init_tetr[0],init_tetr[3],m);
findvertex(init_tetr[2],init_tetr[1],init_tetr[3],m);
findvertex(init_tetr[0],init_tetr[2],init_tetr[3],m);
findvertex(init_tetr[0],init_tetr[1],init_tetr[2],m);
}
}
void myReshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat r = (GLfloat)h/(GLfloat)w;
if(w<=h)
glOrtho(-5,5,-5*r,5*r,-10,10);
else
glOrtho(-5/r,5/r,-5,5,-10,10);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
/*
void idle_display()
{
//static int i=0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor(1,1,1,1);
g=s;
//printw (-1.6, 0.2, 0, "%s : %d", "Succession number", g);
//glColor3fv(color[choice]);
power=g-1;
koch(g);
glFlush();
sleep(1);
//i++;
//if(i==s+1) i=0;
glutPostRedisplay();
}
*/
void zoom(int x, int y)
{
glutReshapeWindow(cx,cy);
int movy;
movy = lasty - y;
if ( abs(movy) < 10 ){ scaling += (float)(movy) / 100.0f; }
if ( scaling < 1.0) scaling = 1.0f;
if ( scaling > 10.0f) scaling = 10.0f;
lasty = y;
glutPostRedisplay();
}
void displayer()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glClearColor(1.0,1.0,1.0,1.0);
glRotatef(theta[0],1,0,0);
glRotatef(theta[1],0,1,0);
glRotatef(theta[2],0,0,1);
koch(s);
glFlush();
glutSwapBuffers();
//int vx = glutGet(GLUT_WINDOW_WIDTH);
//int vy = glutGet(GLUT_WINDOW_HEIGHT);
//glTranslatef(init_size*scaling/10,init_size*scaling/10,0);
glScalef(scaling, scaling, scaling);
}
void init_tetra(float size)
{
//init_size=3;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
init_tetr[i][j]*=init_size;
}
void spintetra()
{
sleep(0.95);
theta[axis]+=2;
if(theta[axis]>360)
theta[axis]-=360;
glutPostRedisplay();
}
void mouse(int btn,int state,int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) axis =0;
glRotatef(theta[0],1,0,0);
if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) axis =1;
glRotatef(theta[1],0,1,0);
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) axis =2;
glRotatef(theta[2],0,0,1);
glFlush();
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
int m_width=600;
int m_height=600;
#if !(defined(__gnu_linux__)||defined(__linux__))
cout << "Enter the number of successions : [0-7] ";
cin >> s;
//s=0;
cout << "Next you will have to enter the size of the triangle, a float point value between 0 and 5." << endl;
cout << "Enter the initial size of the triangle : ";
cin >> init_size;
if(init_size>5)
{
cout << "Maximum limit of size is 5. Restart the program. Bye!";
exit(0);
}
cout << "Enter the initial color of the triangle you wish to see : \n1. RED \n2. GREEN \n3. BLUE \n Enter your choice :";
cin >> choice;
choice-=1;
g=0;
#else
char *pEnd;
if(argc==0)
{
cout << "No args.";
exit(0);
}
s = strtod(argv[1],&pEnd);
init_size = strtod(pEnd,&pEnd);
choice = strtod(pEnd,NULL);
choice--;
g=0;
#endif
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(m_width,m_height);
cx = glutGet(GLUT_SCREEN_WIDTH);
cy = glutGet(GLUT_SCREEN_HEIGHT);
glutInitWindowPosition ((cx-m_width) / 2, (cy-m_height) / 2);
glutCreateWindow("Koch curve");
//init_tetra(init_size);
glutDisplayFunc(displayer);
glutIdleFunc(spintetra);
glutMouseFunc(mouse);
glutMotionFunc(zoom);
glutReshapeFunc(myReshape);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glutMainLoop();
return 0;
}