# Converting spherical coordinates to cartesian in rotated space

#### Bozzle

Hello,

I am working on a program that draws lines by converting spherical coordinates into cartesian coordinates. I use the following equations to calculate the cartesian coordinates of a point based on its distance, azimuthal angle, and polar angle from another point.

The y axis is vertical, x and z are horizontal.

x2=x1+distance*sin(azimuth)*cos(polar)
y2=y1+distance*cos(azimuth)
z2=z1+distance*sin(azimuth)*sin(polar)

What I would like to be able to do is effectively rotate the 3 dimensional space by a given azimuth and polar angle, and use a similar set of equations to calculate the cartesian coordinates in the original x,y,z space, of a point based on its distance, azimuthal angle, and polar angle, relative to the rotated space, to another point.

So there would be 5 known variables:

-the azimuthal angle of rotation of the 3d space
-the polar angle of rotation of the 3d space
-the distance from point 1 to point 2
-the azimuth angle (in the rotated space) from point 1 to point 2
-the polar angle (in the rotated space) from point 1 to point 2

Of course we would also know the coordinates of the starting point

Here is a picture to help clarify.

The yellow lines represent the original axes, the blue lines represent the axes after rotation, the pink line is the line between point 1 and point 2.

In this case, the space has been rotated 30 degrees polar and 60 degrees azimuth. Point 2 lies a distance of 1, an azimuth angle of 45 degrees, and a polar angle of 45 degrees, relative to the rotated space, from point 1(in this case point 1 is the origin).

So how can I find the cartesian coordinates, in the un-rotated space, of point 2 with this information?

#### Attachments

• 25.3 KB Views: 4
Last edited:

#### chiro

MHF Helper
Hey Bozzle.

If you want to rotate the space (or the vectors - either way it has the same effect) you need to use what's called a rotation matrix.

https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle

The above gives the general formulation for a rotation axis an an angle.

If you want to go from one space to another then you need to what is called a change of basis.

Basically if you want the basis to preserve distances of vectors then you use only rotation and translation matrices.

Lets say you wanted to rotate the space and get the matrix for that space where if you calculated v' = Rv then it would represent the vector in the basis represented by r and v is in the universal basis. Note that if R = I (identity then you get the same vector).

If you have an original basis matrix represented by B and you wanted to rotate that basis around the origin then you find NB = R*B where NB is the new basis matrix, R is the rotation and B is the original basis.

If you want to rotate about a point (not the origin) then you have to calculate SRT where T translates by the negative vector being rotated around, R is the rotation, and S translates it by the positive vector being rotated (S translates the negative value that T does and vice-versa).

If no co-ordinate axes are scaled or sheared then they are the only matrices you will need.

Now if you don't have the values and are looking to solve for them, then you will need to expand out the definitions for translation and rotation, do some algebra and then solve for the unknowns.

Also if you know the original and final vectors then you can find the angles between them using inner products <x,y> where <x,y>/ [||x||*||y||] will give the cosine of the angle.

If you know the start vector and the rotated vector then you can get the axis and angle of rotation easily.

You simply take the cross product of two vectors to get the axis of rotation and then you use the formula above to get the cosine of the angle and use arc-cosine to get the angle in radians. Then you plug these in to the axes angle rotation matrix and that gives you information about the rotated space.

1 person

#### studiot

x2=x1+distance*sin(azimuth)*cos(polar)
y2=y1+distance*cos(azimuth)
z2=z1+distance*sin(azimuth)*sin(polar)
I must confess to being unsure what you are trying to do or even if you are starting with the correct formule.

The polar angle and azimuth refer to the origin of coordinates and indirectly to the x, y and z axes.

Are you thinking of setting up new spherical coordinates based on the point P as the new origin?
A sort of chaining of points.
The azimuth, polar angle and radius will be different measured from here comapred to the original system.

Your equations reflect the correct transformations from spherical polar coordinates to cartesian so why don't you work entirely in spherical polar until the last point and then transform to cartesian?

Are you performing calculus on the original sphere and do you want the vector form of the equations to suit?

1 person

#### Bozzle

Hey guys, thanks for the replies,

It was difficult for me to understand the answers given but they did point me in the right direction (I basically got my solution from the wiki page on rotation matrices that chiro linked me to) and now I have a solution to my problem, so I would say I am satisfied. And I'm sorry that its not clear what I'm doing. Fact is, I really don't have the mathematical knowledge or vocabulary to accurately verbalize what I'm doing or interpret the answers given.

I'll try to describe it a little better:

I'm working on a software plugin that generates 3D models of trees. The process involves creating chains of points that form lines in 3d space (y axis is vertical), and each point must be defined by its cartesian coordinates. The direction and/or distance between each point may or may not need to be different, and I use spherical coordinates to define these directions/distances.

At certain times, I need to create sets of lines that branch out from an already existing line (think of whorls of branches or needles on a pine tree) at angles relative to the existing line. In these situations it seems to me to be drastically simpler to describe the angles of the new lines as relative to a redefined 3d space that has been rotated to match the direction of the existing line.

So here is the way I'm doing it:

Say I have a line between two points, where p1 = (1,1,1) and p2 lies at a distance of 3, an azimuthal angle of 0.9 radians, and a polar angle of 2 radians from p1. Then:

xp2=xp1+3*sin(0.9)*cos(2.0)
yp2=yp1+3*cos(0.9)
zp2=zp1+3*sin(0.9)*sin(2.0)

Now lets say I want to make a new point whose spherical coordinates from p1 are relative to a space that has been rotated in such a way that the direction of the vector between p1 and p2 represents the new vertical axis.

Lets say this new point's spherical coordinates in the new space will be (2 (distance), 1.2 (azi), 0.0 (pol)). I first rotate, about the y axis, the vector formed with these coordinates by plugging them into the above equations with the polar rotation of the vector between p1 and p2 added to the new vector's polar rotation.

Then I use the unit vector, u, in the horizontal plane and perpendicular to the new space's polar rotation (2 radians), to define a new rotation axis and matrix, Qu(angle). The angle in this matrix represents the azimuth angle of the line between p1 and p2

Finally, I multiply the vector by this matrix to rotate it about the new axis, u, by an amount equal to the azimuth angle of the line between p1 and p2 (0.9 radians), and just add this vector to p1.

I can randomly increment the relative polar/azimuth rotations, distances, and starting points to draw multiple lines to form whorls of lines around a newly defined axis, like this:

Its working well, my only concern is that there are so many operations involved in the multiplication of the matrices that it will slow down my program. I may look for a simpler way of handling this in the future but for now I'm happy.

I hope that explains things a little better, and thanks again.

Last edited:

#### studiot

That's interesting, but will take some thinking about.