Quick function to create a visualization of the Mandelbrot set as an float HDR image.
| 23 | |
| 24 | // Quick function to create a visualization of the Mandelbrot set as an float HDR image. |
| 25 | static void create_mandelbrot(imagef& img) |
| 26 | { |
| 27 | const int width = 256; |
| 28 | const int height = 256; |
| 29 | const int max_iter = 1000; |
| 30 | |
| 31 | // Create a more interesting color palette |
| 32 | uint8_t palette[256][3]; |
| 33 | for (int i = 0; i < 256; i++) |
| 34 | { |
| 35 | if (i < 64) |
| 36 | { |
| 37 | // Blue to cyan transition |
| 38 | palette[i][0] = static_cast<uint8_t>(0); // Red component |
| 39 | palette[i][1] = static_cast<uint8_t>(i * 4); // Green component |
| 40 | palette[i][2] = static_cast<uint8_t>(255); // Blue component |
| 41 | } |
| 42 | else if (i < 128) |
| 43 | { |
| 44 | // Cyan to green transition |
| 45 | palette[i][0] = static_cast<uint8_t>(0); // Red component |
| 46 | palette[i][1] = static_cast<uint8_t>(255); // Green component |
| 47 | palette[i][2] = static_cast<uint8_t>(255 - (i - 64) * 4); // Blue component |
| 48 | } |
| 49 | else if (i < 192) |
| 50 | { |
| 51 | // Green to yellow transition |
| 52 | palette[i][0] = static_cast<uint8_t>((i - 128) * 4); // Red component |
| 53 | palette[i][1] = static_cast<uint8_t>(255); // Green component |
| 54 | palette[i][2] = static_cast<uint8_t>(0); // Blue component |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | // Yellow to red transition |
| 59 | palette[i][0] = static_cast<uint8_t>(255); // Red component |
| 60 | palette[i][1] = static_cast<uint8_t>(255 - (i - 192) * 4); // Green component |
| 61 | palette[i][2] = static_cast<uint8_t>(0); // Blue component |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Iterate over each pixel in the image |
| 66 | for (int px = 0; px < width; px++) |
| 67 | { |
| 68 | for (int py = 0; py < height; py++) |
| 69 | { |
| 70 | double x0 = (px - width / 2.0) * 4.0 / width; |
| 71 | double y0 = (py - height / 2.0) * 4.0 / height; |
| 72 | double zx = 0.0; |
| 73 | double zy = 0.0; |
| 74 | double zx_squared = 0.0; |
| 75 | double zy_squared = 0.0; |
| 76 | double x_temp; |
| 77 | |
| 78 | int iter; |
| 79 | for (iter = 0; iter < max_iter; iter++) |
| 80 | { |
| 81 | zx_squared = zx * zx; |
| 82 | zy_squared = zy * zy; |
no test coverage detected