From 09c061e871299394a7ec3333aea96ec5049e5ee3 Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Mon, 12 Oct 2020 12:44:30 +0200 Subject: [PATCH] [poincare/zoom] Method RangeFromSingleValue This method adds margins to a range whose bounds are equal. Change-Id: I2b2fe26fe431deda112389060d401f738a75b1ae --- apps/shared/range_1D.h | 2 +- poincare/include/poincare/zoom.h | 4 ++++ poincare/src/zoom.cpp | 17 +++++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/apps/shared/range_1D.h b/apps/shared/range_1D.h index b89b316d8..bd1b7a3b9 100644 --- a/apps/shared/range_1D.h +++ b/apps/shared/range_1D.h @@ -19,7 +19,7 @@ class __attribute__((packed)) Range1D final { public: /* If m_min and m_max are too close, we cannot divide properly the range by * the number of pixels, which creates a drawing problem. */ - constexpr static float k_minFloat = 1E-4f; + constexpr static float k_minFloat = Poincare::Zoom::k_minimalRangeLength; constexpr static float k_default = Poincare::Zoom::k_defaultHalfRange; Range1D(float min = -k_default, float max = k_default) : m_min(min), diff --git a/poincare/include/poincare/zoom.h b/poincare/include/poincare/zoom.h index a12c9f1d8..958c88756 100644 --- a/poincare/include/poincare/zoom.h +++ b/poincare/include/poincare/zoom.h @@ -12,6 +12,7 @@ public: static constexpr float k_smallUnitMantissa = 1.f; static constexpr float k_mediumUnitMantissa = 2.f; static constexpr float k_largeUnitMantissa = 5.f; + static constexpr float k_minimalRangeLength = 1e-4f; typedef float (*ValueAtAbscissa)(float abscissa, Context * context, const void * auxiliary); @@ -25,6 +26,9 @@ public: * of the smallest axis. If it is true, the longest axis will be reduced.*/ static void SetToRatio(float yxRatio, float * xMin, float * xMax, float * yMin, float * yMax, bool shrink = false); + /* Compute a default range so that boundMin < value < boundMax */ + static void RangeFromSingleValue(float value, float * boundMin, float * boundMax); + private: static constexpr int k_peakNumberOfPointsOfInterest = 3; static constexpr int k_sampleSize = Ion::Display::Width / 4; diff --git a/poincare/src/zoom.cpp b/poincare/src/zoom.cpp index b90198405..75e236401 100644 --- a/poincare/src/zoom.cpp +++ b/poincare/src/zoom.cpp @@ -18,7 +18,8 @@ constexpr float Zoom::k_maxRatioBetweenPointsOfInterest, Zoom::k_smallUnitMantissa, Zoom::k_mediumUnitMantissa, - Zoom::k_largeUnitMantissa; + Zoom::k_largeUnitMantissa, + Zoom::k_minimalRangeLength; bool Zoom::InterestingRangesForDisplay(ValueAtAbscissa evaluation, float * xMin, float * xMax, float * yMin, float * yMax, float tMin, float tMax, Context * context, const void * auxiliary) { assert(xMin && xMax && yMin && yMax); @@ -191,9 +192,7 @@ void Zoom::RefinedYRangeForDisplay(ValueAtAbscissa evaluation, float xMin, float *yMin = std::min(*yMin, sampleYMin); *yMax = std::max(*yMax, sampleYMax); if (*yMin == *yMax) { - float d = (*yMin == 0.f) ? 1.f : *yMin * 0.2f; - *yMin -= d; - *yMax += d; + RangeFromSingleValue(*yMin, yMin, yMax); } /* Round out the smallest bound to 0 if it is negligible compare to the * other one. This way, we can display the X axis for positive functions such @@ -263,6 +262,16 @@ void Zoom::RangeWithRatioForDisplay(ValueAtAbscissa evaluation, float yxRatio, f SetToRatio(yxRatio, xMin, xMax, yMin, yMax, true); } +void Zoom::RangeFromSingleValue(float value, float * boundMin, float * boundMax) { + constexpr float margin = 0.2f; + float delta = margin * std::fabs(value); + if (delta < k_minimalRangeLength) { + delta = 1.f; + } + *boundMin = value - delta; + *boundMax = value + delta; +} + bool Zoom::IsConvexAroundExtremum(ValueAtAbscissa evaluation, float x1, float x2, float x3, float y1, float y2, float y3, Context * context, const void * auxiliary, int iterations) { if (iterations <= 0) { return false;