diff --git a/apps/graph/graph/graph_view.cpp b/apps/graph/graph/graph_view.cpp index ac22f7c65..90120e8a4 100644 --- a/apps/graph/graph/graph_view.cpp +++ b/apps/graph/graph/graph_view.cpp @@ -40,18 +40,8 @@ void GraphView::drawRect(KDContext * ctx, KDRect rect) const { } float tmin = f->tMin(); float tmax = f->tMax(); - /* The step is a fraction of tmax-tmin. We will evaluate the function at - * every step and if the consecutive dots are close enough, we won't - * evaluate any more dot within the step. We pick a very strange fraction - * denominator to avoid evaluating a periodic function periodically. For - * example, if tstep was (tmax - tmin)/10, the polar function r(θ) = sin(5θ) - * defined on 0..2π would be evaluated on r(0) = 0, r(π/5) = 0, r(2*π/5) = 0 - * which would lead to no curve at all. With 10.0938275501223, the - * problematic functions are the functions whose period is proportionned to - * 10.0938275501223 which are hopefully rare enough. - * TODO: The drawCurve algorithm should use the derivative function to know - * how fast the function moves... */ - float tstep = (tmax-tmin)/10.0938275501223f; + + float tstep = (tmax-tmin)/k_graphStepDenominator; float tCacheMin, tCacheStep; if (type == ContinuousFunction::PlotType::Cartesian) { @@ -60,7 +50,7 @@ void GraphView::drawRect(KDContext * ctx, KDRect rect) const { tCacheStep = pixelWidth(); } else { tCacheMin = tmin; - tCacheStep = tstep; + tCacheStep = tstep / ContinuousFunctionCache::k_parametricStepFactor; } ContinuousFunctionCache::PrepareForCaching(f.operator->(), cch, tCacheMin, tCacheStep); diff --git a/apps/graph/graph/graph_view.h b/apps/graph/graph/graph_view.h index c3e190a3a..ded212f6d 100644 --- a/apps/graph/graph/graph_view.h +++ b/apps/graph/graph/graph_view.h @@ -7,6 +7,19 @@ namespace Graph { class GraphView : public Shared::FunctionGraphView { public: + /* The step is a fraction of tmax-tmin. We will evaluate the function at + * every step and if the consecutive dots are close enough, we won't + * evaluate any more dot within the step. We pick a very strange fraction + * denominator to avoid evaluating a periodic function periodically. For + * example, if tstep was (tmax - tmin)/10, the polar function r(θ) = sin(5θ) + * defined on 0..2π would be evaluated on r(0) = 0, r(π/5) = 0, r(2*π/5) = 0 + * which would lead to no curve at all. With 10.0938275501223, the + * problematic functions are the functions whose period is proportionned to + * 10.0938275501223 which are hopefully rare enough. + * TODO: The drawCurve algorithm should use the derivative function to know + * how fast the function moves... */ + static constexpr float k_graphStepDenominator = 10.0938275501223f; + GraphView(Shared::InteractiveCurveViewRange * graphRange, Shared::CurveViewCursor * cursor, Shared::BannerView * bannerView, Shared::CursorView * cursorView); void reload() override; diff --git a/apps/shared/continuous_function_cache.cpp b/apps/shared/continuous_function_cache.cpp index 464186226..df24141a9 100644 --- a/apps/shared/continuous_function_cache.cpp +++ b/apps/shared/continuous_function_cache.cpp @@ -7,6 +7,7 @@ namespace Shared { constexpr int ContinuousFunctionCache::k_sizeOfCache; constexpr float ContinuousFunctionCache::k_cacheHitTolerance; constexpr int ContinuousFunctionCache::k_numberOfAvailableCaches; +constexpr int ContinuousFunctionCache::k_parametricStepFactor; // public void ContinuousFunctionCache::PrepareForCaching(void * fun, ContinuousFunctionCache * cache, float tMin, float tStep) { @@ -44,13 +45,6 @@ Poincare::Coordinate2D ContinuousFunctionCache::valueForParameter(const C } // private -float ContinuousFunctionCache::StepFactor(ContinuousFunction * function) { - /* When drawing a parametric or polar curve, the range is first divided by - * ~10,9, creating 11 intervals which are filled by dichotomy. - * We memoize 16 values for each of the 10 big intervals. */ - return (function->plotType() == ContinuousFunction::PlotType::Cartesian) ? 1.f : 16.f; -} - void ContinuousFunctionCache::invalidateBetween(int iInf, int iSup) { for (int i = iInf; i < iSup; i++) { m_cache[i] = NAN; @@ -59,7 +53,7 @@ void ContinuousFunctionCache::invalidateBetween(int iInf, int iSup) { void ContinuousFunctionCache::setRange(ContinuousFunction * function, float tMin, float tStep) { m_tMin = tMin; - m_tStep = tStep / StepFactor(function); + m_tStep = tStep; } int ContinuousFunctionCache::indexForParameter(const ContinuousFunction * function, float t) const { diff --git a/apps/shared/continuous_function_cache.h b/apps/shared/continuous_function_cache.h index b48617214..ee5bf598d 100644 --- a/apps/shared/continuous_function_cache.h +++ b/apps/shared/continuous_function_cache.h @@ -1,6 +1,7 @@ #ifndef SHARED_CONTINUOUS_FUNCTION_CACHE_H #define SHARED_CONTINUOUS_FUNCTION_CACHE_H +#include "../graph/graph/graph_view.h" #include #include #include @@ -10,10 +11,13 @@ namespace Shared { class ContinuousFunction; class ContinuousFunctionCache { -public: +private: /* The size of the cache is chosen to optimize the display of cartesian - * function */ + * functions */ + static constexpr int k_sizeOfCache = Ion::Display::Width; +public: static constexpr int k_numberOfAvailableCaches = 2; + static constexpr int k_parametricStepFactor = k_sizeOfCache / int(Graph::GraphView::k_graphStepDenominator); static void PrepareForCaching(void * fun, ContinuousFunctionCache * cache, float tMin, float tStep); @@ -23,13 +27,8 @@ public: void clear(); Poincare::Coordinate2D valueForParameter(const ContinuousFunction * function, Poincare::Context * context, float t); private: - /* The size of the cache is chosen to optimize the display of cartesian - * function */ - static constexpr int k_sizeOfCache = Ion::Display::Width; static constexpr float k_cacheHitTolerance = 1e-3; - static float StepFactor(ContinuousFunction * function); - void invalidateBetween(int iInf, int iSup); void setRange(ContinuousFunction * function, float tMin, float tStep); int indexForParameter(const ContinuousFunction * function, float t) const;