#include #include #include #include "images/images.h" #include "complex.c" void draw_mandelbrot(int width, int height, int iterations); void draw_mandelbrot_auto(int width, int iterations); int test_mandelbrot(Complex c, int iterations); int main(int argc, char* argv[]) { int width = 3000; int iterations = 60; if (argc > 1) { width = atoi(argv[1]); } if (argc > 2) { iterations = atoi(argv[2]); } draw_mandelbrot_auto(width, iterations); } void draw_mandelbrot_auto(int width, int iterations) { draw_mandelbrot(width, 2 * width / 3, iterations); } void draw_mandelbrot(int width, int height, int iterations) { int center_x = 2 * width / 3; int center_y = height / 2; float step = 3.0f / width; Image img; image_new(width, height, &img); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Complex c = complex_new( (x - center_x) * step, (y - center_y) * step ); int ret = test_mandelbrot(c, iterations); int r = (ret * 160 / iterations); int g = (ret * 9 / iterations); int b = (ret * 165 / iterations); image_set_px(img, x, y, r,g,b); } } image_save_bmp(img, "mandelbrot.bmp"); image_destroy(img); } int test_mandelbrot(Complex c, int iterations) { Complex z = complex_new(0,0); for (int i = 0; i < iterations; i++) { z = complex_add(complex_mult(z,z), c); // z^2 + c if (complex_abs(z) > 2) return i; } return 0; }