我正在尝试使用c编程,openGL,linux上的过剩来显示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);
}
答案 0 :(得分:4)
有两个主要错误:
你的循环看起来很怪异。 i
的循环在每次迭代中都不会更改z
的值,j
也是如此。
建议:不要在循环语句中更新z
,这只会使事情变得复杂,而是直接在循环体中计算它,如下所示:
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 */
}
}
您没有以正确的方式使用OpenGL
。通过调用glVertex2i(i, j);
,您指定了坐标x=i
和y=j
,但OpenGL
的默认视口为(-1,1)x(-1,1)
。
以下是您可以使用的两种解决方案:
在创建窗口后放置glScalef
以指定坐标上的比例,并且不要更改代码的其他部分:
/* ... */
glutCreateWindow(argv[0]);
glScalef(1. / 750., 1. / 500., 1.);
/* ... */
传递缩放坐标:
/* don't use this line of your code: */
/* glVertex2i(i, j); */
/* while use this line: */
glVertex2f(i / 750., j / 500.);