KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > CoordinateMath


1 package JSci.maths;
2
3 /**
4 * The coordinate transformation math library.
5 * Provides common coordinate tranformations.
6 * This class cannot be subclassed or instantiated because all methods are static.
7 * @version 1.1
8 * @author Mark Hale
9 */

10 public final class CoordinateMath extends AbstractMath {
11         private CoordinateMath() {}
12
13         /**
14         * Converts cartesian coordinates to polar coordinates.
15         * @return an array with [0] containing the radius and [1] containing the angle.
16         */

17         public static double[] cartesianToPolar(double x,double y) {
18                 final double rtheta[]=new double[2];
19                 final double xAbs=Math.abs(x);
20                 final double yAbs=Math.abs(y);
21                 if(xAbs==0.0 && yAbs==0.0)
22                         rtheta[0]=0.0;
23                 else if(xAbs<yAbs)
24                         rtheta[0]=yAbs*Math.sqrt(1.0+(x/y)*(x/y));
25                 else
26                         rtheta[0]=xAbs*Math.sqrt(1.0+(y/x)*(y/x));
27                 rtheta[1]=Math.atan2(y,x);
28                 return rtheta;
29         }
30         /**
31         * Converts polar coordinates to cartesian coordinates.
32         * @return an array with [0] containing the x-coordinate and [1] containing the y-coordinate.
33         */

34         public static double[] polarToCartesian(double r,double theta) {
35                 final double xy[]=new double[2];
36                 xy[0]=r*Math.cos(theta);
37                 xy[1]=r*Math.sin(theta);
38                 return xy;
39         }
40         /**
41         * Converts 3D cartesian coordinates to spherical polar coordinates.
42         * @return an array with [0] containing the radius, [1] containing the theta angle and [2] containing the phi angle.
43         */

44         public static double[] cartesianToSpherical(double x,double y,double z) {
45                 final double rthetaphi[]=new double[3];
46                 rthetaphi[0]=Math.sqrt(x*x+y*y+z*z);
47                 rthetaphi[1]=Math.acos(z/rthetaphi[0]);
48                 rthetaphi[2]=Math.atan2(y,x);
49                 return rthetaphi;
50         }
51         /**
52         * Converts spherical polar coordinates to 3D cartesian coordinates.
53         * @return an array with [0] containing the x-coordinate, [1] containing the y-coordinate and [2] containing the z-coordinate.
54         */

55         public static double[] sphericalToCartesian(double r,double theta,double phi) {
56                 final double xyz[]=new double[3];
57                 xyz[0]=r*Math.sin(theta)*Math.cos(phi);
58                 xyz[1]=r*Math.sin(theta)*Math.sin(phi);
59                 xyz[2]=r*Math.cos(theta);
60                 return xyz;
61         }
62         /**
63         * Converts 3D cartesian coordinates to cylindrical coordinates.
64         * @return an array with [0] containing the radius, [1] containing the angle and [2] containing the height.
65         */

66         public static double[] cartesianToCylindrical(double x,double y,double z) {
67                 final double rphih[]=new double[3];
68                 final double rphi[]=cartesianToPolar(x,y);
69                 rphih[0]=rphi[0];
70                 rphih[1]=rphi[1];
71                 rphih[2]=z;
72                 return rphih;
73         }
74         /**
75         * Converts cylindrical coordinates to cartesian coordinates.
76         * @return an array with [0] containing the x-coordinate, [1] containing the y-coordinate and [2] containing the z-coordinate.
77         */

78         public static double[] cylindricalToCartesian(double r,double phi,double h) {
79                 final double xyz[]=new double[3];
80                 final double xy[]=polarToCartesian(r,phi);
81                 xyz[0]=xy[0];
82                 xyz[1]=xy[1];
83                 xyz[2]=h;
84                 return xyz;
85         }
86         /**
87         * Converts cylindrical coordinates to spherical polar coordinates.
88         * @return an array with [0] containing the radius, [1] containing the theta angle and [2] containing the phi angle.
89         */

90         public static double[] cylindricalToSpherical(double r,double phi,double h) {
91                 final double rthetaphi[]=new double[3];
92                 final double rtheta[]=cartesianToPolar(h,r);
93                 rthetaphi[0]=rtheta[0];
94                 rthetaphi[1]=rtheta[1];
95                 rthetaphi[2]=phi;
96                 return rthetaphi;
97         }
98         /**
99         * Converts spherical coordinates to cylindrical coordinates.
100         * @return an array with [0] containing the radius, [1] containing the angle and [2] containing the height.
101         */

102         public static double[] sphericalToCylindrical(double r,double theta,double phi) {
103                 final double rphih[]=new double[3];
104                 final double hr[]=polarToCartesian(r,theta);
105                 rphih[0]=hr[1];
106                 rphih[1]=phi;
107                 rphih[2]=hr[0];
108                 return rphih;
109         }
110 }
111
112
Popular Tags