HP Forums

Full Version: (35S) Spherical coordinates <-> rectangular
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Find below two programs to convert from/to rectangular coordinates to/from spherical coordinates.

The variables of the calculator are preserved.
Only the stack is used.

Two programs are provided:
- rect2sph: XEQ S
- sph2rect: XEQ R

Cartesian coordinates are in the form (x, y, z)
Spherical coordinates are in the form (r, theta, phi),
where r is the radius/distance, theta is the azimuth, and phi is the elevation.

Formula used are:

x = r.cos(theta).cos(phi)
y = r.sin(theta).cos(phi)
z = r.sin(phi)

r = sqrt(x²+y²+z²)
theta = atan2(y, x)
phi = atan2(z, sqrt(x²+y²))

Usage for rect2sph:
Stack before:
Z: x
Y: y
X: z

Exec program: XEQ S

Stack after:
T: [x, y, z]
Z: r
Y: theta
X: phi

Usage for sph2rect:
Stack before:
Z: r
Y: theta
X: phi

Exec program: XEQ R

Stack after:
T: [r, theta, phi]
Z: x
Y: y
X: z


Code for rect2sph:
Code:

S001 LBL S
S002 [REGZ,REGY,REGX]
S003 ARG(REGT+REGZ*i)    CK=7FF4 LN=16
S004 x<>y
S005 ARG(ABS(REGX*[1,0,0]+REGT*i)+REGZ*i)    CK=D2C6 LN=36
S006 x<>y
S007 ENTER
S008 ABS
S009 Rdown
S010 Rdown
S011 RTN

CK=B926 LN=101

Code for sph2rect:
Code:

R001 LBL R
R002 [REGZ,REGY,REGX]
R003 Rup
R004 REGX*COS(REGT)*COS(REGZ)    CK=C8A2 LN=24
R005 x<>y
R006 REGX*SIN(REGZ*[0,1,0])*COS(REGT)    CK=7F3B LN=32
R007 x<>y
R008 Rdown
R009 REGT*SIN(REGZ*[0,0,1])    CK=33B2 LN=22
R010 RTN

CK=6852 LN=124
(05-05-2017 05:51 PM)stephane Wrote: [ -> ]Find below two programs to convert from/to rectangular coordinates to/from spherical coordinates.

The variables of the calculator are preserved.
Only the stack is used.

Two programs are provided:
- rect2sph: XEQ S
- sph2rect: XEQ R

Cartesian coordinates are in the form (x, y, z)
Spherical coordinates are in the form (r, theta, phi),
where r is the radius/distance, theta is the azimuth, and phi is the elevation.
.........................

[/code]
Well, finally something to try on my HP 35s. Could you put a numerical example, and perhaps a graphic illustration, to check that everything works well?
Thank you in advance, Pedro
To check that everything works well:

First, put your calculator in DEG mode, because the result depends on DEG or RAD.

Example for rect2sph(10, 20, 30):

Code:

10
ENTER
20
ENTER
30
XEQ S ENTER

should give the following values in the stack:
T: [10,20,30]
Z: 37.4166 (meters of distance)
Y: 63.4349 (degrees of azimuth)
X: 53.3008 (degrees of elevation)


Example for sph2rect(5, 30°, 45°):

Code:

5
ENTER
30
ENTER
45
XEQ R ENTER

should give the following values in the stack:
T: [5,30,45]
Z: 3.0619 (x value in meters)
Y: 1.7678 (y value in meters)
X: 3.5355 (z value in meters)

Then, pressing XEQ S ENTER gives you again the initial spherical coordinates.
Thank you stepahne, Pedro
Reference URL's