Mandelbrot: Maintain aspect ratio when selecting a region

This makes sure the aspect ratio of the widget and the selection
match. Otherwise you'd end up with distorted images when zooming
in.
This commit is contained in:
Gunnar Beutner 2021-05-17 16:16:23 +02:00 committed by Andreas Kling
parent 198a4e2a3c
commit c961616e6d

View file

@ -152,7 +152,15 @@ void Mandelbrot::mousedown_event(GUI::MouseEvent& event)
void Mandelbrot::mousemove_event(GUI::MouseEvent& event)
{
if (m_dragging) {
m_selection_end = event.position();
// Maintain aspect ratio
int selection_width = event.position().x() - m_selection_start.x();
int selection_height = event.position().y() - m_selection_start.y();
int aspect_corrected_selection_width = selection_height * width() / height();
int aspect_corrected_selection_height = selection_width * height() / width();
if (selection_width * aspect_corrected_selection_height > aspect_corrected_selection_width * selection_height)
m_selection_end = { event.position().x(), m_selection_start.y() + abs(aspect_corrected_selection_height) * ((selection_height < 0) ? -1 : 1) };
else
m_selection_end = { m_selection_start.x() + abs(aspect_corrected_selection_width) * ((selection_width < 0) ? -1 : 1), event.position().y() };
update();
}