1 package nl.tudelft.simulation.language.d3;
2
3 import java.awt.geom.Point2D;
4
5 import org.djutils.draw.point.Point3d;
6
7 /**
8 * The Point3D class with utilities to convert to point2D where the z-axis is neglected.
9 * <p>
10 * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
11 * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
12 * project is distributed under a three-clause BSD-style license, which can be found at
13 * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
14 * </p>
15 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
16 */
17 public class CartesianPoint extends Point3d
18 {
19 /**
20 * constructs a new CartesianPoint.
21 * @param x x
22 * @param y y
23 * @param z z
24 */
25 public CartesianPoint(final double x, final double y, final double z)
26 {
27 super(x, y, z);
28 }
29
30 /**
31 * constructs a new CartesianPoint.
32 * @param xyz x,y,z
33 */
34 public CartesianPoint(final double[] xyz)
35 {
36 super(xyz);
37 }
38
39 /**
40 * constructs a new CartesianPoint.
41 * @param point2D a 2D point
42 */
43 public CartesianPoint(final Point2D point2D)
44 {
45 this(point2D.getX(), point2D.getY(), 0);
46 }
47
48 /**
49 * returns the 2D representation of the point.
50 * @return Point2D the result
51 */
52 public Point2D to2D()
53 {
54 return new Point2D.Double(this.getX(), this.getY());
55 }
56
57 /**
58 * converts the point to a sperical point.
59 * @return the spherical point
60 */
61 public SphericalPoint toSphericalPoint()
62 {
63 return CartesianPoint.toSphericalPoint(this);
64 }
65
66 /**
67 * converts a cartesian point to a sperical point. See https://mathworld.wolfram.com/SphericalCoordinates.html
68 * @param point the cartesian point
69 * @return the spherical point
70 */
71 public static SphericalPoint toSphericalPoint(final CartesianPoint point)
72 {
73 double rho = Math.sqrt(Math.pow(point.getX(), 2) + Math.pow(point.getY(), 2) + Math.pow(point.getZ(), 2));
74 double theta = Math.atan2(point.getY(), point.getX());
75 double phi = Math.acos(point.getZ() / rho);
76 return new SphericalPoint(rho, theta, phi);
77 }
78
79 @Override
80 public String toString()
81 {
82 return "CartesianPoint [x=" + this.getX() + ", y=" + this.getY() + ", z=" + this.getZ() + "]";
83 }
84
85 }