View Javadoc
1   package org.djutils.draw.curve;
2   
3   import org.djutils.draw.curve.Flattener.FlattableCurve;
4   import org.djutils.draw.line.PolyLine2d;
5   import org.djutils.draw.point.DirectedPoint2d;
6   import org.djutils.draw.point.Point2d;
7   
8   /**
9    * This interface narrows down the interface of continuous curves for 2d use.
10   * <p>
11   * Copyright (c) 2023-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13   * </p>
14   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
16   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
17   */
18  public interface Curve2d
19          extends Curve<DirectedPoint2d, Double, Point2d, Flattener2d, PolyLine2d>, FlattableCurve<Point2d, Double>
20  {
21  
22      @Override
23      default DirectedPoint2d getStartPoint()
24      {
25          return new DirectedPoint2d(getPoint(0.0), getDirection(0.0));
26      }
27  
28      @Override
29      default DirectedPoint2d getEndPoint()
30      {
31          return new DirectedPoint2d(getPoint(1.0), getDirection(1.0));
32      }
33  
34      @Override
35      default Double getStartDirection()
36      {
37          return getStartPoint().dirZ;
38      }
39  
40      @Override
41      default Double getEndDirection()
42      {
43          return getEndPoint().dirZ;
44      }
45  
46      @Override
47      default Double getDirection(final double fraction)
48      {
49          Point2d p1, p2;
50          if (fraction < 0.5) // to prevent going above 1.0
51          {
52              p1 = getPoint(fraction);
53              p2 = getPoint(fraction + 1e-6);
54          }
55          else
56          {
57              p1 = getPoint(fraction - 1e-6);
58              p2 = getPoint(fraction);
59          }
60          return p1.directionTo(p2);
61      }
62  
63  }