-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractal-serial.cpp
More file actions
70 lines (53 loc) · 1.58 KB
/
fractal-serial.cpp
File metadata and controls
70 lines (53 loc) · 1.58 KB
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
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include "writeBMP.hpp"
#define xMin 0.74395
#define xMax 0.74973
#define yMin 0.11321
#define yMax 0.11899
int main(int argc, char *argv[])
{
double dx, dy;
int width;
const int maxdepth = 256;
struct timeval start, end;
/* check command line */
if(argc != 2) {fprintf(stderr, "usage: exe <width> \n"); exit(-1);}
width = atoi(argv[1]);
if (width < 10) {fprintf(stderr, "edge_length must be at least 10\n"); exit(-1);}
dx = (xMax - xMin) / width;
dy = (yMax - yMin) / width;
printf("computing %d by %d fractal with a maximum depth of %d\n", width, width, maxdepth);
unsigned char *cnt = (unsigned char*)malloc(width * width * sizeof(unsigned char));
/* start time */
gettimeofday(&start, NULL);
for(int row = 0; row < width; ++row) {
for(int col = 0; col < width; ++col) {
double x2, y2, x, y, cx, cy;
int depth;
cy = yMin + row * dy; //compute row #
cx = xMin + col * dx; //compute column #
x = -cx;
y = -cy;
depth = maxdepth;
do {
x2 = x * x;
y2 = y * y;
y = 2 * x * y - cy;
x = x2 - y2 - cx;
depth--;
} while ((depth > 0) && ((x2 + y2) <= 5.0));
cnt[row * width + col] = depth & 255;
}
}
/* end time */
gettimeofday(&end, NULL);
printf("compute time: %.8f s\n", end.tv_sec + end.tv_usec / 1000000.0 - start.tv_sec - start.tv_usec / 1000000.0);
/* verify result by writing it to a file */
if (width <= 2048) {
WriteBMP(width, width, cnt, "fractal.bmp");
}
free(cnt);
return 0;
}