Mandelbrot set

From Rosetta Code
Revision as of 08:16, 31 March 2009 by Ce (talk | contribs) (→‎{{header|C}}: A note about the C code, added C++)
This page uses content from Wikipedia. The original article was at Mandelbrot_set. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)
Task
Mandelbrot set
You are encouraged to solve this task according to the task description, using any language you may know.

Generate and draw the Mandelbrot set.

C

Works with: gcc

This code assumes there exists a function DrawPixel taking as arguments the x and y position, followed by the red, green and blue components of the color. Note that this function is not part of standard C. <lang C>

  1. define MAX_ITERATIONS 500

void draw(int width, int height){

 int x, y, colour;
 double x0, y0, xtemp, cr, ci;
 for (x = 0; x < width, x++){
   for (y = 0; y < height; y++){
     x0 = y0 = 0;
     cr = ((double)x / (double)width - 0.5);
     ci = ((double)y / (double)height - 0.5);
     i = 0;
     while (x0*x0 + y0*y0 <= 4.0 && i < MAX_ITERATIONS){
       xtemp = x0*x0 - y0*y0 + cr;
       y0 = 2*x0*0 + ci;
       x0 = xtemp;
       i++;
     }
     /* points in the set are coloured as white, others are coloured black. */
     colour = (i == MAX_ITERATIONS ? 255 : 0);
     DrawPixel(x, y, colour, colour, colour);
   }
 }

} </lang>

C++

This generic function assumes that the image can be accessed like a two-dimensional array of colors. It may be passed a true array (in which case the Mandelbrot set will simply be drawn into that array, which then might be saved as image file), or a class which maps the subscript operator to the pixel drawing routine of some graphics library. In the latter case, there must be functions get_first_dimension and get_second_dimension defined for that type, to be found by argument dependent lookup. The code provides those functions for built-in arrays. <lang cpp>

  1. include <cstdlib>
  2. include <complex>

// get dimensions for arrays teplate<typename ElementType, std::size_t dim1, std::size_t dim2>

std::size_t get_first_dimension(ElementType (&a)[dim1][dim2])

{

 return dim1;

}

teplate<typename ElementType, std::size_t dim1, std::size_t dim2>

std::size_t get_second_dimension(ElementType (&a)[dim1][dim2])

{

 return dim2;

}


template<typename ColorType, typename ImageType>

void draw_Mandelbrot(ImageType& image,                                       // where to draw the image
                     ColorType set_color, ColorType non_set_color,           // which colors to use for set/non-set points
                     double cxmin, double cxmax, double cymin, double cymax,  // the rectangle to draw in the complex plane
                     unsigned int max_iterations)                            // the maximum number of iterations

{

 std::size_t const ixsize = get_first_dimension(ImageType);
 std::size_t const iysize = get_first_dimension(ImageType);
 for (std::size_t ix = 0; ix < ixsize; ++ix)
   for (std::size_t iy = 0; iy < iysize; ++iy)
   {
     std::complex c(cxmin + ix/(ixsize-1.0)*(cxmax-cxmin), cymin + iy/(iysize-1.0)*(cymax-cymin));
     std::complex z = 0;
     unsigned int iterations = 0;
     while (iterations < max_iterations && std::norm(z) < 4.0) // note: std::norm(z) gives the *squared* absolute value of z
     {
       z = z*z + c;
       ++iterations;
     }
     if (iterations == max_iterations)
       image[ix][iy] = set_color;
     else
       image[ix][iy] = non_set_color;
   }

} </lang>