From 8f5c11cba06b4f70b13ab1b1f853c6c206e328cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 19 Oct 2016 17:39:32 +0200 Subject: [PATCH] [apps/graph/values] Enable displaying the derivative function values Change-Id: I3b970f20e16c8b3943f060abb0d8be534102c97b --- apps/graph/values/values_controller.cpp | 81 ++++++++++++++++++++++--- apps/graph/values/values_controller.h | 7 ++- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/apps/graph/values/values_controller.cpp b/apps/graph/values/values_controller.cpp index 203f2c979..c6c6c8aa8 100644 --- a/apps/graph/values/values_controller.cpp +++ b/apps/graph/values/values_controller.cpp @@ -54,7 +54,13 @@ int ValuesController::numberOfRows() { }; int ValuesController::numberOfColumns() { - return 1 + m_functionStore->numberOfActiveFunctions(); + int result = 1; + for (int i = 0; i < m_functionStore->numberOfFunctions(); i++) { + if (m_functionStore->functionAtIndex(i)->isActive()) { + result += 1 + m_functionStore->functionAtIndex(i)->displayDerivative(); + } + } + return result; }; KDCoordinate ValuesController::rowHeight(int j) { @@ -143,10 +149,10 @@ void ValuesController::didBecomeFirstResponder() { if (m_activeCellY == -1) { setActiveCell(0,0); } else { - if (m_activeCellX < m_functionStore->numberOfActiveFunctions()) { + if (m_activeCellX < numberOfColumns()) { setActiveCell(m_activeCellX, m_activeCellY); } else { - setActiveCell(m_functionStore->numberOfActiveFunctions(), m_activeCellY); + setActiveCell(numberOfColumns() - 1, m_activeCellY); } } } @@ -188,7 +194,11 @@ bool ValuesController::handleEvent(Ion::Events::Event event) { configureAbscissa(); return true; } - configureFunction(); + if (isDerivativeColumn(m_activeCellX)) { + configureDerivativeFunction(); + } else { + configureFunction(); + } return true; } if (m_activeCellX == 0) { @@ -215,12 +225,16 @@ void ValuesController::configureAbscissa() { } void ValuesController::configureFunction() { - Function * function = m_functionStore->activeFunctionAtIndex(m_activeCellX-1); + Function * function = functionAtColumn(m_activeCellX); m_functionParameterController.setFunction(function); StackViewController * stack = ((StackViewController *)parentResponder()); stack->push(&m_functionParameterController); } +void ValuesController::configureDerivativeFunction() { + // TODO: push a derivativeParameterController +} + void ValuesController::editValue(bool overwrite, char initialDigit) { /* This code assumes that the active cell remains the one which is edited * until the invocation is performed. This could lead to concurrency issue in @@ -309,8 +323,13 @@ void ValuesController::willDisplayCellAtLocation(View * cell, int i, int j) { return; } FunctionTitleCell * myFunctionCell = (FunctionTitleCell *)cell; - Function * function = m_functionStore->activeFunctionAtIndex(i-1); + Function * function = functionAtColumn(i); myFunctionCell->setColor(function->color()); + if (isDerivativeColumn(i)) { + myFunctionCell->setDerivative(true); + } else { + myFunctionCell->setDerivative(false); + } myFunctionCell->setText(function->name(), function->color()); return; } @@ -334,10 +353,56 @@ void ValuesController::willDisplayCellAtLocation(View * cell, int i, int j) { myValueCell->setText(buffer); return; } - Function * function = m_functionStore->activeFunctionAtIndex(i-1); + Function * function = functionAtColumn(i); float x = m_interval.element(j-1); - Float(function->evaluateAtAbscissa(x, m_evaluateContext)).convertFloatToText(buffer, 14, 7); + if (isDerivativeColumn(i)) { + Float(function->approximateDerivative(x, m_evaluateContext)).convertFloatToText(buffer, 14, 3); + } else { + Float(function->evaluateAtAbscissa(x, m_evaluateContext)).convertFloatToText(buffer, 14, 7); + } myValueCell->setText(buffer); } +Function * ValuesController::functionAtColumn(int i) { + assert(i > 0); + int index = 1; + for (int k = 0; k < m_functionStore->numberOfFunctions(); k++) { + if (m_functionStore->functionAtIndex(k)->isActive()) { + if (i == index) { + return m_functionStore->functionAtIndex(k); + } + index++; + if (m_functionStore->functionAtIndex(k)->displayDerivative()) { + if (i == index) { + return m_functionStore->functionAtIndex(k); + } + index++; + } + } + } + assert(false); + return nullptr; +} + +bool ValuesController::isDerivativeColumn(int i) { + assert(i >= 1); + int index = 1; + for (int k = 0; k < m_functionStore->numberOfFunctions(); k++) { + if (m_functionStore->functionAtIndex(k)->isActive()) { + if (i == index) { + return false; + } + index++; + if (m_functionStore->functionAtIndex(k)->displayDerivative()) { + if (i == index) { + return true; + } + index++; + } + } + } + assert(false); + return false; +} + } diff --git a/apps/graph/values/values_controller.h b/apps/graph/values/values_controller.h index 437dc58d4..c96ca942f 100644 --- a/apps/graph/values/values_controller.h +++ b/apps/graph/values/values_controller.h @@ -24,8 +24,6 @@ public: Interval * interval(); ValueCell * abscisseCellAtRow(int rowIndex); void editValue(bool overwrite, char initialDigit = 0); - void configureAbscissa(); - void configureFunction(); const char * title() const override; bool handleEvent(Ion::Events::Event event) override; @@ -56,7 +54,12 @@ public: static constexpr KDCoordinate k_ordinateCellWidth = 100; private: + Function * functionAtColumn(int i); + bool isDerivativeColumn(int i); Responder * tabController() const; + void configureAbscissa(); + void configureFunction(); + void configureDerivativeFunction(); constexpr static int k_maxNumberOfCells = 40; constexpr static int k_maxNumberOfFunctions = 5; // !!! CAUTION: The order here is important