Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Mandelbrot Set Coloring

Tags:

c

mandelbrot

I am working on the following code in C. So far it has all been working and it is zoomed to the correct level, etc, however I am struggling with getting the colors to work as I want. Ideally I would like to end up with something like this regardless of color:

Mandelbrot Set w/ correct colors

however my program as it is below currently produces something like this:

Current Mandelbrot Set

Therefore, I would appreciate any help I could get with making the colors turn out as I want them to.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define ITERMAX 100.0
#define DIVERGING 1.1
#define XMAX 500
#define YMAX 500
#define COLORINTENSITY 255

/* allow up to ITERMAX feedbacks searching for convergence
for the feedback
  z0 = 0 + 0i
  znew = z^2 + c
If we have not diverged to distance DIVERGING before ITERMAX feedbacks
we will assume the feedback is convergent at this value of c.
We will report divergence if |z|^2 > DIVERGING
*/

/* We will print color values for each pixel from (0, 0) to (XMAX, YMAX)
The color of pixel (cx, cy) will be set by convergent()
                                    or by divergent()
depending on the convergence or divergence of the feedback
when c = cx + icy
*/

/* The max value of the red, green, or blue component of a color */

void convergent();   /* one color for convergence */
void divergent();    /* a different color for divergence */

void feedback(double *x, double *y, double cx, double cy);
void pixel(char red, char green, char blue);
FILE *fp;


int main()
{
  fp = fopen("mandelbrot.ppm", "wb");
  double x, y, cx, cy;
  int iteration,squarex, squarey, pixelx, pixely;
  double grow=1.0;

/* header for PPM output */
fprintf(fp, "P6\n# CREATOR: EK, BB, RPJ via the mandel program\n");
fprintf(fp, "%d %d\n%d\n",XMAX, YMAX, COLORINTENSITY);

for (pixely = 0; pixely < YMAX; pixely++)  {
    for (pixelx = 0; pixelx < XMAX; pixelx++)  {
        cx = (((float)pixelx)/((float)XMAX)-0.5)/grow*3.0-0.7;
        cy = (((float)pixely)/((float)YMAX)-0.5)/grow*3.0;
        x = 0.0; y = 0.0;
        for (iteration=1;iteration<ITERMAX;iteration++)  {
                     feedback(&x, &y, cx, cy);
                     if (x*x + y*y > 100.0) iteration = 1000;
        }
        if (iteration==ITERMAX) {
          iteration = x*x + y*y;
          pixel((char) 0, (char) 0, (char) 0);
        }
        else {
          iteration = sqrt(x*x + y*y);
          pixel((char) iteration, (char) 0, (char) iteration);
               }
           }
    }
}

void feedback(double *x, double *y, double cx, double cy) {
/* Update x and y according to the feedback equation
 xnew = x^2 - y^2 + cx
 ynew = 2xy + cy
 (these are the real and imaginary parts of the complex equation:
  znew = z^2 + c)
*/
 double xnew = (*x) * (*x) - (*y) * (*y) + cx;
 double ynew  = 2 * *x * *y + cy;
 *x = xnew;
 *y = ynew;
}

void pixel(char red, char green, char blue)  {
/* put a r-g-b triple to the standard out */
 fputc(red, fp);
 fputc(green, fp);
 fputc(blue, fp);
}
like image 555
Kane Avatar asked May 02 '16 21:05

Kane


People also ask

How do you color the Mandelbrot set?

Adding colors to the Mandelbrot Set In order to add some colors, one could associate a color for each possible value of iterations. In the following example, we are switching from RGB colors to HSV (hue, saturation, value) colors. This allows us to change the color easily by modifying only the hue.

How are Mandelbrot fractals colored?

Every pixel that contains a point of the Mandelbrot set is colored black. Every pixel that does not cotain a point of the Mandelbrot set is colored using hue values depending on how close that point is to the Mandelbrot set.

Why does the Mandelbrot set have colors?

The color of each point represents how quickly the values reached the escape point. Often black is used to show values that fail to escape before the iteration limit, and gradually brighter colors are used for points that escape.

Is the Mandelbrot set a Julia set?

In other words, the Mandelbrot set forms a kind of index into the Julia set. A Julia set is either connected or disconnected, values of c chosen from within the Mandelbrot set are connected while those from the outside of the Mandelbrot set are disconnected.


1 Answers

To fix the banding, you need to iterate over your tables to find your maximum value for the iteration count, then scale your other values to be relative to this max (ie. normalize the values). You may also wish to logarithmically rescale the values to adjust the slope of the color-change.

And you probably don't want to work directly in RGB space. If you define your colors in HSB space, you can set a constant hue and saturation, and vary the brightness proportionally to the normalized iteration counts.

like image 116
luser droog Avatar answered Sep 26 '22 00:09

luser droog