Reputation: 9920
I am trying to display Mandelbrot set fractal using c programming, openGL, glut on linux. Here is my code. it only displays a dot in the center. not able to figure out where I am going wrong. any help?
#include <GL/glut.h> // Header File For The GLUT Library
#include <math.h>
#include <complex.h>
int main (int argc, char *argv[])
{
int i, j, count;
float re_max=1.0, re_min=-2.0, im_max=1.0, im_min=-1.0 ; //aspect ratio 3/2
float real_delta = (re_max - re_min)/750;
float imag_delta = (im_max - im_min)/500;
double complex x = 0.0 + 0.0 * I;
double complex z = re_min + im_min * I;
double absolute_x;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(150,150);
glutInitWindowSize(750,500); // aspect ratio of 3/2
glutCreateWindow (argv[0]);
glClearColor (0.1, 0.2, 0.3, 0.0); // choosing the background color
glClear (GL_COLOR_BUFFER_BIT); // setting the color buffer to background color
glColor4f(0.5,0.7,0.3,0.0);
glPointSize(1);
for (i=0, z = re_min + im_min * I ; i<750; i++, z = (re_min + real_delta) + im_min * I)
{
for (j=0, z = creal(z) + im_min * I; j<500; j++, z = creal(z) + (im_min + imag_delta) * I)
{
count = 0;
x = 0 + 0*I;
while ((absolute_x = fabs(x))<=2.0 && count < 64)
{
x = (x * x) + z;
count++;
}
if (absolute_x <= 2.0){
glBegin(GL_POINTS);
glVertex2i(i,j);
glEnd();
}
}
}
glFlush();
glutMainLoop();
return (0);
}
Since the coding issue is solved, I am editing this question with the 'correct working code' along with a beautiful picture of the resulting fractal :-)
here is the working code:
#include <GL/glut.h> // Header File For The GLUT Library
#include <math.h>
#include <complex.h>
int main (int argc, char *argv[])
{
int i, j, count;
float re_max=1., re_min=-2.0, im_max=1.0, im_min=-1.0 ; //aspect ratio 3/2
float real_delta = (re_max - re_min)/750;
float imag_delta = (im_max - im_min)/500;
double complex x = 0.0 + 0.0 * I;
double complex z = re_min + im_min * I;
double absolute_x;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(150,150);
glutInitWindowSize(750,500); // aspect ratio of 3/2
glutCreateWindow (argv[0]);
glClearColor (0.5, 0.2, 0.3, 0.0); // choosing the background color
glClear (GL_COLOR_BUFFER_BIT); // setting the color buffer to background color
glColor4f(0.5,0.7,0.3,0.0);
glPointSize(1);
for (i=0, z = re_min + im_min * I ; i<750; i++, z = (creal(z) + real_delta) + im_min * I)
{
for (j=0, z = creal(z) + im_min * I; j<500; j++, z = creal(z) + (cimag(z) + imag_delta) * I)
{
count = 0;
x = 0 + 0*I;
while ((absolute_x = fabs(x))<=2.0 && count < 64)
{
x = (x * x) + z;
count++;
}
if (absolute_x <= 2.0){
glBegin(GL_POINTS);
glVertex2f(i / 750., j / 500.);;
glEnd();
}
}
}
glFlush();
glutMainLoop();
return (0);
}
Upvotes: 1
Views: 2026
Reputation: 14751
There're two major mistakes:
Your loops looks weird. The loop of i
don't change z
's value in each of it's iteration, and so is j
.
Suggestion: don't update z
in the loop statement which would only make things complicated, instead calculate it directly in the loop body like this:
for (i=0; i<750; i++)
{
for (j=0; j<500; j++)
{
z = re_min + real_delta * i + (im_min + imag_delta * j) * I;
/* other of your codes */
}
}
You're not using OpenGL
the right way. By invoking glVertex2i(i, j);
you're specifying the coordinates x=i
and y=j
, but the default viewport of OpenGL
is (-1,1)x(-1,1)
.
Here're two solutions you could use:
Placing a glScalef
after created the window to specify the scales on coordinates and don't change other part of your code:
/* ... */
glutCreateWindow(argv[0]);
glScalef(1. / 750., 1. / 500., 1.);
/* ... */
Passing scaled coordinates:
/* don't use this line of your code: */
/* glVertex2i(i, j); */
/* while use this line: */
glVertex2f(i / 750., j / 500.);
Upvotes: 4