Skip to content

Commit eca13ff

Browse files
committed
Add implementation that does not use std::complex
1 parent 1c0577d commit eca13ff

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/no_complex.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "mandelbrot.hpp"
2+
3+
#ifdef NO_COMPLEX
4+
int64_t mandelbrot_computation(ofstream &matrix_out)
5+
{
6+
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
7+
8+
const auto start = chrono::steady_clock::now();
9+
#pragma omp parallel for schedule(dynamic, 1)
10+
for (int pos = 0; pos < HEIGHT * WIDTH; ++pos)
11+
{
12+
13+
const int row = pos / WIDTH;
14+
const int col = pos % WIDTH;
15+
const double cr = col * STEP + MIN_X;
16+
const double ci = row * STEP + MIN_Y;
17+
18+
19+
// z = z^2 + c
20+
double zr = 0;
21+
double zi = 0;
22+
23+
for (uint16_t k = 1; k <= ITERATIONS; ++k) {
24+
double zr2 = zr*zr;
25+
double zi2 = zi*zi;
26+
double zrzi = zr * zi;
27+
28+
zr = (zr2 - zi2) + cr;
29+
zi = (zrzi + zrzi) + ci;
30+
zr2 = zr*zr;
31+
zi2 = zi*zi;
32+
double mag2 = zr2 + zi2;
33+
if(mag2 >= 4.0) {
34+
image[pos] = k;
35+
break;
36+
}
37+
}
38+
}
39+
const auto end = chrono::steady_clock::now();
40+
auto difference = chrono::duration_cast<chrono::milliseconds>(end - start).count();
41+
cerr << "Time elapsed: "
42+
<< difference
43+
<< " milliseconds." << endl;
44+
for (int row = 0; row < HEIGHT; row++)
45+
{
46+
for (int col = 0; col < WIDTH; col++)
47+
{
48+
matrix_out << image[row * WIDTH + col];
49+
50+
if (col < WIDTH - 1)
51+
matrix_out << ',';
52+
}
53+
if (row < HEIGHT - 1)
54+
matrix_out << endl;
55+
}
56+
matrix_out.close();
57+
58+
delete[] image; // It's here for coding style, but useless
59+
return difference;
60+
}
61+
#endif

0 commit comments

Comments
 (0)