Creating an image of The Mandelbrot Set with PNGwriter


To show PNGwriter's graphical abilities, I'll show you how to create an image of the Mandelbrot set using PNGwriter.

In simple terms, the Mandelbrot Set is a subset of the complex plane. It contains those points that do not diverge when they are taken as the starting condition for an equation which is iterated over and over a certain number of times. The image corresponds to a square of initial conditions in the complex plane, and the colour of the pixel tells us whether that pixel belongs or does not to the Mandelbrot Set. If it does belong, it is coloured black. If it diverges, it's colour corresponds to the rate at which it diverges.


This is the image that the program makes... literally!! I ran the program, and copied mandel1.png to my webserver's appropriate directory. No conversion, no changes, no work needed!.

PNGwriter can obviously generate colour images (using the standard R, G, B or Hue, Saturation, Value triplets). I have made this image in grayscale for aesthetic reasons.








The most important thing to remember is that the colour coefficients go from 0 to 65535 and are of int type. Since version 0.3.2, you can also use colour coefficients that go from 0.0 to 1.0 and are of double type.



#include "src/pngwriter.h"  //  Remember to include pngwriter.h!!.

#include <iostream.h>

#include <math.h>



double rmin = -1.428;

double rmax = -1.3785;

double imin = 0.000937;

double imax = 0.0484;

double  iteratelimit = 150;



double  a,b,i;



double xwidth = 600;

double yheight =600;



double maxcolours = 65535;



double xx1,yy1,xx2,yy2,radius;

double count;







int main()

{

   

pngwriter png1(xwidth,yheight,65535,"mandel1.png"); //  This is the constructor.

   

for(a=0;a & <xwidth;a++)

   {

      for(b=0;b<yheight;b++)

         {

            xx1 = 0;

	     

            yy1 = 0;

	     

            count = 0;

	     

            i=0;

	    

            radius = 0;

	     

            while((radius<2)&&(i<iteratelimit))

               {

                  xx2 = xx1*xx1 - yy1*yy1 + a*((rmax-rmin)/xwidth)+rmin;

		  

                  yy2 = 2*xx1*yy1 + b*((imax-imin)/yheight)+imin;

		  

                  radius = sqrt(xx2*xx2 +yy2*yy2);

		  

                  count = count + 1;

		  

                  xx1 = xx2;

		  

                  yy1 = yy2;

	

                  i++;

               }

	     //  This is how to plot a point.

            png1.plot(a,b,maxcolours - maxcolours*count/iteratelimit,

                                            (maxcolours - maxcolours*count/iteratelimit),

                                             maxcolours - maxcolours*count/iteratelimit);

	     

            if(i == iteratelimit)

               {

                  png1.plot(a,b,0,0,0);  //  The plot function used more clearly.

               }

            }

         }

   

   //  Remember to close this object!!.

      png1.close(); 

   

   return 0;

}



Valid HTML 4.01! Valid CSS!


© 2002, 2003, 2004, 2005, 2006, 2007 Paul Blackburn