/* coordinate.c sample coordinate conversion code, tailor to your needs */ /* cartesian coordinates are x,y x,y,z x,y,z,t */ /* */ /* polar radius r, angle from x axis theta */ /* x = r * cos(theta) r = sqrt(x*x+y*y) */ /* y = r * sin(theta) theta = arctan(y/x) or atan2 */ /* */ /* theta in 0 to 2Pi need theta over complete range */ /* r positive any real x,y */ /* */ /* area = Pi*r*r */ /* */ /* cylindrical radius r, angle theta and height z */ /* x = r * cos(theta) r = sqrt(x*x+y*y) */ /* y = r * sin(theta) theta = arctan(y/x) or atan2 */ /* z = z z = z */ /* */ /* theta in 0 to 2Pi need theta over complete range */ /* r positive any real x,y */ /* */ /* area = 2*Pi*r*z + 2*Pi*r*r */ /* volume = Pi*r*r*z */ /* */ /* spherical radius r, angle theta in x,y plane from positive x axis */ /* angle phi from positive z axis */ /* x = r * sin(phi) * cos(theta) r = sqrt(x*x+y*y+z*z) */ /* y = r * sin(phi) * sin(theta) theta = arctan(y/x) */ /* z = r * cos(phi) phi = arctan(sqrt(x*x+y*y)/z) */ /* */ /* phi in 0 to Pi need phi over complete range */ /* theta in 0 to 2Pi need theta over complete range */ /* r positive any real x,y,z */ /* */ /* area = 4*Pi*r*r */ /* volume = 4/3 Pi*r*r*r */ /* */ /* 4D spherical radius r, angle theta in x,y plane from positive x axis */ /* angle phi from positive z axis */ /* angle psi from positive t axis */ /* x = r * sin(psi) * sin(phi) * cos(theta) */ /* r = sqrt(x*x+y*y+z*z+t*t) */ /* y = r * sin(psi) * sin(phi) * sin(theta) */ /* theta = arctan(y/x) */ /* z = r * sin(psi) * cos(phi) */ /* phi = arctan(sqrt(x*x+y*y)/z) */ /* t = r * cos(psi) */ /* psi = arctan(sqrt(x*x+y*y+z*z)/t) */ /* */ /* psi in 0 to Pi need psi over complete range */ /* phi in 0 to Pi need phi over complete range */ /* theta in 0 to 2Pi need theta over complete range */ /* r positive any real x,y,z,t */ /* */ /* area = 8*Pi*r*r */ /* volume = 2 Pi*Pi*r*r*r */ /* 4D volume = 1/2 Pi*Pi*r*r*r*r */ /* */ /* */ /* all angles are in radians, convert radians a to degrees d */ /* d = a * 180.0/Pi */ /* but, you may want the range in degrees to be different */ /* */ /* convert degrees d to radians a */ /* a = d * Pi/180.0 */ #include #include #define Pi 3.14159265358979323846 void cartesian_to_polar(double x, double y, double *r, double *theta) { *r = sqrt(x*x+y*y); *theta = atan2(y, x); } void polar_to_cartesian(double r, double theta, double *x, double *y) { *x = r * cos(theta); *y = r * sin(theta); } void cartesian_to_cylindrical(double x, double y, double z, double *r, double *theta, double *zs) { *r = sqrt(x*x+y*y); *theta = atan2(y, x); *zs = z; } void cylindrical_to_cartesian(double r, double theta, double zs, double *x, double *y, double *z) { *x = r * cos(theta); *y = r * sin(theta); *z = zs; } void cartesian_to_spherical(double x, double y, double z, double *r, double *theta, double *phi) { *r = sqrt(x*x+y*y+z*z); *theta = atan2(y, x); *phi = atan2(sqrt(x*x+y*y),z); } void spherical_to_cartesian(double r, double theta, double phi, double *x, double *y, double *z) { *x = r * sin(phi) * cos(theta); *y = r * sin(phi) * sin(theta); *z = r * cos(phi); } void cartesian4D_to_spherical(double x, double y, double z, double t, double *r, double *theta, double *phi, double *psi) { *r = sqrt(x*x+y*y+z*z+t*t); *theta = atan2(y, x); *phi = atan2(sqrt(x*x+y*y),z); *psi = atan2(sqrt(x*x+y*y+z*z),t); } void spherical_to_cartesian4D(double r, double theta, double phi, double psi, double *x, double *y, double *z, double *t) { *x = r * sin(psi) * sin(phi) * cos(theta); *y = r * sin(psi) * sin(phi) * sin(theta); *z = r * sin(psi) * cos(phi); *t = r * cos(psi); } double to_theta_degrees(double theta) { /* latitude positive 0 to 360 */ if(theta>0.0) return theta * 180.0/Pi; else return 360.0 + theta * 180.0/Pi; } double to_phi_degrees(double phi) { /* longitude -90 to +90 degrees */ if(phi