//A.14 Mandelbrot集程序
#include<stdio.h>
#include<stdlib.h>
#include<gl/glut.h>
/* default data */
#define CENTERX -0.5
#define CENTERY 0.5
#define HEIGHT 0.5
#define WIDTH 0.5
#define MAX_ITER 100
/* N * M array to be generated */
#define N 500
#define M 500
float height=HEIGHT;
float width = WIDTH;
float cx = CENTERX;
float cy = CENTERY;
int max = MAX_ITER;
int n=N;
int m=M;
/* use unsigned bytes for image */
GLubyte image[N][M];
/* complex data type and complex add,mult,and magnitude functions
probably not worth overloaded */
typedef float complex[2];
void add(complex a,complex b,complex p)
{
p[0]=a[0]+b[0];
p[1]=a[1]+b[1];
}
void mult(complex a,complex b,complex p)
{
p[0]=a[0]*b[0]-a[1]*b[1];
p[1]=a[0]*b[1]+a[1]*b[0];
}
float mag2(complex a)
{
return (a[0]*a[0]+a[1]*a[1]);
}
void form(float a,float b,complex p)
{
p[0]=a;
p[1]=b;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(n,m,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,image);
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D(0.0, 0.0, (GLfloat) n,(GLfloat)m* (GLfloat ) h /
(GLfloat) w);
else
gluOrtho2D(0.0, 0.0, (GLfloat)n*(GLfloat) w / (GLfloat) h,
(GLfloat) m);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void myinit()
{
float redmap[256],greenmap[256],bluemap[256];
int i;
glClearColor (1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0.0, 0.0, (GLfloat)n, (GLfloat)m);
/* define pseudocolor maps,ramps for red and blue,
random for green */
for(i=0;i<256;i++)
{
redmap[i]=i/255.0;
greenmap[i]=rand()%255/255.0;
bluemap[i]=1.0-i/255;
}
glPixelMapfv(GL_PIXEL_MAP_I_TO_R,256,redmap);
glPixelMapfv(GL_PIXEL_MAP_I_TO_G,256,greenmap);
glPixelMapfv(GL_PIXEL_MAP_I_TO_B,256,bluemap);
}
void main(int argc, char **argv)
{
int i,j,k;
float x,y,v;
complex c0,c,d;
scanf("%f",&cx);/*center x*/
scanf("%f",&cy); /*center y*/
scanf("%f",&width); /*rectangle width*/
height = width; /*rectangle height*/
scanf("%d",&max); /*maximum iteration*/
for(i=0;i<n;i++)for(j=0;j<m;j++)
{
/* starting point */
x=i*(width/(n-1))+cx-width/2;
y=j*(height/(m-1))+cy-height/2;
form(0,0,c);
form(x,y,c0);
/* complex iteration */
for(k=0;k<max;k++)
{
mult(c,c,d);
add(d,c0,c);
v=mag2(c);
if(v>4.0) break ;/* assume not in set if mag>4 */
}
/*assign gray level to point based on its magnitude*/
if(v>1.0) v=1.0; /* clamp if v>1 */
image[i][j]=255*v;
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(N,M);
glutCreateWindow("Mandelbrot set");
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
}
[OpenGL]课后案例14:Mandelbrot集程序
最新推荐文章于 2021-04-17 11:11:24 发布