View Javadoc
1   package demo;
2   
3   import org.djutils.draw.Transform2d;
4   import org.djutils.draw.Transform3d;
5   import org.djutils.draw.line.PolyLine2d;
6   import org.djutils.draw.point.Point2d;
7   import org.djutils.draw.point.Point3d;
8   
9   /**
10   * TransformDemos.java.
11   * <p>
12   * Copyright (c) 2021-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
14   * </p>
15   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   */
18  public final class TransformDemos
19  {
20      /**
21       * Do not instantiate.
22       */
23      private TransformDemos()
24      {
25          // Do not instantiate
26      }
27  
28      /**
29       * Demonstrate the transformations.
30       * @param args String...; the command line arguments (not used)
31       */
32      public static void main(final String... args)
33      {
34          translateAndRotate();
35          shear2d();
36          shear3d();
37          scale();
38          transformPolyLine();
39      }
40      
41      /**
42       * Demonstrate translate and rotate.
43       */
44      public static void translateAndRotate()
45      {
46          System.out.println("Translate and rotate");
47          Point3d point = new Point3d(1, 0, 0);
48          System.out.println(point);
49          Transform3d transform1 = new Transform3d();
50          transform1.translate(2, 5, 8); // Translate
51          System.out.println(transform1.transform(point));
52          transform1.rotY(Math.PI / 2);
53          System.out.println(transform1.transform(point));
54  
55          Transform3d transform2 = new Transform3d().translate(2, 5, 8).rotY(Math.PI / 2);
56          System.out.println(transform2.transform(point));
57      }
58      
59      /**
60       * Demonstrate 2D shear.
61       */
62      public static void shear2d()
63      {
64          System.out.println("2D shear");
65          Transform2d transform = new Transform2d().shear(2, 3);
66          for (int x = 0; x < 2; x++)
67          {
68              for (int y = 0; y < 2; y++)
69              {
70                  Point2d p = new Point2d(x, y);
71                  System.out.println(p + " -> " + transform.transform(p));
72              }
73          }
74      }
75      
76      /**
77       * Demonstrate 3d shear.
78       */
79      public static void shear3d()
80      {
81          System.out.println("3D shear");
82          Transform3d transform = new Transform3d().shearXY(2, 3);
83          for (int x = 0; x < 2; x++)
84          {
85              for (int y = 0; y < 2; y++)
86              {
87                  for (int z = 0; z < 2; z++)
88                  {
89                      Point3d p = new Point3d(x, y, z);
90                      System.out.println(p + " -> " + transform.transform(p));
91                  }
92              }
93          }
94      }
95      
96      /**
97       * Demonstrate scale.
98       */
99      public static void scale()
100     {
101         System.out.println("Scaling");
102         Transform2d transform = new Transform2d().scale(4, 6);
103         for (int x = 0; x < 2; x++)
104         {
105             for (int y = 0; y < 2; y++)
106             {
107                 Point2d p = new Point2d(x, y);
108                 System.out.println(p + " -> " + transform.transform(p));
109             }
110         }
111     }
112     
113     /**
114      * Demonstrate transforming of iterator of Point.
115      */
116     public static void transformPolyLine()
117     {
118         System.out.println("Transforming a multi-point Drawable");
119         PolyLine2d line = new PolyLine2d(new Point2d(1, 2), new Point2d(2, 3), new Point2d(5, 0));
120         System.out.println(line);
121         Transform2d transform = new Transform2d().scale(2, 3);
122         PolyLine2d transformedLine = new PolyLine2d(transform.transform(line.getPoints()));
123         System.out.println(transformedLine);
124     }
125 }