@@ -209,6 +209,12 @@ class Mandelbrot : public GUI::Frame {
209
209
210
210
void export_image (String const & export_path);
211
211
212
+ enum class Zoom {
213
+ In,
214
+ Out,
215
+ };
216
+ void zoom (Zoom in_out, const Gfx::IntPoint& center);
217
+
212
218
private:
213
219
virtual void paint_event (GUI::PaintEvent&) override ;
214
220
virtual void mousedown_event (GUI::MouseEvent& event) override ;
@@ -227,6 +233,33 @@ class Mandelbrot : public GUI::Frame {
227
233
MandelbrotSet m_set;
228
234
};
229
235
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
+
230
263
void Mandelbrot::paint_event (GUI::PaintEvent& event)
231
264
{
232
265
Frame::paint_event (event);
@@ -306,29 +339,7 @@ void Mandelbrot::mouseup_event(GUI::MouseEvent& event)
306
339
307
340
void Mandelbrot::mousewheel_event (GUI::MouseEvent& event)
308
341
{
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 ());
332
343
}
333
344
334
345
void Mandelbrot::resize_event (GUI::ResizeEvent& event)
0 commit comments