From db6f228a2c39adac8185469977e27187c21c276d Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 10 Nov 2020 18:24:00 +0100 Subject: [PATCH] add option to choose max_iter for mandelbrot --- src/main.c | 2 +- src/options.c | 10 ++++++++-- src/options.h | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 10661f4..7011f0a 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) { if (!image) err(EXIT_FAILURE, "could not allocate image"); - mandelbrot(image, 100); + mandelbrot(image, opt.max_iter); print_ppm(image, opt.output); fclose(opt.output); diff --git a/src/options.c b/src/options.c index 44f9d1d..4967cc2 100644 --- a/src/options.c +++ b/src/options.c @@ -9,17 +9,22 @@ struct options parse_options(int *argc, char **argv[]) { .output = stdout, .w = 960, .h = 540, + .max_iter = 100, }; opterr = 0; // Deactivate error message from `getopt` int opt; - while ((opt = getopt(*argc, *argv, "h:o:w:")) != -1) { + while ((opt = getopt(*argc, *argv, "h:m:o:w:")) != -1) { switch (opt) { case 'h': if (!sscanf(optarg, "%zu", &opts.h)) errx(EXIT_FAILURE, "could not parse height"); break; + case 'm': + if (!sscanf(optarg, "%zu", &opts.max_iter)) + errx(EXIT_FAILURE, "could not parse max_iter"); + break; case 'o': if (!freopen(optarg, "w", opts.output)) err(EXIT_FAILURE, "could not open output file"); @@ -29,7 +34,8 @@ struct options parse_options(int *argc, char **argv[]) { errx(EXIT_FAILURE, "could not parse width"); break; default: - fprintf(stderr, "Usage: %s [-o FILE] [-h HEIGHT] [-w WIDTH]\n", + fprintf(stderr, + "Usage: %s [-o FILE] [-m MAX_ITER] [-h HEIGHT] [-w WIDTH]\n", (*argv)[0]); exit(EXIT_FAILURE); } diff --git a/src/options.h b/src/options.h index 2cf0b68..0f2c8f0 100644 --- a/src/options.h +++ b/src/options.h @@ -8,6 +8,7 @@ struct options { FILE *output; size_t w; size_t h; + size_t max_iter; }; struct options parse_options(int *argc, char **argv[]);