icp/oer/courses/c-openmp/sections/04-problem-solving/00-mandelbrot/program.c

58 lines
1.3 KiB
C

#include <stdio.h>
#include <omp.h>
#include <math.h>
#include <stdlib.h>
#define X_PIXEL 1000
#define Y_PIXEL 1000
double scaled_x(int x) {
return -2.5 + (3.0 / X_PIXEL) * (double)x;
}
double scaled_y(int y) {
return -2.0 + (4.0 / Y_PIXEL) * (double)y;
}
int main(int argc, const char *argv[])
{
double start = omp_get_wtime();
char picture[X_PIXEL][Y_PIXEL];
for (int px = 0; px < X_PIXEL; px++)
for (int py = 0; py < Y_PIXEL; py++)
{
double cx = scaled_x(px);
double cy = scaled_y(py);
double x = 0.0;
double y = 0.0;
int i = 0;
int max_i = 1000;
while(i < max_i && x*x + y*y < (1 << 16))
{
double x_tmp = x*x - y*y + cx;
double y_tmp = 2*x*y + cy;
x = x_tmp;
y = y_tmp;
i++;
}
if (i == max_i) {
picture[px][py] = '*';
} else {
picture[px][py] = ' ';
}
}
double end = omp_get_wtime();
// We downscale the image
// by only printing one in twenty pixels
for (int x = 0; x < X_PIXEL; x+=20) {
printf("\n");
for (int y = 0; y < Y_PIXEL; y+=20)
printf("%c", picture[x][y]);
}
printf("\nComputation took %f seconds\n", end - start);
}