1 package JSci.maths; 2 3 10 public final class CoordinateMath extends AbstractMath { 11 private CoordinateMath() {} 12 13 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 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 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 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 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 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 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 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 |