Skip to content

Instantly share code, notes, and snippets.

@Anticom
Last active December 17, 2015 18:49
Show Gist options
  • Save Anticom/5655444 to your computer and use it in GitHub Desktop.
Save Anticom/5655444 to your computer and use it in GitHub Desktop.
Java convert karthesian coordinate to spherical back and forth failure
package javax.vecmath;
import com.sun.corba.se.impl.interceptors.PICurrent;
public class Point3d_karthesian extends Point3d {
public Point3d_karthesian() {
super();
}
public Point3d_karthesian(double[] arg0) {
super(arg0);
}
public Point3d_karthesian(Point3d arg0) {
super(arg0);
}
public Point3d_karthesian(Point3f arg0) {
super(arg0);
}
public Point3d_karthesian(Tuple3d arg0) {
super(arg0);
}
public Point3d_karthesian(Tuple3f arg0) {
super(arg0);
}
public Point3d_karthesian(double arg0, double arg1, double arg2) {
super(arg0, arg1, arg2);
}
public double getOffset() {
return z;
}
/**
* Convert to radius,phi,theta point
*
* @see https://de.wikipedia.org/wiki/Kugelkoordinaten#.C3.9Cbliche_Konvention
* @return Point3d_spherical
*/
public Point3d_spherical toSpherePoint() {
double radius, phi, theta;
double z = this.z;
radius = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
/*
if(x > 0) {
phi = Math.atan((y / x));
} else if(x == 0) {
phi = Math.signum(y) * (Math.PI / 2);
} else if(x < 0 && y >= 0) {
phi = Math.atan((y / x)) + Math.PI;
} else if(x < 0 && y < 0) {
phi = Math.atan((y / x)) - Math.PI;
} else {
phi = 0;
System.err.println("Huston we got a problem!");
}
*/
phi = Math.atan2(y, x);
if(radius != 0) {
theta = Math.acos((z / radius));
} else {
theta = 0d;
//theta = Math.acos(z);
}
return new Point3d_spherical(radius, phi, theta);
}
}
package javax.vecmath;
public class Point3d_spherical {
protected double radius,
phi,
theta;
public Point3d_spherical() {
}
public Point3d_spherical(double radius, double phi, double theta) {
this.radius = radius;
this.phi = phi;
this.theta = theta;
}
public String toString() {
return "(" + radius + ", " + phi + ", " + theta + ")";
}
/**
* Convert to x,y,z point
*
* @see https://de.wikipedia.org/wiki/Kugelkoordinaten#.C3.9Cbliche_Konvention
* @return Point3d_karthesian
*/
public Point3d_karthesian toPoint3d() {
double x, y, z;
x = radius * Math.sin(theta) * Math.cos(phi);
y = radius * Math.sin(theta) * Math.sin(phi);
z = radius * Math.cos(theta);
return new Point3d_karthesian(x, y, z);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getPhi() {
return phi;
}
public void setPhi(double phi) {
this.phi = phi;
}
public double getTheta() {
return theta;
}
public void setTheta(double theta) {
this.theta = theta;
}
}
import javax.vecmath.Point3d_karthesian;
import javax.vecmath.Point3d_spherical;
public class Runner {
/**
* @param args
*/
public static void main(String[] args) {
new Runner();
}
public Runner() {
Point3d_karthesian point = new Point3d_karthesian(10, 0, 0);
Point3d_spherical sphere = point.toSpherePoint();
Point3d_karthesian reverse = sphere.toPoint3d();
System.out.printf("Original point : %s\n", point);
System.out.printf("Sphere point : %s\n", sphere);
System.out.printf("Reverse point : %s\n", reverse);
}
}
Original point : (10.0, 0.0, 0.0)
Sphere point : (10.0, 0.0, 1.5707963267948966)
Reverse point : (10.0, 0.0, 6.123233995736766E-16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment