- #1
computerex
- 68
- 0
Hello. I am attempting to render the bifurcation diagram of the logistic difference equation in C with OpenGL. I think the math behind it is simple enough, but for some reason I am ending up with a structure that resembles the tree but doesn't look quite like it. I would really appreciate it if someone could point out where I am going wrong. Here is how it should look like:
http://brain.cc.kogakuin.ac.jp/~kanamaru/Chaos/e/BifArea/
http://planetmath.org/encyclopedia/FeigenbaumFractal.html
The following program produces the output attached to this post. As you can see, you can distinguish that the structure seems to be correct.
http://brain.cc.kogakuin.ac.jp/~kanamaru/Chaos/e/BifArea/
http://planetmath.org/encyclopedia/FeigenbaumFractal.html
The following program produces the output attached to this post. As you can see, you can distinguish that the structure seems to be correct.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>
#include <windows.h>
#include <math.h>
#define NUM_OF_ITT 100.0 // number of itterations
#define NUM_OF_SETS 1000 // number of ks that will be tested
double xstart = 0.1; // x0
double dataPoints[NUM_OF_SETS][(int)NUM_OF_ITT][2]; //
void calculatePlot(double k, unsigned int inx)
{
double result = k*xstart*(1.0-xstart);
dataPoints[inx][0][0]=k;
dataPoints[inx][0][1]=result;
for ( unsigned int i = 1; i < NUM_OF_ITT; i++ )
{
result = k * result*(1.0-result);
dataPoints[inx][i][0]=k;
dataPoints[inx][i][1]=result;
}
}
void handleKeyPress(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
}
}
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glLineWidth(1.0);
double k = 1;
double kstep = (4.0-k)/(double)NUM_OF_SETS;
for ( unsigned int i = 0; i < NUM_OF_SETS; i++ )
{
calculatePlot(k, i);
k+=kstep;
}
}
void handleResize(int q, int z)
{
glViewport(0, 0, q, z);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)q / (double)z, 1.0, 200.0);
}
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-3, 0, -5);
glColor3f(0.0, 1.0, 0.0);
// not rendering the transient states
glBegin(GL_LINE_STRIP);
for ( unsigned int set = 0; set < NUM_OF_SETS; set++ )
{
glVertex2f(dataPoints[set][0][0], dataPoints[set][0][1]);
}
glEnd();
glBegin(GL_LINE_STRIP);
for ( unsigned int set = 0; set < NUM_OF_SETS; set++ )
{
glVertex2f(dataPoints[set][(int)NUM_OF_ITT-1][0], dataPoints[set][(int)NUM_OF_ITT-1][1]);
}
glEnd();
glutSwapBuffers();
}
void update(int value)
{
glutTimerFunc(10, update, 0);
}
void idlefunc()
{
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Logistcs");
initRendering();
glutDisplayFunc(drawScene);
glutIdleFunc(idlefunc);
glutKeyboardFunc(handleKeyPress);
glutReshapeFunc(handleResize);
glutMouseFunc(mouse);
glutTimerFunc(100, update, 0);
glutMainLoop();
return 0;
}
Attachments
Last edited by a moderator: