[chronojump] JumpsDjOptimalFall manages ok inverted paraboles and other problems



commit dbc50abe8799772ea4797ab73c2a7cd27792b7ae
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Dec 27 14:37:18 2019 +0100

    JumpsDjOptimalFall manages ok inverted paraboles and other problems

 src/gui/app1/chronojumpJumpsDjOptimalFall.cs |  1 +
 src/gui/jumpsDjOptimalFall.cs                | 71 +++++++++++++++++++++-------
 src/jumpsDjOptimalFall.cs                    |  5 ++
 src/utilMath.cs                              | 25 +++++++++-
 4 files changed, 85 insertions(+), 17 deletions(-)
---
diff --git a/src/gui/app1/chronojumpJumpsDjOptimalFall.cs b/src/gui/app1/chronojumpJumpsDjOptimalFall.cs
index afbcc40f..6518e7ff 100644
--- a/src/gui/app1/chronojumpJumpsDjOptimalFall.cs
+++ b/src/gui/app1/chronojumpJumpsDjOptimalFall.cs
@@ -56,6 +56,7 @@ public partial class ChronoJumpWindow
                        JumpsDjOptimalFallGraph jdofg = new JumpsDjOptimalFallGraph(
                                        jumpsDjOptimalFall.Point_l,
                                        jumpsDjOptimalFall.Coefs,
+                                       jumpsDjOptimalFall.ParaboleType, //model
                                        jumpsDjOptimalFall.XatMaxY, //model
                                        jumpsDjOptimalFall.GetMaxValue(),
                                        drawingarea_jumps_dj_optimal_fall,
diff --git a/src/gui/jumpsDjOptimalFall.cs b/src/gui/jumpsDjOptimalFall.cs
index 1c51bda7..b63e6bd9 100644
--- a/src/gui/jumpsDjOptimalFall.cs
+++ b/src/gui/jumpsDjOptimalFall.cs
@@ -28,6 +28,7 @@ public class JumpsDjOptimalFallGraph
 {
        List<Point> point_l;
        double[] coefs;
+       LeastSquares.ParaboleTypes paraboleType;
        double xAtMMaxY;
        double pointsMaxValue;
        DrawingArea area;
@@ -66,11 +67,15 @@ public class JumpsDjOptimalFallGraph
        }
 
        //regular constructor
-       public JumpsDjOptimalFallGraph (List<Point> point_l, double[] coefs, double xAtMMaxY, //x at Model 
MaxY
+       public JumpsDjOptimalFallGraph (
+                       List<Point> point_l, double[] coefs,
+                       LeastSquares.ParaboleTypes paraboleType,
+                       double xAtMMaxY, //x at Model MaxY
                        double pointsMaxValue, DrawingArea area, string title, string date)
        {
                this.point_l = point_l;
                this.coefs = coefs;
+               this.paraboleType = paraboleType;
                this.xAtMMaxY = xAtMMaxY;
                this.pointsMaxValue = pointsMaxValue;
                this.area = area;
@@ -94,8 +99,15 @@ public class JumpsDjOptimalFallGraph
 
                if(coefs.Length == 3)
                {
-                       plotPredictedMaxPoint();
-                       writeText();
+                       if(paraboleType == LeastSquares.ParaboleTypes.CONVEX)
+                       {
+                               plotPredictedMaxPoint();
+                               writeTextPredictedPoint();
+                       }
+                       else
+                               writeTextConcaveParabole();
+               } else {
+                       writeTextNeed3PointsWithDifferentFall();
                }
 
                endGraph();
@@ -150,7 +162,7 @@ public class JumpsDjOptimalFallGraph
                        maxY += .5 * maxY;
                }
 
-               if(coefs.Length == 3)
+               if(coefs.Length == 3 && paraboleType == LeastSquares.ParaboleTypes.CONVEX)
                {
                        yAtMMaxY = coefs[0] + coefs[1]*xAtMMaxY + coefs[2]*Math.Pow(xAtMMaxY,2);
                        absoluteMaxY = yAtMMaxY;
@@ -171,8 +183,8 @@ public class JumpsDjOptimalFallGraph
                printText(graphWidth - Convert.ToInt32(outerMargins/2), graphHeight - outerMargins, 0, 
textHeight, "Fall (cm)", g, false);
 
                //2 paint grid: horizontal, vertical
-               paintCairoGrid (minY, absoluteMaxY, 5, true);
-               paintCairoGrid (minX, maxX, 5, false);
+               paintGrid (minY, absoluteMaxY, 5, true);
+               paintGrid (minX, maxX, 5, false);
        }
 
        private void plotPredictedLine()
@@ -180,6 +192,8 @@ public class JumpsDjOptimalFallGraph
                bool firstValue = false;
                double minMax50Percent = (minX + maxX)/2;
                double xgraphOld = 0;
+               bool wasOutOfMargins = false; //avoids to not draw a line between the end point of a line on 
a margin and the start point again of that line
+
                for(double x = minX - minMax50Percent; x < maxX + minMax50Percent; x += (maxX-minX)/200)
                {
                        double xgraph = calculatePaintX(
@@ -199,7 +213,15 @@ public class JumpsDjOptimalFallGraph
                        if(
                                        xgraph < outerMargins || xgraph > graphWidth - outerMargins ||
                                        ygraph < outerMargins || ygraph > graphHeight - outerMargins )
+                       {
+                               wasOutOfMargins = true;
                                continue;
+                       } else {
+                               if(wasOutOfMargins)
+                                       g.MoveTo(xgraph, ygraph);
+
+                               wasOutOfMargins = false;
+                       }
 
                        if(! firstValue)
                                g.LineTo(xgraph, ygraph);
@@ -261,12 +283,29 @@ public class JumpsDjOptimalFallGraph
                g.Stroke ();
        }
 
-       private void writeText()
+       private void writeTextPredictedPoint()
+       {
+               writeTextAtRight(0, "Optimal values:");
+               writeTextAtRight(1, "Fall: " + Util.TrimDecimals(xAtMMaxY, 2) + " cm");
+               writeTextAtRight(2, "Jump height: " + Util.TrimDecimals(yAtMMaxY, 2) + " cm");
+       }
+
+       private void writeTextConcaveParabole()
+       {
+               writeTextAtRight(0, "Error:");
+               writeTextAtRight(1, "Parabole is concave");
+       }
+
+       private void writeTextNeed3PointsWithDifferentFall()
+       {
+               writeTextAtRight(0, "Error:");
+               writeTextAtRight(1, "Need at least 3 points");
+               writeTextAtRight(2, "with different fall heights");
+       }
+
+       private void writeTextAtRight(int line, string text)
        {
-               //at right
-               printText(graphWidth + outerMargins, Convert.ToInt32(graphHeight/2) - textHeight*2, 0, 
textHeight, "Optimal values:", g, false);
-               printText(graphWidth + outerMargins, Convert.ToInt32(graphHeight/2),      0, textHeight, 
"Fall: " + Util.TrimDecimals(xAtMMaxY, 2) + " cm", g, false);
-               printText(graphWidth + outerMargins, Convert.ToInt32(graphHeight/2) + textHeight*2, 0, 
textHeight, "Jump height: " + Util.TrimDecimals(yAtMMaxY, 2) + " cm", g, false);
+               printText(graphWidth + Convert.ToInt32(outerMargins/2), Convert.ToInt32(graphHeight/2) + 
textHeight*2*line, 0, textHeight, text, g, false);
        }
 
        private void endGraph()
@@ -275,9 +314,9 @@ public class JumpsDjOptimalFallGraph
                g.Dispose ();
        }
 
-       private void paintCairoGrid (double min, double max, int seps, bool horiz)
+       private void paintGrid (double min, double max, int seps, bool horiz)
        {
-               LogB.Information(string.Format("paintCairoGrid: {0}, {1}, {2}, {3}", min, max, seps, horiz));
+               LogB.Information(string.Format("paintGrid: {0}, {1}, {2}, {3}", min, max, seps, horiz));
                //show 5 steps positive, 5 negative (if possible)
                int temp = Convert.ToInt32(Util.DivideSafe(max - min, seps));
                int step = temp;
@@ -302,7 +341,7 @@ public class JumpsDjOptimalFallGraph
                g.SetDash(new double[]{1, 2}, 0);
                // i <= max*1.5 to allow to have grid just above the maxpoint if it's below innermargins
                // see: if(ytemp < outerMargins) continue;
-               for(int i = Convert.ToInt32(min); i <= max *1.5 ; i += step)
+               for(double i = min; i <= max *1.5 ; i += step)
                {
                        //LogB.Information("i: " + i.ToString());
                        if(horiz)
@@ -312,14 +351,14 @@ public class JumpsDjOptimalFallGraph
                                        continue;
                                g.MoveTo(outerMargins, ytemp);
                                g.LineTo(graphWidth - outerMargins, ytemp);
-                               printText(Convert.ToInt32(outerMargins/2), ytemp, 0, textHeight, 
i.ToString(), g, true);
+                               printText(Convert.ToInt32(outerMargins/2), ytemp, 0, textHeight, 
Convert.ToInt32(i).ToString(), g, true);
                        } else {
                                int xtemp = Convert.ToInt32(calculatePaintX(i, graphWidth, max, min, 
outerMargins + innerMargins, outerMargins + innerMargins));
                                if(xtemp > graphWidth)
                                        continue;
                                g.MoveTo(xtemp, graphHeight - outerMargins);
                                g.LineTo(xtemp, outerMargins);
-                               printText(xtemp, graphHeight - Convert.ToInt32(outerMargins/2), 0, 
textHeight, i.ToString(), g, true);
+                               printText(xtemp, graphHeight - Convert.ToInt32(outerMargins/2), 0, 
textHeight, Convert.ToInt32(i).ToString(), g, true);
                        }
                }
                g.Stroke ();
diff --git a/src/jumpsDjOptimalFall.cs b/src/jumpsDjOptimalFall.cs
index 6f825b59..d71baede 100644
--- a/src/jumpsDjOptimalFall.cs
+++ b/src/jumpsDjOptimalFall.cs
@@ -88,6 +88,11 @@ public class JumpsDjOptimalFall
                }
        }
 
+       public LeastSquares.ParaboleTypes ParaboleType
+       {
+               get { return ls.ParaboleType; }
+       }
+
        public double XatMaxY //model
        {
                get {
diff --git a/src/utilMath.cs b/src/utilMath.cs
index 2756d7c8..ef8b195f 100644
--- a/src/utilMath.cs
+++ b/src/utilMath.cs
@@ -40,6 +40,12 @@ public class Point
        public double Y {
                get { return y; }
        }
+
+       public override string ToString()
+       {
+               return string.Format("X:{0}, Y:{1}", x, y);
+       }
+
 }
 
 
@@ -56,6 +62,7 @@ public class LeastSquares
 
        public bool CalculatedXatMaxY;
        public double XatMaxY;
+       public enum ParaboleTypes { NOTCALCULATED, STRAIGHT, CONVEX, CONCAVE } //CONVEX is usually OK
 
        //constructor
        public LeastSquares() {
@@ -157,5 +164,21 @@ public class LeastSquares
                XatMaxY = - Coef[1] / (2 * Coef[2]);
                CalculatedXatMaxY = true;
        }
-}
 
+       public ParaboleTypes ParaboleType
+       {
+               get {
+                       if(! CalculatedCoef)
+                               return ParaboleTypes.NOTCALCULATED;
+
+                       if(Coef[2] == 0)
+                               return ParaboleTypes.STRAIGHT;
+
+                       if(Coef[2] > 0)
+                               return ParaboleTypes.CONCAVE;
+
+                       return ParaboleTypes.CONVEX;
+               }
+       }
+
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]