Z是一个复数,x[0]=0,x[n+1]=x[n]^2+Z,使x[n](n->无穷大时)收敛的Z就是Mandelbrot集的元素。下面的程序近似表达了它从整体看的形状,这是一个分形。
// glut04.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
//把这个头文件放在最后
const int width=800,height=800;
const double xmin=-2, xmax=0.5, ymin=-1.25, ymax=1.25;
const double xstep=(xmax-xmin)/width;
const double ystep=(ymax-ymin)/height;
const int iterationtimes=64;
namespace w_h_
{
struct rgbColor
{
GLubyte red;
GLubyte green;
GLubyte blue;
void set(GLubyte r,GLubyte g,GLubyte b)
{
r=red; g=green; b=blue;
}
};
}
void pickColor( const std::complex
& z, struct w_h_::rgbColor& c )
{
std::complex
w;//zero
GLubyte iterations=0;
while(iterations
16) break; iterations++; } if(iterations==iterationtimes) c.red=c.green=c.blue=255; else { GLubyte grey=((double)iterations)/(iterationtimes-1)*254; c.red=c.green=c.blue=grey; } } void display() { int a,b; double x,y; struct w_h_::rgbColor c; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); for(a=0,x=xmin;a
(x,y),c); glColor3ub(c.red,c.green,c.blue); glVertex2i(a,b); } } glEnd(); glFlush(); } int _tmain(int argc, _TCHAR* argv[]) { glutInit(&argc,(char **)argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(width,height); glutInitWindowPosition(0,0); glutCreateWindow("sample"); glClearColor(1,1,1,0); gluOrtho2D(0,width,0,height); glutDisplayFunc(display); glutMainLoop(); return 0; }