View Javadoc
1   package demo;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.djutils.draw.DrawRuntimeException;
7   import org.djutils.draw.line.LineSegment2d;
8   import org.djutils.draw.line.PolyLine2d;
9   import org.djutils.draw.point.Point2d;
10  
11  /**
12   * LineDemos.java.
13   * <p>
14   * Copyright (c) 2021-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
16   * </p>
17   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   */
20  public final class LineDemos
21  {
22      /**
23       * Do not instantiate.
24       */
25      private LineDemos()
26      {
27          // Do not instantiate
28      }
29  
30      /**
31       * Demonstrate the line classes.
32       * @param args String[]; the command line arguments (not used)
33       * @throws DrawRuntimeException ...
34       */
35      public static void main(final String[] args) throws DrawRuntimeException
36      {
37          PolyLine2d pl1 = new PolyLine2d(new Point2d(1, 2), new Point2d(3, 4), new Point2d(20, -5));
38          System.out.println(pl1);
39          Point2d[] pointArray = new Point2d[] { new Point2d(1, 2), new Point2d(3, 4), new Point2d(20, -5) };
40          PolyLine2d pl2 = new PolyLine2d(pointArray);
41          System.out.println(pl2);
42          double[] x = new double[] { 1, 3, 20 };
43          double[] y = new double[] { 2, 4, -5 };
44          PolyLine2d pl3 = new PolyLine2d(x, y);
45          System.out.println(pl3);
46          List<Point2d> pointList = new ArrayList<>();
47          pointList.add(new Point2d(1, 2));
48          pointList.add(new Point2d(3, 4));
49          pointList.add(new Point2d(20, -5));
50          PolyLine2d pl4 = new PolyLine2d(pointList);
51          System.out.println(pl4);
52  
53          PolyLine2d polyLine2d = new PolyLine2d(new Point2d(1, 1), new Point2d(5, 1), new Point2d(5, 2), new Point2d(9, 5));
54          System.out.println("PolyLine: " + polyLine2d);
55          System.out.println("length: " + polyLine2d.getLength());
56          System.out.println("fragment: " + polyLine2d.extract(2.0, 9.0));
57          System.out.println("fragment: " + polyLine2d.extractFractional(0.2, 0.9));
58  
59          System.out.println("PolyLine: " + polyLine2d);
60          System.out.println("location at distance 7.0: " + polyLine2d.getLocation(7.0));
61          System.out.println("extended location at distance 15: " + polyLine2d.getLocationExtended(15.0));
62          System.out.println("extended location at distance -8: " + polyLine2d.getLocationExtended(-8.0));
63  
64          System.out.println("PolyLine: " + polyLine2d);
65          for (int index = 0; index < polyLine2d.size() - 1; index++)
66          {
67              System.out.println("segment " + index + ": " + polyLine2d.getSegment(index));
68          }
69  
70          System.out.print(polyLine2d.toPlot());
71  
72          System.out.println("PolyLine: " + polyLine2d);
73          System.out.println("closest point to (0,1): " + polyLine2d.closestPointOnPolyLine(new Point2d(0, 1)));
74          System.out.println("closest point to (6,0): " + polyLine2d.closestPointOnPolyLine(new Point2d(6, 0)));
75          System.out.println("closest point to (10,0): " + polyLine2d.closestPointOnPolyLine(new Point2d(10, 0)));
76          System.out.println("closest point to (50,0): " + polyLine2d.closestPointOnPolyLine(new Point2d(50, 0)));
77          System.out.println("project (0,0) orthogonal: " + polyLine2d.projectOrthogonal(new Point2d(0, 0)));
78          System.out.println("project (4,0) orthogonal: " + polyLine2d.projectOrthogonal(new Point2d(4, 0)));
79          System.out.println("project (5,0) orthogonal: " + polyLine2d.projectOrthogonal(new Point2d(5, 0)));
80          System.out.println("project (6,0) orthogonal: " + polyLine2d.projectOrthogonal(new Point2d(6, 0)));
81          System.out.println("project (10,0) orthogonal: " + polyLine2d.projectOrthogonal(new Point2d(10, 0)));
82          System.out.println("project (50,0) orthogonal: " + polyLine2d.projectOrthogonal(new Point2d(50, 0)));
83          System.out.println("project (50,0) orthogonal extended: " + polyLine2d.projectOrthogonalExtended(new Point2d(50, 0)));
84  
85          LineSegment2d ls1 = new LineSegment2d(new Point2d(1, 2), new Point2d(5, 0));
86          System.out.println("ls1: " + ls1);
87          LineSegment2d ls2 = new LineSegment2d(new Point2d(1, 2), 5, 0);
88          System.out.println("ls2: " + ls2);
89          LineSegment2d ls3 = new LineSegment2d(1, 2, new Point2d(5, 0));
90          System.out.println("ls3: " + ls3);
91          LineSegment2d ls4 = new LineSegment2d(1, 2, 5, 0);
92          System.out.println("ls4: " + ls4);
93      }
94  }