mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:27:45 +00:00
Mandelbrot: Implement color smoothing with gradients
This removes the color banding that happens for some of the "outer" areas which all have the same iteration count.
This commit is contained in:
parent
c961616e6d
commit
b429ee88f8
1 changed files with 20 additions and 5 deletions
|
@ -45,7 +45,7 @@ public:
|
||||||
calculate();
|
calculate();
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 mandelbrot(double px, double py, i32 max_iterations)
|
double mandelbrot(double px, double py, i32 max_iterations)
|
||||||
{
|
{
|
||||||
// Based on https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set
|
// Based on https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set
|
||||||
const double x0 = px * (m_x_end - m_x_start) / m_bitmap->width() + m_x_start;
|
const double x0 = px * (m_x_end - m_x_start) / m_bitmap->width() + m_x_start;
|
||||||
|
@ -64,15 +64,30 @@ public:
|
||||||
iteration++;
|
iteration++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (iteration == max_iterations)
|
||||||
return iteration;
|
return iteration;
|
||||||
|
|
||||||
|
auto lz = sqrt(x * x + y * y) / 2;
|
||||||
|
return 1 + iteration + log(lz / log(2)) / log(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static double linear_interpolate(double v0, double v1, double t)
|
||||||
|
{
|
||||||
|
return v0 + t * (v1 - v0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void calculate_pixel(int px, int py, int max_iterations)
|
void calculate_pixel(int px, int py, int max_iterations)
|
||||||
{
|
{
|
||||||
auto iterations = mandelbrot(px, py, max_iterations);
|
auto iterations = mandelbrot(px, py, max_iterations);
|
||||||
double hue = (double)iterations * 360.0 / (double)max_iterations;
|
auto whole_iterations = floor(iterations);
|
||||||
if (hue == 360.0)
|
auto partial_iterations = fmod(iterations, 1);
|
||||||
hue = 0.0;
|
double hue1 = whole_iterations * 360.0 / max_iterations;
|
||||||
|
if (hue1 >= 360.0)
|
||||||
|
hue1 = 0.0;
|
||||||
|
double hue2 = (whole_iterations + 1) * 360.0 / max_iterations;
|
||||||
|
if (hue2 >= 360.0)
|
||||||
|
hue2 = 0.0;
|
||||||
|
double hue = linear_interpolate(hue1, hue2, partial_iterations);
|
||||||
double saturation = 1.0;
|
double saturation = 1.0;
|
||||||
double value = iterations < max_iterations ? 1.0 : 0;
|
double value = iterations < max_iterations ? 1.0 : 0;
|
||||||
m_bitmap->set_pixel(px, py, Color::from_hsv(hue, saturation, value));
|
m_bitmap->set_pixel(px, py, Color::from_hsv(hue, saturation, value));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue