Post Reply 
Coordinate conversions in 3D (rectangular, cylindrical and spherical)
10-09-2016, 12:42 PM (This post was last modified: 11-06-2016 11:41 AM by JMB.)
Post: #1
Coordinate conversions in 3D (rectangular, cylindrical and spherical)
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:

   

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


Attached File(s)
.hpprgm  CC3D.hpprgm (Size: 3.29 KB / Downloads: 32)
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)