diff --git a/apps/graph/test/ranges.cpp b/apps/graph/test/ranges.cpp index 4d13c1546..d73f1e3cc 100644 --- a/apps/graph/test/ranges.cpp +++ b/apps/graph/test/ranges.cpp @@ -125,4 +125,52 @@ QUIZ_CASE(graph_ranges_several_functions) { } } +void assert_zooms_to(float xMin, float xMax, float yMin, float yMax, float targetXMin, float targetXMax, float targetYMin, float targetYMax, bool conserveRatio, bool zoomIn) { + float ratio = zoomIn ? 1.f / ZoomCurveViewController::k_zoomOutRatio : ZoomCurveViewController::k_zoomOutRatio; + + InteractiveCurveViewRange graphRange; + graphRange.setXMin(xMin); + graphRange.setXMax(xMax); + graphRange.setYMin(yMin); + graphRange.setYMax(yMax); + + float xCenter = (xMax + xMin) / 2.f; + float yCenter = (yMax + yMin) / 2.f; + + graphRange.zoom(ratio, xCenter, yCenter); + + quiz_assert(float_equal(graphRange.xMin(), targetXMin) && float_equal(graphRange.xMax(), targetXMax) && float_equal(graphRange.yMin(), targetYMin) && float_equal(graphRange.yMax(), targetYMax)); + quiz_assert(float_equal((yMax - yMin) / (xMax - xMin), (targetYMax - targetYMin) / (targetXMax - targetXMin)) == conserveRatio); +} + +void assert_zooms_in_to(float xMin, float xMax, float yMin, float yMax, float targetXMin, float targetXMax, float targetYMin, float targetYMax, bool conserveRatio) { + assert_zooms_to(xMin, xMax, yMin, yMax, targetXMin, targetXMax, targetYMin, targetYMax, conserveRatio, true); +} + +void assert_zooms_out_to(float xMin, float xMax, float yMin, float yMax, float targetXMin, float targetXMax, float targetYMin, float targetYMax, bool conserveRatio) { + assert_zooms_to(xMin, xMax, yMin, yMax, targetXMin, targetXMax, targetYMin, targetYMax, conserveRatio, false); +} + +QUIZ_CASE(graph_ranges_zoom) { + assert_zooms_in_to( + -12, 12, -12, 12, + -8, 8, -8, 8, + true); + + assert_zooms_in_to( + -3, 3, 0, 1e-4, + -3, 3, 0, 1e-4, + true); + + assert_zooms_out_to( + -10, 10, -10, 10, + -15, 15, -15, 15, + true); + + assert_zooms_out_to( + -1, 1, 9e7, 1e8, + -1.5, 1.5, 87500000, 1e8, + false); +} + } diff --git a/apps/shared/zoom_curve_view_controller.cpp b/apps/shared/zoom_curve_view_controller.cpp index 45c25078d..77778be78 100644 --- a/apps/shared/zoom_curve_view_controller.cpp +++ b/apps/shared/zoom_curve_view_controller.cpp @@ -14,7 +14,7 @@ bool ZoomCurveViewController::handleEvent(Ion::Events::Event event) { } bool ZoomCurveViewController::handleZoom(Ion::Events::Event event) { - float ratio = event == Ion::Events::Plus ? 2.0f/3.0f : 3.0f/2.0f; + float ratio = event == Ion::Events::Plus ? 1.f / k_zoomOutRatio : k_zoomOutRatio; interactiveCurveViewRange()->zoom(ratio, xFocus(), yFocus()); curveView()->reload(); return true; diff --git a/apps/shared/zoom_curve_view_controller.h b/apps/shared/zoom_curve_view_controller.h index 97698ae76..da96a4d8d 100644 --- a/apps/shared/zoom_curve_view_controller.h +++ b/apps/shared/zoom_curve_view_controller.h @@ -13,6 +13,8 @@ namespace Shared { class ZoomCurveViewController : public ViewController { public: + static constexpr float k_zoomOutRatio = 3.f / 2.f; + ZoomCurveViewController(Responder * parentResponder) : ViewController(parentResponder) {} View * view() override { return curveView(); } bool handleEvent(Ion::Events::Event event) override;