I work with the Oracle JGeometry method which returns (or takes as method argument) 2D segments as double array (x1,y1,x2,y2 ... xn,yn). I need to extend this array to contain 3D segments as double array (x1,y1,z1,x2,y2,z2 ... xn,yn,zn) by default Z value or reduce to 2D (by removing of all Z coordinate). I wrote simple utility methods for making this. Is there any easier or smarter way to do this?
The conversion from 2D to 3D:
public static double[] to3D(double z, double[] inputArray) {
List<Double> convertedItems = new ArrayList<>();
for (int i = 0; i < inputArray.length; i++) {
convertedItems.add(inputArray[i]);
if ((i + 1) % 2 == 0) {
convertedItems.add(z);
}
}
return convertedItems.stream().mapToDouble(Double::doubleValue).toArray();
}
The conversion from 3D to 2D:
public static double[] to2D(double[] inputArray) {
List<Double> convertedItems = new ArrayList<>();
for (int i = 0; i < inputArray.length; i++) {
if ((i + 1) % 3 == 0) {
continue;
}
convertedItems.add(inputArray[i]);
}
return convertedItems.stream().mapToDouble(Double::doubleValue).toArray();
}