-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplication.cpp
275 lines (239 loc) · 9.22 KB
/
Application.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <SFML/Graphics.hpp>
#include <complex>
#include <fstream>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include "MandelbrotSet.cpp"
class Application {
public:
Application(int width, int height, int maxIter, int numThreads)
: m_fractal(MandelbrotSet(width, height, maxIter, numThreads)),
m_window(sf::VideoMode(width, height), "Mandelbrot Set",
sf::Style::Titlebar | sf::Style::Close) {
// Initialize the SFML image and texture
m_image.create(m_fractal.WIDTH, m_fractal.HEIGHT);
drawFractal();
m_texture.loadFromImage(m_image);
m_sprite.setTexture(m_texture);
// Configure the selection rectangle (to zoom in)
m_selectionRect.setFillColor(sf::Color(0, 0, 0, 0));
m_selectionRect.setOutlineColor(sf::Color::Red);
m_selectionRect.setOutlineThickness(3);
// Load font and create buttons
if (!m_font.loadFromFile("TimesNewRoman.ttf")) {
std::cout << "Please, provide a Font!\n";
}
createButton(width - 150, 5, m_changeColorButton, m_changeColorButtonText,
"Change color");
createButton(width - 150, 50, m_resetZoomingButton,
m_resetZoomingButtonText, "Reset Zooming");
}
void run() {
while (m_window.isOpen()) {
sf::Event event;
while (m_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
m_window.close();
return;
}
handleEvent(event);
break;
}
// Draw elements on the window
m_window.draw(m_sprite);
if (m_dragging) {
m_window.draw(m_selectionRect);
}
m_window.draw(m_changeColorButton);
m_window.draw(m_changeColorButtonText);
m_window.draw(m_resetZoomingButton);
m_window.draw(m_resetZoomingButtonText);
m_window.display();
}
}
private:
void drawFractal() {
m_fractal.renderFractal();
for (int y = 0; y < m_fractal.HEIGHT; ++y) {
for (int x = 0; x < m_fractal.WIDTH; ++x) {
int iter = m_fractal.image[y * m_fractal.WIDTH + x];
sf::Color color = getColor(iter);
m_image.setPixel(x, y, color);
}
}
}
sf::Color getColor(int iter) {
if (iter < m_fractal.MAX_ITER) {
switch (m_COLOR) {
case 1:
return sf::Color(0, 255 * iter / m_fractal.MAX_ITER,
255 * iter / m_fractal.MAX_ITER);
case 2:
return sf::Color(255 * iter / m_fractal.MAX_ITER, 0,
255 * iter / m_fractal.MAX_ITER);
case 3:
return sf::Color(255 * iter / m_fractal.MAX_ITER,
255 * iter / m_fractal.MAX_ITER, 0);
}
}
return sf::Color::Black;
}
void handleEvent(sf::Event& event) {
if (event.type == sf::Event::MouseButtonPressed &&
event.mouseButton.button == sf::Mouse::Left) {
if (m_changeColorButton.getGlobalBounds().contains(event.mouseButton.x,
event.mouseButton.y)) {
changeColorButtonHandler();
} else if (m_resetZoomingButton.getGlobalBounds().contains(
event.mouseButton.x, event.mouseButton.y)) {
resetZoomingButtonHandler();
} else {
// Start rectangle selection
m_dragging = true;
m_start = sf::Mouse::getPosition(m_window);
}
return;
}
// Mouse left-click released after selecting a rectangle (zoom in is needed)
if (event.type == sf::Event::MouseButtonReleased &&
event.mouseButton.button == sf::Mouse::Left && m_dragging) {
handleZoomingIn();
return;
}
if (m_dragging) {
handleDragging();
return;
}
if (event.type == sf::Event::MouseWheelScrolled &&
event.mouseWheelScroll.delta != 0) {
ZoomByScrolling(event.mouseWheelScroll.delta, event.mouseWheelScroll.x,
event.mouseWheelScroll.y);
}
}
void changeColorButtonHandler() {
m_COLOR = (m_COLOR % 3) + 1;
drawFractal();
m_texture.loadFromImage(m_image);
m_sprite.setTexture(m_texture);
}
void resetZoomingButtonHandler() {
m_fractal.setDefaultRegion();
drawFractal();
m_texture.loadFromImage(m_image);
m_sprite.setTexture(m_texture);
}
void handleZoomingIn() {
m_dragging = false;
m_end = sf::Mouse::getPosition(m_window);
if (std::abs(m_start.x - m_end.x) < EPS &&
std::abs(m_start.y - m_end.y) < EPS) {
return;
}
// Ensure aspect ratio is preserved (4:3)
double widthRatio = 4.0 / 3.0;
double newWidth = m_end.x - m_start.x;
double newHeight = newWidth / widthRatio;
if (newHeight < 0)
newHeight = -newHeight;
// Adjust the end point to maintain aspect ratio
if (m_end.y < m_start.y) {
m_end.y = m_start.y - newHeight;
} else {
m_end.y = m_start.y + newHeight;
}
// Specify a new area of the complex plane
double realMin =
m_fractal.REAL_MIN + (m_fractal.REAL_MAX - m_fractal.REAL_MIN) *
std::min(m_start.x, m_end.x) / m_fractal.WIDTH;
double realMax =
m_fractal.REAL_MIN + (m_fractal.REAL_MAX - m_fractal.REAL_MIN) *
std::max(m_start.x, m_end.x) / m_fractal.WIDTH;
double imagMin = m_fractal.IMAG_MIN +
(m_fractal.IMAG_MAX - m_fractal.IMAG_MIN) *
std::min(m_start.y, m_end.y) / m_fractal.HEIGHT;
double imagMax = m_fractal.IMAG_MIN +
(m_fractal.IMAG_MAX - m_fractal.IMAG_MIN) *
std::max(m_start.y, m_end.y) / m_fractal.HEIGHT;
// Clear red rectangle
m_selectionRect.setSize(sf::Vector2f(0, 0));
m_fractal.updateRegion(realMin, realMax, imagMin, imagMax);
drawFractal();
m_texture.loadFromImage(m_image);
m_sprite.setTexture(m_texture);
}
void handleDragging() {
// Update selection rectangle during dragging
m_end = sf::Mouse::getPosition(m_window);
double newWidth = std::abs(m_end.x - m_start.x);
double newHeight =
newWidth * 3.0 / 4.0; // Adjust the aspect ratio (must be 4:3)
if (m_end.x > m_start.x && m_end.y < m_start.y) { // up-right
m_end.y = m_start.y - newHeight;
} else if (m_end.x < m_start.x && m_end.y < m_start.y) { // up-left
m_end.y = m_start.y - newHeight;
m_end.x = m_start.x - newWidth;
} else if (m_end.x < m_start.x && m_end.y > m_start.y) { // down-left
m_end.x = m_start.x - newWidth;
} else { // down-right
m_end.y = m_start.y + newHeight;
}
m_selectionRect.setPosition(sf::Vector2f(std::min(m_start.x, m_end.x),
std::min(m_start.y, m_end.y)));
m_selectionRect.setSize(sf::Vector2f(newWidth, newHeight));
}
void ZoomByScrolling(float delta, int mouseX, int mouseY) {
// Calculate zoom factor
double zoomFactor = (delta > 0) ? 0.9 : 1.1;
// Calculate the new complex plane coordinates based on mouse position
double mouseRe =
m_fractal.REAL_MIN +
(m_fractal.REAL_MAX - m_fractal.REAL_MIN) * mouseX / m_fractal.WIDTH;
double mouseIm =
m_fractal.IMAG_MIN +
(m_fractal.IMAG_MAX - m_fractal.IMAG_MIN) * mouseY / m_fractal.HEIGHT;
// Zoom in or out by adjusting the region
double newWidth = (m_fractal.REAL_MAX - m_fractal.REAL_MIN) * zoomFactor;
double newHeight = (m_fractal.IMAG_MAX - m_fractal.IMAG_MIN) * zoomFactor;
// Center the new region around the mouse position
double newRealMin = mouseRe - (mouseRe - m_fractal.REAL_MIN) * zoomFactor;
double newRealMax = newRealMin + newWidth;
double newImagMin = mouseIm - (mouseIm - m_fractal.IMAG_MIN) * zoomFactor;
double newImagMax = newImagMin + newHeight;
m_fractal.updateRegion(newRealMin, newRealMax, newImagMin, newImagMax);
drawFractal();
m_texture.loadFromImage(m_image);
m_sprite.setTexture(m_texture);
}
void createButton(int x, int y, sf::RectangleShape& button, sf::Text& text,
const std::string& title) {
button = sf::RectangleShape(sf::Vector2f(125, 30));
button.setFillColor(sf::Color::Black);
button.setPosition(x, y);
button.setOutlineThickness(2);
button.setOutlineColor(sf::Color::White);
text.setFont(m_font);
text.setString(title);
text.setCharacterSize(15);
text.setFillColor(sf::Color::White);
text.setPosition(button.getPosition().x + 10, button.getPosition().y + 5);
}
MandelbrotSet m_fractal;
sf::RenderWindow m_window;
sf::Image m_image;
sf::Texture m_texture;
sf::Sprite m_sprite;
sf::Vector2i m_start;
sf::Vector2i m_end;
sf::RectangleShape m_selectionRect;
sf::RectangleShape m_changeColorButton;
sf::Text m_changeColorButtonText;
sf::RectangleShape m_resetZoomingButton;
sf::Text m_resetZoomingButtonText;
sf::Font m_font;
constexpr static const double EPS = 1e-10;
int m_COLOR = 1; // Color palette identifier (1, 2, or 3)
bool m_dragging =
false; // Flag to indicate if the user is dragging for selection
};