View Javadoc
1   package org.djutils.draw;
2   
3   import java.util.Locale;
4   
5   import org.djutils.draw.line.LineSegment2d;
6   import org.djutils.draw.line.LineSegment3d;
7   import org.djutils.draw.line.PolyLine2d;
8   import org.djutils.draw.line.PolyLine3d;
9   import org.djutils.draw.line.Polygon2d;
10  import org.djutils.draw.line.Polygon3d;
11  
12  /**
13   * Export.java.
14   * <p>
15   * Copyright (c) 2023-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
17   * distributed under a three-clause BSD-style license, which can be found at
18   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
19   * </p>
20   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   */
22  public final class Export
23  {
24      /**
25       * Utility class; do not instantiate.
26       */
27      private Export()
28      {
29          // Do not instantiate
30      }
31  
32      /**
33       * Convert a LineSegment2d to Peter's plot format.
34       * @param lineSegment LineSegment2d; the line segment to convert
35       * @return String; the line segment in Peter's plot format
36       */
37      public static String toPlot(final LineSegment2d lineSegment)
38      {
39          return String.format(Locale.US, "M%.3f,%.3fL%.3f,%.3f\n", lineSegment.startX, lineSegment.startY, lineSegment.endX,
40                  lineSegment.endY);
41      }
42  
43      /**
44       * Convert a PolyLine2d to Peter's plot format.
45       * @param polyLine PolyLine2d; the polyline to convert
46       * @return String; the polyline in Peter's plot format
47       */
48      public static String toPlot(final PolyLine2d polyLine)
49      {
50          StringBuffer result = new StringBuffer();
51          for (int i = 0; i < polyLine.size(); i++)
52          {
53              result.append(String.format(Locale.US, "%s%.3f,%.3f", 0 == result.length() ? "M" : " L", polyLine.getX(i),
54                      polyLine.getY(i)));
55          }
56          result.append("\n");
57          return result.toString();
58      }
59  
60      /**
61       * Convert a Polygon2d into Peter's plot format.
62       * @param polygon Polygon2d; the polygon to convert
63       * @return String; the polygon in Peter's plot format
64       */
65      public static String toPlot(final Polygon2d polygon)
66      {
67          StringBuffer result = new StringBuffer();
68          for (int i = 0; i < polygon.size(); i++)
69          {
70              result.append(String.format(Locale.US, "%s%.3f,%.3f", 0 == result.length() ? "M" : " L", polygon.getX(i),
71                      polygon.getY(i)));
72          }
73          result.append(String.format(Locale.US, " L%.3f,%.3f", polygon.getX(0), polygon.getY(0)));
74          result.append("\n");
75          return result.toString();
76      }
77  
78      /**
79       * Convert a LineSegment2d into something that a TSV parser can handle.
80       * @param lineSegment LineSegment2d; the line segment to convert
81       * @return String; the line segment in TSV format
82       */
83      public static String toTsv(final LineSegment2d lineSegment)
84      {
85          return lineSegment.startX + "\t" + lineSegment.startY + "\n" + lineSegment.endX + "\t" + lineSegment.endY + "\n";
86      }
87  
88      /**
89       * Convert a PolyLine2d into something that a TSV parser can handle.
90       * @param polyLine PolyLine2d; the polyline to convert
91       * @return String; the polyline in TSV format
92       */
93      public static String toTsv(final PolyLine2d polyLine)
94      {
95          StringBuffer s = new StringBuffer();
96          for (int i = 0; i < polyLine.size(); i++)
97          {
98              s.append(polyLine.getX(i) + "\t" + polyLine.getY(i) + "\n");
99          }
100         return s.toString();
101     }
102 
103     /**
104      * Convert a Polygon2d into something that a TSV parser can handle.
105      * @param polygon Polygon2d; the polygon to convert
106      * @return String; the polygon in TSV format
107      */
108     public static String toTsv(final Polygon2d polygon)
109     {
110         StringBuffer s = new StringBuffer();
111         for (int i = 0; i < polygon.size(); i++)
112         {
113             s.append(polygon.getX(i) + "\t" + polygon.getY(i) + "\n");
114         }
115         s.append(polygon.getX(0) + "\t" + polygon.getY(0) + "\n");
116         return s.toString();
117     }
118 
119     /**
120      * Convert a LineSegment3d into something that a TSV parser can handle.
121      * @param lineSegment LineSegment3d; the line segment to convert
122      * @return String; the line segment in TSV format
123      */
124     public static String toTsv(final LineSegment3d lineSegment)
125     {
126         return lineSegment.startX + "\t" + lineSegment.startY + "\t" + lineSegment.startZ + "\n" + lineSegment.endX + "\t"
127                 + lineSegment.endY + "\t" + lineSegment.endZ + "\n";
128     }
129 
130     /**
131      * Convert a PolyLine3d into something that a TSV parser can handle.
132      * @param polyline PolyLine3d; the polyline to convert
133      * @return String; the polyline in TSV format
134      */
135     public static String toTsv(final PolyLine3d polyline)
136     {
137         StringBuffer s = new StringBuffer();
138         for (int i = 0; i < polyline.size(); i++)
139         {
140             s.append(polyline.getX(i) + "\t" + polyline.getY(i) + "\t" + polyline.getZ(i) + "\n");
141         }
142         return s.toString();
143     }
144 
145     /**
146      * Convert a Polygon3d into something that a TSV parser can handle.
147      * @param polygon Polygon3d; the polygon to convert
148      * @return String; the polygon in TSV format
149      */
150     public static String toTsv(final Polygon3d polygon)
151     {
152         StringBuffer s = new StringBuffer();
153         for (int i = 0; i < polygon.size(); i++)
154         {
155             s.append(polygon.getX(i) + "\t" + polygon.getY(i) + "\t" + polygon.getZ(i) + "\n");
156         }
157         s.append(polygon.getX(0) + "\t" + polygon.getY(0) + "\t" + polygon.getZ(0) + "\n");
158         return s.toString();
159     }
160 
161 }