我想顯示mandelbrot集分形使用c編程,openGL,在linux上過剩。 這是我的代碼。它只在中心顯示一個點。無法弄清楚我出錯的地方。任何幫助?c編程 - Mandelbrot集
#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);
}
由於編碼問題解決了,我在編輯這個問題與所產生的分形:-)美的畫卷沿「正確的工作代碼」
這裏是工作代碼:
#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);
}
'absolute_x = cabs(x);'或''? –
chux
「它只在中心顯示一個點。」我已經編譯並運行你的代碼,並且至少在我的筆記本電腦上(Linux x86_64)從未調用過對'glBegin'的調用。 – starrify
你的循環看起來很奇怪:在'i'的每個循環中你都有一個相同的'z',並且我認爲你沒有從'j'的循環中得到正確的'z' – starrify