#include <stdlib.h>
#include <stdio.h>
#include <math.h>
const int countLimit = 800;
int MandelSetCount( double startx, double starty, int limit)
{
int count;
double x=startx, y=starty;
double newx, newy, funSq;
funSq = startx*startx + starty*starty;
for (count=0; count<limit && funSq<4.0; count++ )
{
newx = x*x - y*y + startx;
newy = 2.0*x*y + starty;
funSq = newx*newx + newy*newy;
x = newx; y = newy;
}
return count ;
}
int main(int argc, char *argv[])
{
int r, nrows, c, ncols ;
int red, grn, blu;
double x, y ;
const double xmin = -1.50, xmax = 1.00;
const double ymin = -1.25, ymax = 1.25;
FILE *image;
if ( argc != 4 )
{
printf("mandel fileName.ppm nrows ncols\n");
return 0;
}
/* open the image file for writing in binary mode */
if ( (image = fopen( argv[1], "wb") ) == NULL )
{
printf("file %s could not be created\n", argv[1]);
return 0;
}
nrows = atoi( argv[2] );
ncols = atoi( argv[3] );
/* write out the PPM Header information */
fprintf( image, "P6 ");
fprintf( image, "%d %d %d ", ncols, nrows, 255 );
/* write out the pixel data */
for ( r=0; r<nrows; r++ )
for ( c=0; c<ncols; c++ )
{
x = xmin + (xmax-xmin)*c/ncols;
y = ymin + (ymax-ymin)*(nrows-r)/nrows;
if ( MandelSetCount( x, y, countLimit )==countLimit )
{
red = 0; grn = 0; blu = 0;
}
else
{
red = 220; grn = 250; blu = 220;
}
fputc( red, image ); fputc( grn, image ); fputc( blu, image );
}
/* close the file */
fclose ( image );
return 1;
}
Comments: Creating a more colorful image is up to you.