Skip to content

Commit c961616

Browse files
gunnarbeutnerawesomekling
authored andcommitted
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.
1 parent 198a4e2 commit c961616

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Userland/Demos/Mandelbrot/Mandelbrot.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,15 @@ void Mandelbrot::mousedown_event(GUI::MouseEvent& event)
152152
void Mandelbrot::mousemove_event(GUI::MouseEvent& event)
153153
{
154154
if (m_dragging) {
155-
m_selection_end = event.position();
155+
// Maintain aspect ratio
156+
int selection_width = event.position().x() - m_selection_start.x();
157+
int selection_height = event.position().y() - m_selection_start.y();
158+
int aspect_corrected_selection_width = selection_height * width() / height();
159+
int aspect_corrected_selection_height = selection_width * height() / width();
160+
if (selection_width * aspect_corrected_selection_height > aspect_corrected_selection_width * selection_height)
161+
m_selection_end = { event.position().x(), m_selection_start.y() + abs(aspect_corrected_selection_height) * ((selection_height < 0) ? -1 : 1) };
162+
else
163+
m_selection_end = { m_selection_start.x() + abs(aspect_corrected_selection_width) * ((selection_width < 0) ? -1 : 1), event.position().y() };
156164
update();
157165
}
158166

0 commit comments

Comments
 (0)