-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelplot.cpp
220 lines (180 loc) · 5.92 KB
/
mandelplot.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
#include <SFML/Graphics.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Mouse.hpp>
#include <vector>
#include <array>
#include <complex>
#include <iostream>
using namespace std;
#define DEFAULT_MIN_DISPLAYED_VALUE_X -2.0
#define DEFAULT_MAX_DISPLAYED_VALUE_X .47
#define X_RANGE DEFAULT_MAX_DISPLAYED_VALUE_X - DEFAULT_MIN_DISPLAYED_VALUE_X
#define DEFAULT_MIN_DISPLAYED_VALUE_Y -1.12
#define DEFAULT_MAX_DISPLAYED_VALUE_Y 1.12
#define Y_RANGE DEFAULT_MAX_DISPLAYED_VALUE_Y - DEFAULT_MIN_DISPLAYED_VALUE_Y
#define XY_RATIO X_RANGE / Y_RANGE
#define BASE_WINDOW_SIZE 600
#define DEFAULT_WINDOW_WIDTH BASE_WINDOW_SIZE
#define DEFAULT_WINDOW_HEIGHT BASE_WINDOW_SIZE
bool isMouseInsideWindow(const sf::Window* window);
void pointZoom(const int, sf::Vector2f*, sf::Vector2f*, const sf::Window*);
void drag(
const sf::Vector2i,
const sf::Vector2f,
const sf::Vector2f,
sf::Vector2f*,
sf::Vector2f*,
const sf::Window*
);
int main()
{
// initialize window
sf::RenderWindow window(
sf::VideoMode(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT),
"Mandelbrot",
sf::Style::None
);
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
sf::Event event;
sf::Texture texture;
sf::Shader shader;
sf::Sprite sprite;
sf::Vector2f minDisplayedValues(
DEFAULT_MIN_DISPLAYED_VALUE_X,
DEFAULT_MIN_DISPLAYED_VALUE_Y
);
sf::Vector2f maxDisplayedValues(
DEFAULT_MAX_DISPLAYED_VALUE_X,
DEFAULT_MAX_DISPLAYED_VALUE_Y
);
// check if shaders are supported
if (!sf::Shader::isAvailable()) {
cerr << "ERROR: Shaders are not available in your machine." << endl;
return 1;
}
// try initializing shader
shader.loadFromFile("shader.frag", sf::Shader::Fragment);
// share variables with shader
shader.setUniform("resolution", sf::Vector2f(window.getSize()));
// initialize sprite that covers entire screen
// our shader will shade this empty sprite
texture.create(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
sprite.setTexture(texture);
// click and drag
bool isDragging;
sf::Vector2i draggingFrom;
sf::Vector2f startingMinDisplayedValues;
sf::Vector2f startingMaxDisplayedValues;
while (window.isOpen()) {
while (window.pollEvent(event)) {
// allow quitting with q or ESC
if (event.type == sf::Event::Closed
|| sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
window.close();
continue;
}
if (event.type == sf::Event::MouseWheelScrolled) {
pointZoom(
event.mouseWheelScroll.delta,
&minDisplayedValues,
&maxDisplayedValues,
&window
);
continue;
}
if (event.type == sf::Event::MouseButtonPressed) {
isDragging = true;
draggingFrom = sf::Mouse::getPosition(window);
startingMinDisplayedValues = sf::Vector2f(minDisplayedValues);
startingMaxDisplayedValues = sf::Vector2f(maxDisplayedValues);
continue;
}
if (event.type == sf::Event::MouseButtonReleased) {
isDragging = false;
continue;
}
if (isDragging && event.type == sf::Event::MouseMoved) {
drag(
draggingFrom,
startingMinDisplayedValues,
startingMaxDisplayedValues,
&minDisplayedValues,
&maxDisplayedValues,
&window
);
}
}
shader.setUniform("min_displayed", minDisplayedValues);
shader.setUniform("max_displayed", maxDisplayedValues);
window.clear();
window.draw(sprite, &shader);
window.display();
}
return 0;
}
/**
* Updates minDisplayedValues and maxDisplayedValues
*/
void pointZoom(
// can be -1 or 1
const int scrollDelta,
sf::Vector2f* minDisplayedValues,
sf::Vector2f* maxDisplayedValues,
const sf::Window* window
)
{
if (!isMouseInsideWindow(window)) return;
sf::Vector2i mousePosition = sf::Mouse::getPosition(*window);
sf::Vector2u windowSize = window->getSize();
double mousePercentLeft = double(mousePosition.x) / double(windowSize.x);
double mousePercentBottom = 1.0 - double(mousePosition.y) / double(windowSize.y);
double scaleFactor = 0.1;
// add or subtract scaleFactor, depending on scroll direction
double rangeScale = 1 + scaleFactor * scrollDelta;
double oldXRange = maxDisplayedValues->x - minDisplayedValues->x;
double oldYRange = maxDisplayedValues->y - minDisplayedValues->y;
double newXRange = oldXRange * rangeScale;
double newYRange = oldYRange * rangeScale;
double newMinX =
minDisplayedValues->x + (oldXRange - newXRange) * mousePercentLeft;
double newMinY =
minDisplayedValues->y + (oldYRange - newYRange) * (mousePercentBottom / 1);
minDisplayedValues->x = newMinX;
minDisplayedValues->y = newMinY;
maxDisplayedValues->x = newMinX + newXRange;
maxDisplayedValues->y = newMinY + newYRange;
}
bool isMouseInsideWindow(const sf::Window* window)
{
sf::Vector2i position = sf::Mouse::getPosition(*window);
sf::Vector2u windowSize = window->getSize();
if (position.x < 0 || position.y < 0) return false;
if (position.x > windowSize.x || position.y > windowSize.y) return false;
return true;
}
void drag(
// can be -1 or 1
const sf::Vector2i startingPosition,
const sf::Vector2f startingMinDisplayedValues,
const sf::Vector2f startingMaxDisplayedValues,
sf::Vector2f* minDisplayedValues,
sf::Vector2f* maxDisplayedValues,
const sf::Window* window
) {
if (!isMouseInsideWindow(window)) return;
sf::Vector2i currentPosition = sf::Mouse::getPosition(*window);
sf::Vector2f range = *maxDisplayedValues - *minDisplayedValues;
sf::Vector2f scale(
range.x / window->getSize().x,
-1 * range.y / window->getSize().y
);
sf::Vector2i diff(currentPosition - startingPosition);
sf::Vector2f scaledDiff(
scale.x * diff.x,
scale.y * diff.y
);
*minDisplayedValues = startingMinDisplayedValues - scaledDiff;
*maxDisplayedValues = startingMaxDisplayedValues - scaledDiff;
}