Go Back
#include <iostream.h>
#include <math.h>
#include <GL/glut.h>
GLint width=620, height=230;
GLint mainWindow, iterWindow, graphWindow;
// Iteration to highlight
int nIter=1;
// Initial x value for all iterations
double xStart;
// Tolerance for checking equality of doubles
const double TOL = 1E-6;
// Overall window functions
void dispMain();
void reshape(int w, int h);
// Graph displaying functions
void display();
void mouse(int button, int state, int x, int y);
// Info displaying function
void dispIter();
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(width, height);
// Initialize the windows
mainWindow = glutCreateWindow("Bifurcate This!");
glutDisplayFunc(dispMain);
glutReshapeFunc(reshape);
iterWindow = glutCreateSubWindow(mainWindow, 10, 10, 600, 50);
glutDisplayFunc(dispIter);
graphWindow = glutCreateSubWindow(mainWindow, 10, 70, 600, 150);
glutDisplayFunc(display);
glutMouseFunc(mouse);
// Get the initial x value from the user
cout << "Starting x value? ";
cin >> xStart;
// Start!
glutMainLoop();
return 0;
}
void dispMain()
{
// Initialize overall window to black
glutSetWindow(mainWindow);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void reshape(int w, int h)
{
// If reshaping is attempted, restore everything
glutSetWindow(mainWindow);
glutReshapeWindow(width, height);
glutSetWindow(iterWindow);
glutPositionWindow(10, 10);
glutReshapeWindow(600, 50);
glutSetWindow(graphWindow);
glutPositionWindow(10, 70);
glutReshapeWindow(600, 150);
// Redraw.
glFlush();
glutPostRedisplay();
}
void display()
{
int i, j;
double temp;
double kval[600], xval[600];
double iters[600];
glutSetWindow(graphWindow);
glClearColor(0.5, 0.5, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(0.0, 4.0, 0.0, 1.0, -1.0, 1.0);
// DRAW AXES
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(4.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
// Draw tick marks
for (i = 0; i <= 4; i++)
{
glVertex3f(i, 0.0, 0.0);
glVertex3f(i, 0.04, 0.0);
}
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(0.04, 1.0, 0.0);
glEnd();
glBegin(GL_POINTS);
for (i = 0; i < 600; i++)
{
// One kval and xval for each pixel
kval[i] = (4.0/600.0)*double(i);
xval[i] = xStart;
}
for (i = 0; i < 200; i++)
{
glColor3f(0.0, 0.0, 1.0);
// Plot the current point
for (j = 0; j < 600; j++)
glVertex3f(kval[j], xval[j], 0.0);
// and then iterate it.
for (j = 0; j < 600; j++)
{
temp = kval[j]*xval[j];
xval[j] = temp - temp*xval[j];
// If this is the selected iteration, save the values
if (i == nIter) iters[j] = xval[j];
}
}
glEnd();
// Plot the selected iteration with bright red.
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 0.0, 0.0);
for (j = 0; j < 600; j++)
glVertex3f(kval[j], iters[j], 0.0);
glEnd();
}
void mouse(int button, int state, int x, int y)
{
// Left click = increase iterations, right click = decrease them.
if (state==GLUT_DOWN)
{
if (button == 0)
nIter++;
else if (nIter > 0)
nIter--;
display();
dispIter();
}
}
void dispIter()
{
int i, j, len;
if (len > 0) len = int(log10(nIter))+1;
double nIterCopy = nIter+TOL;
while (nIterCopy > 10-TOL) nIterCopy /= 10;
glutSetWindow(iterWindow);
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(0.0, 8.0, 0.0, 1.0, -1.0, 1.0);
// Display selected iteration number
char msg[13] = "Iteration: ";
glColor3f(0.0, 0.0, 0.0);
glRasterPos3f(1.0, 0.3, 0.0);
for (i = 0; i < 12; i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, msg[i]);
if (nIter == 0)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, '0');
else for (i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, char(int(nIterCopy)+'0'));
nIterCopy = nIterCopy - int(nIterCopy);
nIterCopy *= 10;
}
// Display personal info
char id[2][11] = {"Gary Sivek","Period 7 "};
for (i = 0; i < 2; i++)
{
glRasterPos3f(6.0, 0.65-0.4*double(i), 0.0);
for (j = 0; j < 10; j++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, id[i][j]);
}
}