diff --git a/apps/shared/cartesian_function.cpp b/apps/shared/cartesian_function.cpp index f915acd33..5fb0cc53e 100644 --- a/apps/shared/cartesian_function.cpp +++ b/apps/shared/cartesian_function.cpp @@ -2,6 +2,7 @@ #include "expression_model_store.h" #include "poincare_helpers.h" #include +#include #include #include #include @@ -106,14 +107,40 @@ CartesianFunction::PlotType CartesianFunction::plotType() const { return recordData()->plotType(); } -void CartesianFunction::setPlotType(PlotType plotType) { +void CartesianFunction::setPlotType(PlotType newPlotType) { + PlotType currentPlotType = plotType(); + if (newPlotType == currentPlotType) { + return; + } /* Reset memoized layout. */ + Expression e = expressionClone(); m_model.tidy(); - double tMin = plotType == PlotType::Cartesian ? -INFINITY : 0.0; - double tMax = plotType == PlotType::Cartesian ? INFINITY : 360.0; + double tMin = newPlotType == PlotType::Cartesian ? -INFINITY : 0.0; + double tMax = newPlotType == PlotType::Cartesian ? INFINITY : 360.0; setTMin(tMin); setTMax(tMax); - return recordData()->setPlotType(plotType); + recordData()->setPlotType(newPlotType); + if (currentPlotType == PlotType::Parametric) { + // Change [x(t) y(t)] to y(t) + if (e.type() == ExpressionNode::Type::Matrix + && static_cast(e).numberOfRows() == 2 + && static_cast(e).numberOfColumns() == 1) + { + Expression nextContent = e.childAtIndex(1); + /* We need to detach it, otherwise nextContent will think it has a parent + * when we retrieve it from the storage. */ + nextContent.detachFromParent(); + setExpressionContent(nextContent); + } + return; + } else if (newPlotType == PlotType::Parametric) { + // Change y(t) to [t y(t)] + Matrix newExpr = Matrix::Builder(); + newExpr.addChildAtIndexInPlace(Symbol::Builder(UCodePointUnknownX), 0, 0); + newExpr.addChildAtIndexInPlace(e, newExpr.numberOfChildren(), newExpr.numberOfChildren()); + newExpr.setDimensions(2, 1); + setExpressionContent(newExpr); + } } template diff --git a/poincare/include/poincare/tree_handle.h b/poincare/include/poincare/tree_handle.h index 959141249..62b296c70 100644 --- a/poincare/include/poincare/tree_handle.h +++ b/poincare/include/poincare/tree_handle.h @@ -4,8 +4,11 @@ #include #include -namespace Poincare { +namespace Shared { + class CartesianFunction; +} +namespace Poincare { /* TODO: implement an iterator over the children, so we can use "for (TreeHandle * c : children)" instead of a for loop over the child index. This should be * faster, as we do not have to recompute childAtIndex(i) at each iteration. @@ -22,6 +25,7 @@ namespace Poincare { class TreeHandle { template friend class ArrayBuilder; + friend class ::Shared::CartesianFunction; friend class TreeNode; friend class TreePool; public: