Skip to content

Commit ad7bfe0

Browse files
nicogunnarbeutner
authored andcommitted
Mandelbrot: Extract zoom() method
1 parent fbdf17a commit ad7bfe0

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

Userland/Demos/Mandelbrot/Mandelbrot.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ class Mandelbrot : public GUI::Frame {
209209

210210
void export_image(String const& export_path);
211211

212+
enum class Zoom {
213+
In,
214+
Out,
215+
};
216+
void zoom(Zoom in_out, const Gfx::IntPoint& center);
217+
212218
private:
213219
virtual void paint_event(GUI::PaintEvent&) override;
214220
virtual void mousedown_event(GUI::MouseEvent& event) override;
@@ -227,6 +233,33 @@ class Mandelbrot : public GUI::Frame {
227233
MandelbrotSet m_set;
228234
};
229235

236+
void Mandelbrot::zoom(Zoom in_out, const Gfx::IntPoint& center)
237+
{
238+
static constexpr double zoom_in_multiplier = 0.8;
239+
static constexpr double zoom_out_multiplier = 1.25;
240+
241+
bool zooming_in = in_out == Zoom::In;
242+
double multiplier = zooming_in ? zoom_in_multiplier : zoom_out_multiplier;
243+
244+
auto relative_rect = this->relative_rect();
245+
Gfx::IntRect zoomed_rect = relative_rect;
246+
247+
zoomed_rect.set_width(zoomed_rect.width() * multiplier);
248+
zoomed_rect.set_height(zoomed_rect.height() * multiplier);
249+
250+
auto leftover_width = abs(relative_rect.width() - zoomed_rect.width());
251+
auto leftover_height = abs(relative_rect.height() - zoomed_rect.height());
252+
253+
double cursor_x_percentage = static_cast<double>(center.x()) / relative_rect.width();
254+
double cursor_y_percentage = static_cast<double>(center.y()) / relative_rect.height();
255+
256+
zoomed_rect.set_x((zooming_in ? 1 : -1) * leftover_width * cursor_x_percentage);
257+
zoomed_rect.set_y((zooming_in ? 1 : -1) * leftover_height * cursor_y_percentage);
258+
259+
m_set.zoom(zoomed_rect);
260+
update();
261+
}
262+
230263
void Mandelbrot::paint_event(GUI::PaintEvent& event)
231264
{
232265
Frame::paint_event(event);
@@ -306,29 +339,7 @@ void Mandelbrot::mouseup_event(GUI::MouseEvent& event)
306339

307340
void Mandelbrot::mousewheel_event(GUI::MouseEvent& event)
308341
{
309-
static constexpr double zoom_in_multiplier = 0.8;
310-
static constexpr double zoom_out_multiplier = 1.25;
311-
312-
bool zooming_in = event.wheel_delta() < 0;
313-
double multiplier = zooming_in ? zoom_in_multiplier : zoom_out_multiplier;
314-
315-
auto relative_rect = this->relative_rect();
316-
Gfx::IntRect zoomed_rect = relative_rect;
317-
318-
zoomed_rect.set_width(zoomed_rect.width() * multiplier);
319-
zoomed_rect.set_height(zoomed_rect.height() * multiplier);
320-
321-
auto leftover_width = abs(relative_rect.width() - zoomed_rect.width());
322-
auto leftover_height = abs(relative_rect.height() - zoomed_rect.height());
323-
324-
double cursor_x_percentage = static_cast<double>(event.position().x()) / relative_rect.width();
325-
double cursor_y_percentage = static_cast<double>(event.position().y()) / relative_rect.height();
326-
327-
zoomed_rect.set_x((zooming_in ? 1 : -1) * leftover_width * cursor_x_percentage);
328-
zoomed_rect.set_y((zooming_in ? 1 : -1) * leftover_height * cursor_y_percentage);
329-
330-
m_set.zoom(zoomed_rect);
331-
update();
342+
zoom(event.wheel_delta() < 0 ? Zoom::In : Zoom::Out, event.position());
332343
}
333344

334345
void Mandelbrot::resize_event(GUI::ResizeEvent& event)

0 commit comments

Comments
 (0)