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)
err(EXIT_FAILURE, "could not allocate image");
mandelbrot(image, 100);
mandelbrot(image, opt.max_iter);
print_ppm(image, opt.output);
fclose(opt.output);

View file

@ -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);
}

View file

@ -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[]);