From 19e8b2ee1cbeddb6e5ac5046530856eebdadffb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 30 Nov 2018 16:45:11 +0100 Subject: [PATCH] [apps] Fix InteractiveCurveViewController::addMargin --- .../interactive_curve_view_controller.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/apps/shared/interactive_curve_view_controller.cpp b/apps/shared/interactive_curve_view_controller.cpp index 73b36bf4e..87210bc06 100644 --- a/apps/shared/interactive_curve_view_controller.cpp +++ b/apps/shared/interactive_curve_view_controller.cpp @@ -37,7 +37,34 @@ InteractiveCurveViewController::InteractiveCurveViewController(Responder * paren } float InteractiveCurveViewController::addMargin(float x, float range, bool isMin) { + /* We are adding margins. Let's name: + * - The current range: rangeBefore + * - The next range: rangeAfter + * - The bottom margin ratio with which we will evaluate if a point is too + * low on the screen: bottomRatioAfter + * - The bottom margin ratio with which we will evaluate if a point is too + * high on the screen: topRatioAfter + * - The ratios we need to use to create the margins: bottomRatioBefore and + * topRatioBefore + * + * We want to add margins so that: + * bottomRatioAfter*rangeAfter == bottomRatioBefore * rangeBefore + * topRatioAfter*rangeAfter == topRatioBefore * rangeBefore + * Knowing that: + * rangeAfter = (1+bottomRatioBefore+topRatioBefore)*rangeBefore + * + * We thus have: + * bottomRatioBefore = bottomRatioAfter / (1-bottomRatioAfter-topRatioAfter) + * topRatioBefore = topRatioAfter / (1-bottomRatioAfter-topRatioAfter) + * + * If we just used bottomRatioBefore = bottomRatioAfter and + * topRatioBefore = topRatioAfter, we would create too small margins and the + * controller might need to pan right after a Y auto calibration. */ + + assert(displayBottomMarginRatio()+displayTopMarginRatio() < 1); // Assertion so that the formula is correct + float ratioDenominator = 1-displayBottomMarginRatio()-displayTopMarginRatio(); float ratio = isMin ? -displayBottomMarginRatio() : displayTopMarginRatio(); + ratio = ratio / ratioDenominator; return x+ratio*range; }