-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuliaSetSTL.cpp
88 lines (71 loc) · 2.1 KB
/
JuliaSetSTL.cpp
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
#include <algorithm>
#include <complex>
#include <execution>
#include <iostream>
#include <vector>
#include "lodepng/lodepng.h"
using real_t = double;
using complex_t = std::complex<real_t>;
constexpr int numX = 2048;
constexpr int numY = 2048;
constexpr real_t height = 4.0;
constexpr real_t width = 4.0;
constexpr complex_t origin(-2.0, -2.0);
struct julia
{
int operator()(complex_t v)
{
constexpr complex_t c{-0.8, 0.2};
int i;
for (i = 0; i < 200; ++i)
{
v = v*v + c;
if (std::abs(v) > real_t(100.0))
{
break;
}
}
return i;
}
};
struct colorPicture
{
int operator()(int v)
{
constexpr int min = 0;
constexpr int max = 200;
const real_t frac = static_cast<real_t>(v - min) / static_cast<real_t>(max-min);
return static_cast<int> (static_cast<real_t>(255) * (1 - (1-frac) * (1-frac) * (1-frac) * (1-frac) * (1-frac) * (1-frac)) );
}
};
int main()
{
std::vector<complex_t> field(numX*numY);
std::vector<int> count(numX*numY);
for (int y = 0 ; y < numY; ++y)
for (int x = 0 ; x < numX; ++x)
{
const int idx = y * numX + x;
const complex_t v(x/static_cast<real_t>(numX) * width,
y/static_cast<real_t>(numY) * height);
field[idx] = origin + v;
}
std::transform(std::execution::par_unseq,
field.begin(),
field.end(),
count.begin(),
julia{});
std::cout << "min: " << *std::min_element(count.begin(), count.end()) << "\tmax: " << *std::max_element(count.begin(), count.end()) << std::endl;
std::transform(std::execution::par_unseq,
count.begin(),
count.end(),
count.begin(),
colorPicture{});
std::cout << "min: " << *std::min_element(count.begin(), count.end()) << "\tmax: " << *std::max_element(count.begin(), count.end()) << std::endl;
std::vector<unsigned char> RGBpic(numX*numY*3, 0);
for (int i = 0; i<count.size(); ++i)
{
RGBpic[i*3] = count[i];
}
lodepng::encode("julia.png", RGBpic, numX, numY, LCT_RGB);
}