-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mandelbrot_set.cpp
202 lines (178 loc) · 3.67 KB
/
Mandelbrot_set.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
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
#include "Mandelbrot_set.hpp"
using namespace ms;
Mandelbrot_set::Mandelbrot_set(sf::RenderWindow* window, int width, int height, sf::Vertex* vertices) {
this->width = width;
this->height = height;
this->window = window;
this->vertices = vertices;
}
void Mandelbrot_set::show()
{
//std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int* result = mendelbrot_kernel(height, width, maxIter, startX, startY, zoom);
for (int x = 0; x < (*window).getSize().x; ++x)
{
for (int y = 0; y < (*window).getSize().y; ++y)
{
int i = x + y * width;
#ifdef __STATIC_MAX_ITER
vertices[i].color.r = palette[result[i]].r;
vertices[i].color.g = palette[result[i]].g;
vertices[i].color.b = palette[result[i]].b;
#else
double iterationNorm = 360.0 * result[i] / maxIter;
if (result[i])
vertices[i].color = hsv2rgb_fast(iterationNorm);
else
vertices[i].color = sf::Color::Black;
#endif
}
}
cudaFreeHost(result);
(*window).draw(vertices, width * height, sf::Points);
if (isHUDshown) showHUD();
(*window).display();
//std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
//std::cout << "Time enlapsed: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[microseconds]" << std::endl;
}
void Mandelbrot_set::showHUD() {
sf::Font font;
font.loadFromFile("graphics/font/Roboto-Regular.ttf");
char str[128];
snprintf(str, 127, "OffsetX: %.2f OffsetY: %.2f\nZoom: %.4f\nMax iteration: %d", startX, startY, zoom, maxIter);
sf::RectangleShape rect(sf::Vector2f(450, 100));
sf::Text text;
text.setFont(font);
text.setString(str);
text.setCharacterSize(24);
text.setFillColor(sf::Color::Black);
text.setStyle(sf::Text::Bold);
(*window).draw(rect);
(*window).draw(text);
}
void Mandelbrot_set::setZoom(double modifier) {
zoom *= modifier;
show();
}
void Mandelbrot_set::setStartX(double modifier) {
setPosition(modifier, 0);
}
void Mandelbrot_set::setStartY(double modifier) {
setPosition(0, modifier);
}
void Mandelbrot_set::setPosition(double modifierX, double modifierY) {
startX += modifierX * zoom;
startY += modifierY * zoom;
show();
}
void Mandelbrot_set::setIteration(int modifier) {
if (modifier < 0 && maxIter > 1)
maxIter += modifier;
else if (modifier > 0 && maxIter < 199)
maxIter += modifier;
show();
}
void Mandelbrot_set::invertHUD() {
isHUDshown = !isHUDshown;
show();
}
void Mandelbrot_set::reset() {
startX = startY = 0.0;
zoom = 1.0;
maxIter = 100;
show();
}
sf::Color Mandelbrot_set::hsv2rgb(double h, double s, double v) {
double C, X, Y, Z, H;
short i;
sf::Color out;
h /= 60.0;
i = (short)(h);
H = h - i;
C = v * 255;
X = C * (1.0 - s);
Y = C * (1.0 - s * H);
Z = C * (1.0 - s * (1 - H));
switch (i)
{
case 0:
out.r = C;
out.g = Z;
out.b = X;
break;
case 1:
out.r = Y;
out.g = C;
out.b = Z;
break;
case 2:
out.r = X;
out.g = C;
out.b = Z;
break;
case 3:
out.r = X;
out.g = Y;
out.b = C;
break;
case 4:
out.r = Z;
out.g = X;
out.b = C;
break;
case 5:
default:
out.r = C;
out.g = X;
out.b = Y;
break;
}
return out;
}
sf::Color Mandelbrot_set::hsv2rgb_fast(double h) {
const double C = 255.0;
double X, Y, Z, H;
short i;
sf::Color out;
h /= 60.0;
i = (short)(h);
H = h - i;
X = 0.0;
Y = C * (1.0 - H);
Z = C * H;
switch (i)
{
case 0:
out.r = C;
out.g = Z;
out.b = X;
break;
case 1:
out.r = Y;
out.g = C;
out.b = Z;
break;
case 2:
out.r = X;
out.g = C;
out.b = Z;
break;
case 3:
out.r = X;
out.g = Y;
out.b = C;
break;
case 4:
out.r = Z;
out.g = X;
out.b = C;
break;
case 5:
default:
out.r = C;
out.g = X;
out.b = Y;
break;
}
return out;
}