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-2024 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/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
12 * project is distributed under a three-clause BSD-style license, which can be found at
13 * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
14 * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
15 * </p>
16 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
17 */
18 public class CartesianPoint extends Point3d
19 {
20 /** */
21 private static final long serialVersionUID = 20191116L;
22
23 /**
24 * constructs a new CartesianPoint.
25 * @param x double; x
26 * @param y double; y
27 * @param z double; z
28 */
29 public CartesianPoint(final double x, final double y, final double z)
30 {
31 super(x, y, z);
32 }
33
34 /**
35 * constructs a new CartesianPoint.
36 * @param xyz double[]; x,y,z
37 */
38 public CartesianPoint(final double[] xyz)
39 {
40 super(xyz);
41 }
42
43 /**
44 * constructs a new CartesianPoint.
45 * @param point2D Point2D; a 2D point
46 */
47 public CartesianPoint(final Point2D point2D)
48 {
49 this(point2D.getX(), point2D.getY(), 0);
50 }
51
52 /**
53 * returns the 2D representation of the point.
54 * @return Point2D the result
55 */
56 public Point2D to2D()
57 {
58 return new Point2D.Double(this.getX(), this.getY());
59 }
60
61 /**
62 * converts the point to a sperical point.
63 * @return the spherical point
64 */
65 public SphericalPoint toSphericalPoint()
66 {
67 return CartesianPoint.toSphericalPoint(this);
68 }
69
70 /**
71 * converts a cartesian point to a sperical point. See https://mathworld.wolfram.com/SphericalCoordinates.html
72 * @param point CartesianPoint; the cartesian point
73 * @return the spherical point
74 */
75 public static SphericalPoint toSphericalPoint(final CartesianPoint point)
76 {
77 double rho = Math.sqrt(Math.pow(point.getX(), 2) + Math.pow(point.getY(), 2) + Math.pow(point.getZ(), 2));
78 double theta = Math.atan2(point.getY(), point.getX());
79 double phi = Math.acos(point.getZ() / rho);
80 return new SphericalPoint(rho, theta, phi);
81 }
82
83 /** {@inheritDoc} */
84 @Override
85 public String toString()
86 {
87 return "CartesianPoint [x=" + this.getX() + ", y=" + this.getY() + ", z=" + this.getZ() + "]";
88 }
89
90 }