HP Forums
Coordinate conversions in 3D (rectangular, cylindrical and spherical) - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Coordinate conversions in 3D (rectangular, cylindrical and spherical) (/thread-7005.html)



Coordinate conversions in 3D (rectangular, cylindrical and spherical) - JMB - 10-09-2016 12:42 PM

I present here six short programs to convert coordinates in three dimensions, among the three coordinate systems: rectangular, cylindrical and spherical.

To accomplish this task, I use the Prime functions polar_coordinates and rectangular_coordinates. These functions automatically handle special cases (for example: ATAN(y/x) when x, y or both are zero), and place the angles in the correct quadrant.

All these functions use the angular mode that is active in the calculator at the moment when they are executed.

The names and conventions used for the rectangular, cylindrical and spherical coordinate systems, can be seen in the following image:

[attachment=4106]

PHP Code:
EXPORT RecToCyl(rec)
// [x,y,z] → [ρ,φ,z]
BEGIN
  LOCAL cyl
:=polar_coordinates(rec(1),rec(2));
  [
cyl(1),cyl(2),rec(3)];
END;

EXPORT CylToRec(cyl)
// [ρ,φ,z] → [x,y,z]
BEGIN
  LOCAL rec
:=rectangular_coordinates(cyl(1),cyl(2));
  [
rec(1),rec(2),cyl(3)];
END;
  
EXPORT SphToCyl(sph)
// [r,θ,φ] → [ρ,φ,z]
BEGIN
  LOCAL cyl
:=rectangular_coordinates(sph(1),sph(2));
  [
cyl(2),sph(3),cyl(1)];
END;
 
EXPORT CylToSph(cyl)
// [ρ,φ,z] → [r,θ,φ]
BEGIN
  LOCAL sph
:=polar_coordinates(cyl(3),cyl(1));
  [
sph(1),sph(2),cyl(2)];
END
  
EXPORT RecToSph(rec)
// [x,y,z] → [r,θ,φ]
BEGIN
  CylToSph
(RecToCyl(rec));
END;

EXPORT SphToRec(sph)
// [r,θ,φ] → [x,y,z]
BEGIN
  CylToRec
(SphToCyl(sph));
END