// takes a point on the complex plane and determines if it is in the Set or not
// returns the normalised escape value (how many times it was iterated before escaping), or -1 if it is in the Set
// basically this function does z = z^2 + c, but normalises the result for smooth shading
double smoothsample (COMPLEX c)
{
unsigned int i;
double cardoid, z2s;
COMPLEX z, z2 = {0, 0};
// test if this point is in the cardoid or the bulb of the Set
cardoid = (c.r - 0.25f) * (c.r - 0.25f) + c.i * c.i;
if (
4 * cardoid * (cardoid + (c.r - 0.25f)) / c.i / c.i < 1 ||
16 * ((c.r + 1) * (c.r + 1) + c.i * c.i) < 1
)
return -1;
for (i = 0; i < maxIterations; i++)
{
// cache multiplication
z2.r = z.r * z.r;
z2.i = z.i * z.i;
// do actual iteration
z.i = 2 * z.r * z.i + c.i;
z.r = z2.r - z2.i + c.r;
// cache sum
z2s = z2.r + z2.i;
// not part of the Mandelbrot set; colour the pixel and immediately proceed to next pixel
if (z2s > escapeRadius)
return (double)i - log2(log(z2s) / 2);
}
return -1; // point is in the Set
}