# Cartesian to Spherical coordinates conversion issue

**URL:** <https://discourse.processing.org/t/cartesian-to-spherical-coordinates-conversion-issue/8174>\
**Category:** Coding Questions\
**Created:** [February 6, 2019, 8:23pm UTC](https://discourse.processing.org/t/cartesian-to-spherical-coordinates-conversion-issue/8174 "2019-02-06T20:23:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kesson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kesson/32/2286_2.png) [@kesson](https://discourse.processing.org/u/kesson)\
**Post date:** [February 6, 2019, 8:23pm UTC](https://discourse.processing.org/t/cartesian-to-spherical-coordinates-conversion-issue/8174/1 "2019-02-06T20:23:42Z")

</div>

Hi everyone,

I am trying to convert the cartesian coordinates a PVector in 3D space, with coordinates (x, y, z) in spherical coordinates (so obtaining radius, theta and phi).

Below is the code I am testing, rarely it “works”, I guess there is something wrong so something is just inverted.

```auto
import peasy.*;
PeasyCam cam;

PVector p;

void setup() {
  size(600, 600, P3D);
  
  cam = new PeasyCam(this, 500);
  
  // the vector with cartesian coordinates
  p = PVector.random3D();
  p.mult(100);
}

void draw() {
  background(0);
  
  // lines for the coordinate axis
  stroke(100, 100, 255);
  line(-width, 0, 0, width, 0, 0);
  
  stroke(100, 255, 100);
  line(0, 0, width, 0, 0, -width);
  
  stroke(255, 100, 100);
  line(0, -height, 0, 0, height, 0);
  
  // the cube with cartesian coordinates
  pushMatrix();
  translate(p.x, p.y, p.z);
  fill(255);
  stroke(255, 100, 100);
  noFill();
  box(50);
  popMatrix();
  
  //conversion into spherical coordinates
  float radius = sqrt(sq(p.x) + sq(p.y) + sq(p.z));
  float theta = atan(p.y/p.x);
  float phi = acos(p.z/radius);
  
  float x = radius * cos(theta) * sin(phi);
  float y = radius * sin(theta) * sin(phi);
  float z = radius * cos(phi);
    
  pushMatrix();
  translate(x, y, z);
  stroke(100, 255, 100);
  noFill();
  box(25);
  popMatrix();
}

```

 ![Capture3](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/801cc76d21af9e25a531e7246fb411d0a3f04ba1.png)

In the image, the red cube is the vector in cartesian coordinates, the green one the conversion in the code.

As a reference I am following the Spherical Coordinates page on Wolfram.

Where am I wrong?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 7, 2019, 1:52am UTC](https://discourse.processing.org/t/cartesian-to-spherical-coordinates-conversion-issue/8174/2 "2019-02-07T01:52:05Z")

</div>

i think there is a math problem,  
i rearranged your code so it is more easy to play again ( key == ‘a’ )  
and i see every time p.x - negative the result is wrong.  
else it works.

so i limit p.x by

> abs(p.x);

or use:

> theta = atan2(p.y,p.x);

good practice is just to print the data!

```auto
import peasy.*;
PeasyCam cam;

PVector p;
float radius, theta, phi, x, y, z;

void setup() {
  size(600, 600, P3D);
  cam = new PeasyCam(this, 500);
  make_vector(); // the vector with cartesian coordinates
}

void make_vector() {
  p = PVector.random3D();
  p.mult(100);
  // http://mathworld.wolfram.com/SphericalCoordinates.html
  // https://en.wikipedia.org/wiki/Spherical_coordinate_system
  // https://en.wikipedia.org/wiki/Atan2
  // p.x = abs(p.x); //kll way 1: limit x to first quadrant:
  radius = sqrt(sq(p.x) + sq(p.y) + sq(p.z)); //conversion into spherical coordinates

// theta = atan(p.y/p.x);
  theta = atan2(p.y,p.x);

  phi = acos(p.z/radius);
  x = radius * cos(theta) * sin(phi);
  y = radius * sin(theta) * sin(phi);
  z = radius * cos(phi);
  println(" p.x "+nf(p.x,1,1)+" p.y "+nf(p.y,1,1)+" p.z "+nf(p.z,1,1)+
          " radius "+nf(radius,1,1)+" theta "+nf(theta,1,1)+" phi "+nf(phi,1,1)+
          " x "+nf(x,1,1)+" y "+nf(y,1,1)+" z "+nf(z,1,1));
}

void draw_axis() {
  background(0);
  // lines for the coordinate axis
  stroke(100, 100, 255);
  line(-width, 0, 0, width, 0, 0);
  stroke(100, 255, 100);
  line(0, 0, width, 0, 0, -width);
  stroke(255, 100, 100);
  line(0, -height, 0, 0, height, 0);
}

void draw_qcart() { // the cube with cartesian coordinates
  pushMatrix();
  translate(p.x, p.y, p.z);
  fill(255);
  stroke(255, 100, 100);
  noFill();
  box(50);
  popMatrix();
}

void draw_qsphere() { // spherical coordinates
pushMatrix();
translate(x, y, z);
stroke(100, 255, 100);
noFill();
box(25);
popMatrix();
}

void draw() {
  background(0);
  draw_axis();
  draw_qcart();
  draw_qsphere();
}

void keyPressed() {
  if ( key == 'a' ) make_vector();
}
  

```

[http://mathworld.wolfram.com/SphericalCoordinates.html](http://mathworld.wolfram.com/SphericalCoordinates.html)

> **[Spherical coordinate system](https://en.wikipedia.org/wiki/Spherical_coordinate_system)**
>
> In mathematics, a spherical coordinate system is a coordinate system for three-dimensional space where the position of a given point in space is specified by three numbers, (r, θ, φ): the radial distance of the radial line r connecting the point to the fixed point of origin (which is located on a fixed polar axis, or zenith direction axis, or z-axis); the polar angle θ of the radial line r; and the azimuthal angle φ of the radial line r.
> The polar angle θ is measured between the z-axis and ...

> The inverse tangent denoted in _φ_ = arctan _y_ / _x_ must be suitably defined, taking into account the correct quadrant of ( _x_ , _y_ ).

> **[atan2](https://en.wikipedia.org/wiki/Atan2)**
>
> In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition, 
>   
>     
>       
> θ
> =
> atan2
> ⁡
> (
> y
> ,
> x
> )
>       
>     
> {\\displaystyle \\theta =\\operatorname {atan2} (y,x)}
>   
> is the angle measure (in radians, with 
>   
>     
>       
> −
> π
> \<
> θ
> ≤
> π
>       
>     
> {\\displaystyle -\\pi \<\\theta \\leq \\pi }
>   
> ) between the positive 
>   
>     
>       
> x
> ...

---

<div class="post-metadata">

**Author:** ![kesson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kesson/32/2286_2.png) [@kesson](https://discourse.processing.org/u/kesson)\
**Post date:** [February 7, 2019, 11:02am UTC](https://discourse.processing.org/t/cartesian-to-spherical-coordinates-conversion-issue/8174/3 "2019-02-07T11:02:43Z")

</div>

Great! Thanks @kll it worked!
