add option to choose max_iter for mandelbrot

This commit is contained in:
Bruno BELANYI 2020-11-10 18:24:00 +01:00
parent b6882bf52c
commit db6f228a2c
3 changed files with 10 additions and 3 deletions

View file

@ -14,7 +14,7 @@ int main(int argc, char *argv[]) {
if (!image) if (!image)
err(EXIT_FAILURE, "could not allocate image"); err(EXIT_FAILURE, "could not allocate image");
mandelbrot(image, 100); mandelbrot(image, opt.max_iter);
print_ppm(image, opt.output); print_ppm(image, opt.output);
fclose(opt.output); fclose(opt.output);

View file

@ -9,17 +9,22 @@ struct options parse_options(int *argc, char **argv[]) {
.output = stdout, .output = stdout,
.w = 960, .w = 960,
.h = 540, .h = 540,
.max_iter = 100,
}; };
opterr = 0; // Deactivate error message from `getopt` opterr = 0; // Deactivate error message from `getopt`
int opt; int opt;
while ((opt = getopt(*argc, *argv, "h:o:w:")) != -1) { while ((opt = getopt(*argc, *argv, "h:m:o:w:")) != -1) {
switch (opt) { switch (opt) {
case 'h': case 'h':
if (!sscanf(optarg, "%zu", &opts.h)) if (!sscanf(optarg, "%zu", &opts.h))
errx(EXIT_FAILURE, "could not parse height"); errx(EXIT_FAILURE, "could not parse height");
break; break;
case 'm':
if (!sscanf(optarg, "%zu", &opts.max_iter))
errx(EXIT_FAILURE, "could not parse max_iter");
break;
case 'o': case 'o':
if (!freopen(optarg, "w", opts.output)) if (!freopen(optarg, "w", opts.output))
err(EXIT_FAILURE, "could not open output file"); 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"); errx(EXIT_FAILURE, "could not parse width");
break; break;
default: 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]); (*argv)[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View file

@ -8,6 +8,7 @@ struct options {
FILE *output; FILE *output;
size_t w; size_t w;
size_t h; size_t h;
size_t max_iter;
}; };
struct options parse_options(int *argc, char **argv[]); struct options parse_options(int *argc, char **argv[]);