aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/fractals.c
blob: 1c0c4ea8fe43cc800d38b040098c303775f213cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <getopt.h>

#include "grids.h"
#include "precision.h"
#include "fractals.h"


void print_usage(FILE* file, char* program_name){
    fprintf(file, "Usage: %s [-v] [-i iterations] [-x x_res] [-y y_res] [-z magnification] [-l lower_left] [-u upper_right]\n", program_name);
}

void print_help(){
    //TODO: add help
}

void print_info(){
    #ifdef EXTENDED_PRECISION
    printf("Compiled with long double float precision\n");
    #endif
    #ifndef EXTENDED_PRECISION
    printf("Compiled with double float precision\n");
    #endif
}

int main(const int argc, char *argv[]) {
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    //default values
    size_t iterations = 100;
    size_t x_res = w.ws_col;
    size_t y_res = w.ws_row;
    CBASE re_lower_left = -2;
    CBASE im_lower_left = -2;
    CBASE re_upper_right = 2;
    CBASE im_upper_right = 2;
    CBASE magnification = 1;
    bool verbose = false;
    //TODO: allocate adequate size buffer
    bool output_to_file = true;
    char* output_filename = "fractal.grid";

    // TODO: have output format option
    // TODO: have output file


    static struct option long_options[] = {
        {"iterations", required_argument, NULL, 'i'},
        {"x-res", required_argument, NULL, 'x'},
        {"y-res", required_argument, NULL, 'y'},
        {"lower-left", required_argument, NULL, 'l'},
        {"upper-right", required_argument, NULL, 'u'},
        {"magnification", required_argument, NULL, 'z'},
        {"output", required_argument, NULL, 'o'},
        {"verbose", no_argument, NULL, 'v'},
        {"help", no_argument, NULL, 'h'},
        {0, 0, 0, 0} // Termination element
    };

    //parse command line arguments
    int opt;
    while((opt = getopt_long(argc, argv, "i:x:y:l:u:z:o:vh", long_options, NULL)) != -1){
        switch(opt){
            case 'i':
                iterations = strtoull(optarg, NULL, 10);
                break;
            case 'x':
                x_res = strtoull(optarg, NULL, 10);
                break;
            case 'y':
                y_res = strtoull(optarg, NULL, 10);
                break;
            case 'l':
                sscanf(optarg, CFORMAT"+"CFORMAT"i", &re_lower_left, &im_lower_left);
                break;
            case 'u':
                sscanf(optarg, CFORMAT"+"CFORMAT"i", &re_upper_right, &im_upper_right);
                break;
            case 'o':
                //TODO: check if can write to location
                output_to_file = true;
                //TODO: 
                break;
            case 'z':
                sscanf(optarg, CFORMAT, &magnification);
                if(magnification <= 0){
                    fprintf(stderr, "Invalid magnification "CFORMAT", exitting\n", magnification);
                    return 1;
                }
                break;
            case 'v':
                verbose = true;
                break;
            case 'h':
                print_usage(stdout, argv[0]);
                return 0;
            default:
                print_usage(stderr, argv[0]);
                return 1;
        }
    }

    //const CBASE complex lower_left = re_lower_left + im_lower_left * I;
    const complex_t lower_left = { .re=re_lower_left, .im=im_lower_left};
    //const CBASE complex upper_right = re_upper_right + im_upper_right * I;
    const complex_t upper_right = { .re=re_upper_right, .im=im_upper_right};

    grid_t* grid = create_grid(x_res, y_res, iterations, lower_left, upper_right);
    if(!grid) return 1;


    if(magnification != 1){
        if(verbose) printf("Magnification: "CFORMAT"\n", magnification);
        zoom_grid(grid, magnification);
    }

    // params for different fractals
    const double degree = 3.0;
    // const CBASE complex constant = 0.285L + 0.01L*I;
    const complex_t constant = {
        .re = 0.285,
        .im = 0.01
    };
    // const CBASE complex constant = -0.835L -0.321L* I;
    const double radius = 100;

    // enum fractal f = BURNING_SHIP;
    enum fractal f = JULIA;
    switch(f){
        case MANDELBROT:
            mandelbrot_grid(grid);
            break;
        case TRICORN:
            tricorn_grid(grid);
            break;
        case MULTIBROT:
            multibrot_grid(grid, degree);
            break;
        case MULTICORN:
            multicorn_grid(grid, degree);
            break;
        case BURNING_SHIP:
            burning_ship_grid(grid);
            break;
        case JULIA:
            julia_grid(grid, constant, radius);
            break;
        default:
            fprintf(stderr, "Unrecognized fractal\n");
            return 1;
    }

    if(verbose)print_grid_info(grid);

    //write grid to file
    if(output_to_file){
        FILE* write_file = fopen("test.grid", "wb");
        int err = write_grid(write_file , grid);
        if(err == GRID_WRITE_ERROR){
            fprintf(stderr, "Error writing occured while writting to file %s\n", output_filename);
        }
        fclose(write_file);
    }

    free_grid(grid);
    // //attempt to read grid from file
    // FILE* read_file = fopen("test2.grid", "rb");
    // grid_t* grid2 = read_grid(read_file);
    // fclose(read_file);
    //
    // printf("Grids are %s equal\n", grid_equal(grid, grid2) ? "exactly" :"not exactly");

    return 0;
}