diff options
author | Nico Weber <thakis@chromium.org> | 2021-08-14 12:32:16 -0400 |
---|---|---|
committer | Gunnar Beutner <gunnar@beutner.name> | 2021-08-14 19:50:44 +0200 |
commit | ad7bfe017f67b4a59011f80996190f9c19c3c5fa (patch) | |
tree | c734c0b3d4089845fab0aaa2758cf5f87b4ec1e2 | |
parent | fbdf17ae6866b60540f41d7c4928f25b5f2a8419 (diff) | |
download | serenity-ad7bfe017f67b4a59011f80996190f9c19c3c5fa.zip |
Mandelbrot: Extract zoom() method
-rw-r--r-- | Userland/Demos/Mandelbrot/Mandelbrot.cpp | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/Userland/Demos/Mandelbrot/Mandelbrot.cpp b/Userland/Demos/Mandelbrot/Mandelbrot.cpp index 68f7d59a88..9a0bb1745d 100644 --- a/Userland/Demos/Mandelbrot/Mandelbrot.cpp +++ b/Userland/Demos/Mandelbrot/Mandelbrot.cpp @@ -209,6 +209,12 @@ class Mandelbrot : public GUI::Frame { void export_image(String const& export_path); + enum class Zoom { + In, + Out, + }; + void zoom(Zoom in_out, const Gfx::IntPoint& center); + private: virtual void paint_event(GUI::PaintEvent&) override; virtual void mousedown_event(GUI::MouseEvent& event) override; @@ -227,6 +233,33 @@ private: MandelbrotSet m_set; }; +void Mandelbrot::zoom(Zoom in_out, const Gfx::IntPoint& center) +{ + static constexpr double zoom_in_multiplier = 0.8; + static constexpr double zoom_out_multiplier = 1.25; + + bool zooming_in = in_out == Zoom::In; + double multiplier = zooming_in ? zoom_in_multiplier : zoom_out_multiplier; + + auto relative_rect = this->relative_rect(); + Gfx::IntRect zoomed_rect = relative_rect; + + zoomed_rect.set_width(zoomed_rect.width() * multiplier); + zoomed_rect.set_height(zoomed_rect.height() * multiplier); + + auto leftover_width = abs(relative_rect.width() - zoomed_rect.width()); + auto leftover_height = abs(relative_rect.height() - zoomed_rect.height()); + + double cursor_x_percentage = static_cast<double>(center.x()) / relative_rect.width(); + double cursor_y_percentage = static_cast<double>(center.y()) / relative_rect.height(); + + zoomed_rect.set_x((zooming_in ? 1 : -1) * leftover_width * cursor_x_percentage); + zoomed_rect.set_y((zooming_in ? 1 : -1) * leftover_height * cursor_y_percentage); + + m_set.zoom(zoomed_rect); + update(); +} + void Mandelbrot::paint_event(GUI::PaintEvent& event) { Frame::paint_event(event); @@ -306,29 +339,7 @@ void Mandelbrot::mouseup_event(GUI::MouseEvent& event) void Mandelbrot::mousewheel_event(GUI::MouseEvent& event) { - static constexpr double zoom_in_multiplier = 0.8; - static constexpr double zoom_out_multiplier = 1.25; - - bool zooming_in = event.wheel_delta() < 0; - double multiplier = zooming_in ? zoom_in_multiplier : zoom_out_multiplier; - - auto relative_rect = this->relative_rect(); - Gfx::IntRect zoomed_rect = relative_rect; - - zoomed_rect.set_width(zoomed_rect.width() * multiplier); - zoomed_rect.set_height(zoomed_rect.height() * multiplier); - - auto leftover_width = abs(relative_rect.width() - zoomed_rect.width()); - auto leftover_height = abs(relative_rect.height() - zoomed_rect.height()); - - double cursor_x_percentage = static_cast<double>(event.position().x()) / relative_rect.width(); - double cursor_y_percentage = static_cast<double>(event.position().y()) / relative_rect.height(); - - zoomed_rect.set_x((zooming_in ? 1 : -1) * leftover_width * cursor_x_percentage); - zoomed_rect.set_y((zooming_in ? 1 : -1) * leftover_height * cursor_y_percentage); - - m_set.zoom(zoomed_rect); - update(); + zoom(event.wheel_delta() < 0 ? Zoom::In : Zoom::Out, event.position()); } void Mandelbrot::resize_event(GUI::ResizeEvent& event) |