package org.opentrafficsim.demo.ntm; import java.awt.geom.Point2D; import java.util.ArrayList; /** *

* Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License. *

* $LastChangedDate$, @version $Revision$, by $Author$, * initial version 9 Sep 2014
* @author Alexander Verbraeck * @author Hans van Lint * @author Peter Knoppers * @author Guus Tamminga * @author Yufei Yuan */ public class LinearFunctionLibrary { /** * An example (PieceWiseLinear) is shown below: * *

     *   production
     *      |      
     *    Y |      _____________________ 
     *      |     /                      \
     *      |    /                        \ 
     *      |   /                          \  
     *      |  /                            \   
     *      | /                              \ 
     *      |/________________________________\__________ density (number of vehicles)
     *            ^                     ^ 
     *            x1                   x2     x3
     * 
*

* @param xyPairs ArrayList<Point2D>; point that define the curve * @param valueX double; provides te value to execute (returns the Y-value) * @return the Y value of a valueX */ public static double createPieceWiseLinear(final ArrayList xyPairs, final double valueX) { double result = 0; if (valueX < 0) { System.out.println("Strange: negative X value"); } else if (valueX > xyPairs.get(xyPairs.size() - 1).getX()) { System.out.println("Strange: X value above maximum"); } else { Point2D prevPoint = null; for (Point2D p : xyPairs) { if (p.getX() > valueX) { if (prevPoint == null) { System.out.println("test"); } result = prevPoint.getY() + (p.getY() - prevPoint.getY()) * (valueX - prevPoint.getX()) / (p.getX() - prevPoint.getX()); break; } prevPoint = p; } } return result; } }