-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
205 lines (155 loc) · 4.2 KB
/
main.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STEPS 10000
typedef struct Dimensions{
float ymin, ymax, xmin, xmax;
int pixel_width, pixel_height;
}Dimensions;
typedef struct Pixel{
unsigned char r,g,b;
}Pixel;
typedef struct Picture{
int width, height;
Pixel **pixel_colors;
char* filename;
}Picture;
typedef struct Complex{
float x, y;
}Complex;
void read_config_and_populate(Dimensions*, char*);
void create_picture_of_mandelbrot(Picture*, Dimensions, char*);
void create_picture_of_julia(Picture*, Dimensions, char*);
Complex compute_z(Complex, Complex);
unsigned char is_in_mandlebrot(Complex);
unsigned char is_in_julia(Complex, Complex);
void write_picture(Picture);
Pixel* read_colors(char*);
int main(int argc, char** argv){
Dimensions d;
read_config_and_populate(&d, "config");
Picture p;
create_picture_of_mandelbrot(&p, d, "fractal.ppm");
write_picture(p);
create_picture_of_julia(&p, d, "julia.ppm");
write_picture(p);
return 0;
}
void read_config_and_populate(Dimensions* d, char* config_filename){
d->ymin = -1;
d->ymax = 1;
d->xmax = 2;
d->xmin = -2;
d->pixel_width = 1920;
d->pixel_height = 1080;
}
void write_picture(Picture p){
FILE *f = fopen(p.filename, "w");
fprintf(f, "P3\n%d %d\n256\n", p.width, p.height);
int i, j;
for(i = 0; i < p.height; i++){
for(j = 0; j < p.width; j++){
fprintf(f, "%d %d %d ", p.pixel_colors[i][j].r, p.pixel_colors[i][j].g, p.pixel_colors[i][j].b);
}
fprintf(f, "\n");
}
fclose(f);
}
void create_picture_of_mandelbrot(Picture* p, Dimensions d, char* filename){
float x_step = (float)(d.xmax - d.xmin)/(float)d.pixel_width;
float y_step = (float)(d.ymax - d.ymin)/(float)d.pixel_height;
Pixel* pixels = read_colors("pal.ppm");
int i, j;
p->width = d.pixel_width;
p->height = d.pixel_height;
p->filename = filename;
p->pixel_colors = (Pixel**)malloc(sizeof(Pixel*) * d.pixel_height);
for(i = 0; i < p->height; i++){
p->pixel_colors[i] = (Pixel*)malloc(sizeof(Pixel) * d.pixel_width);
}
for(i = 0; i < d.pixel_height; i++){
for(j = 0; j < d.pixel_width; j++){
Complex c;
c.x = j*x_step + d.xmin;
c.y = i*y_step + d.ymin;
unsigned char value = is_in_mandlebrot(c);
p->pixel_colors[i][j].r = pixels[value].r;
p->pixel_colors[i][j].g = pixels[value].g;
p->pixel_colors[i][j].b = pixels[value].b;
}
}
}
void create_picture_of_julia(Picture* p, Dimensions d, char* filename){
float x_step = (float)(d.xmax - d.xmin)/(float)d.pixel_width;
float y_step = (float)(d.ymax - d.ymin)/(float)d.pixel_height;
Complex c;
c.x = -0.153;
c.y = 0.652995;
Pixel* pixels = read_colors("pal.ppm");
int i, j;
p->width = d.pixel_width;
p->height = d.pixel_height;
p->filename = filename;
p->pixel_colors = (Pixel**)malloc(sizeof(Pixel*) * d.pixel_height);
for(i = 0; i < p->height; i++){
p->pixel_colors[i] = (Pixel*)malloc(sizeof(Pixel) * d.pixel_width);
}
for(i = 0; i < d.pixel_height; i++){
for(j = 0; j < d.pixel_width; j++){
Complex z;
z.x = j*x_step + d.xmin;
z.y = i*y_step + d.ymin;
unsigned char value = is_in_julia(z, c);
p->pixel_colors[i][j].r = pixels[value].r;
p->pixel_colors[i][j].g = pixels[value].g;
p->pixel_colors[i][j].b = pixels[value].b;
}
}
}
unsigned char is_in_mandlebrot(Complex c){
int step = 0;
Complex z = c;
while(step < MAX_STEPS && z.x * z.x + z.y * z.y < 4){
step++;
z = compute_z(z, c);
}
if(step < MAX_STEPS){
return step%256;
}
return 0;
}
unsigned char is_in_julia(Complex z, Complex c){
int step = 0;
while(step < MAX_STEPS && z.x * z.x + z.y * z.y < 4){
step++;
z = compute_z(z, c);
}
if(step < MAX_STEPS){
return step%256;
}
return 0;
}
Complex compute_z(Complex z, Complex c){
Complex new_z;
new_z.x = z.x * z.x - z.y * z.y + c.x;
new_z.y = c.y + 2 * z.x * z.y;
return new_z;
}
Pixel* read_colors(char* filename){
int width = 256;
Pixel* pixels = (Pixel*)malloc(sizeof(Pixel) * width);
FILE* f = fopen(filename, "rb");
int endline_count = 0, i;
unsigned char buffer;
do{
fread(&buffer, sizeof(unsigned char), 1, f);
if(buffer == 10){
endline_count++;
}
}while(endline_count < 3);
for(i = 0; i < width; i++){
fread(pixels, sizeof(unsigned char), width*3, f);
}
fclose(f);
return pixels;
}