User:Bensculfor/Plotting algorithms for the Mandelbrot set

Histogram coloring
A more complex coloring method involves using a histogram which pairs each pixel with said pixel's maximum iteration count before escape/bailout. This method will equally distribute colors to the same overall area, and, importantly, is independent of the maximum number of iterations chosen.

This algorithm has four passes. The first pass calculates the iteration counts associated with each pixel (but without any pixels being plotted). These are stored in an array, IterationCounts, where IterationCounts[x][y] is the number of iterations required to escape by the pixel at coordinates (x, y) on the screen.

The second pass creates the histogram of the number of pixels for each escape time value. This is stored in an array NumIterationsPerPixel, where NumIterationsPerPixel[i] is the number of pixels which require i iterations to escape (so the length of the array will be the maximum number of iterations). Pseudocode for this pass is:

for (x = 0; x < width; x++) do for (y = 0; y < height; y++) do i := IterationCounts[x][y] NumIterationsPerPixel[i]++

The third pass iterates through the NumIterationsPerPixel array and adds up all the stored values, saving them in total.

total: = 0 for (i = 0; i < max_iterations; i++) do total += NumIterationsPerPixel[i]

After this, the fourth pass begins and all the values in the IterationCounts array are indexed, and, for each iteration count i, associated with each pixel, the count is added to a global sum of all the iteration counts from 1 to i in the NumIterationsPerPixel array. This value is then normalized by dividing the sum by the total value computed earlier.

hue[][] := 0.0 for (x = 0; x < width; x++) do for (y = 0; y < height; y++) do iteration := IterationCounts[x][y] for (i = 0; i <= iteration; i++) do hue[x][y] += NumIterationsPerPixel[i] / total ''/* Must be floating-point division. */''

Finally, the computed value is used, e.g. as an index to a color palette.

This method may be combined with the smooth coloring method below for more aesthetically pleasing images.