# problem involving spheres and angles

## Recommended Posts

This is for a coding project I'm working on. It's not to do with men walking on spheres but it's a good way of describing the problem.

Can anyone produce equations, code or similar to calculate outputs for given inputs.

Inputs are:

x, y & z coordinates of the spheres center

start latitude coordinate of the man

start longitude coordinate of the man

start angle of the man

distance for the man to travel

For the following scenario:

Imagine the sphere centered at given coordinates where the north pole is at the topmost point. A man stands on the sphere at the latitude and longitude specified where 0 latitude is the north pole, 1 latitude is the south pole and ±1 longitude rotates east\west once around the sphere. He is facing a specified angle where 0 and 1 are facing the north pole, 0.5, 0.25 to the east, 0.75 to the west etc... he walks the specified distance around the sphere from his point of view in a straight line in the direction he is facing where 1 represents once round the largest part of the sphere such as the equator.

I then need to be able to get the following outputs:

final x, y & z coordinates of the man

final latitude coordinate of the man

final longitude coordinate of the man

final angle of the man

The mans start angle will definitely be equal to his final angle if the distance traveled is a multiple of 1 or he starts with a latitude of 0.5 and an angle of 0.25 or 0.75 representing him going either way round the equator. Similarly going away from or towards the poles without passing them would show no change in angle.

Edited by alan2here
##### Share on other sites

I'm sure there are more straight forward methods but they would most likely be derived from any or all of the following:

- represent the current rotations of the man with a rotation matrix

- the y rotation would be the latitude

- to get the longitude negate the angle of the y rotation(up axis) component of the rotation matrix to align with or perpendicular to the x or the y components of the rotation matrix and take the aligned value(play with this)

- use the Arc Length equation to calculate an angle in any given axis rotation keeping in mind the radius is relative to the position along said axis. Create a rotation matrix from the angle and multiply the current rotation matrix by it

- Convert the Spherical Coordinates(longitude, latitude, radius) to Cartesian Coordinates to get the x,y,z

I make many mistakes on things like this which I usually iron out by doing it... having not actually attempted I may have said something wrong; if so forgive me. I have done many similar models and there are lots of references online to how to properly do the steps involved. You might also be interested in looking up Quaternions....

- Euler Angles

- Rotation Matrix

- Transformation Matrix

- Quaternions

- Arc Length

- Solid Arc(maybe, not really)

- anything else all of the above leads to

o~o

Merged post follows:

Consecutive posts merged

a) you could do this all in spherical coordinates like this

b) if you use matrices in particular if you expand the representation to a transformation matrix you can include the x,y,z translation in a POV built matrix. also the earth is not perfectly round and this could be compensated for by integration of a scaling matrix(all frames of reference will have to be considered when developing a model that does this)

The matrix method in the end is much easier to use especially as the model becomes more complex. I don't know, I kind of got a headache when I looked for the simple method of doing 10000km 25ºNE in spherical coordinates in my head....

Edited by buttacup
##### Share on other sites

Thanks, looks like it would make a good question for one studying maths, hard with various possible methods of doing it covering all sorts of stuff.

I can use this equation from the article to create a simple verstion of the problem.

x = r Cos[lat] Cos[long]

y = r Cos[lat] Sin[long]

z = r Sin[lat]

This will be ok as a placeholder for now. I tried to create a similar equation but it was not as good as my sphere was lemon shaped.

Merged post follows:

Consecutive posts mergedSo I know I'm correctly oriented, for using this equation, with trigonometric functions that use radians.

z = up\down

latitude of -pi*0.5 = north pole

latitude of pi*0.5 = south pole

longitude of 0 is the same place as a longitude of pi.

Edited by alan2here
Consecutive posts merged.
##### Share on other sites

I would start trying subsequent simple coordinate transformations:

1) A rotation matrix A that rotates the position of the man into the x-y plane.

2) A rotation matrix B (rot around the z-axis) that moves the man on x=1, y=0 (or however you label say your north pole).

3) A rotation matrix C (around the x-axis in my proposal) that rotates the forward direction to e.g. (0,1,0).

4) You know can now apply your step forward (in my example he will walk counter-clockwise in a circle in the x-y plane).

5) After that, you probably want to transform your coordinates back to the original coordinate system, i.e. undo steps 3, 2 and 1 in that order. That is done with the matrix $A^{-1}B^{-1}C^{-1}$, the exponent -1 denoting the inverse, in case you are not familiar with matrices (you'll probably need a lot more help in this case, then).

Hint: Denoting the stepping (in the rotated intermediate coordinate system) symbolically by X (in fact it is just another rotation matrix so you can actually write it as a matrix) the mathematical expression for the step from the old coordinates x to the new coordinates y is $y = \underbrace{A^{-1}B^{-1}C^{-1}}_{=T^{-1}} \ X \ \underbrace{CBA}_{=:T} x$. By construction, you already know what XCBAx will be (because the matrices were constructed to get that result). Let's call that z. So the problem you have to solve is finding the inverse of CBA (or their individual inverses) and then applying it on z.

There might be a more intuitive way. Since the matrices in question are all rotation matrices around one of the main axes it's quite likely that writing down the mess analytically can lead to drastic simplifications (if you have access to a computer algebra system or like doing math then you could try that). But it should work in principle.

EDIT: Be aware that the proposal above does not take into account the way you expressed x-, y- and z-coordiante as a function of two angles (was too lazy). So a different order of rotations might suit you better. But I hope you get the idea.

Thank you for a non-homework, non-"I proved Einstein/Gödel/Fermat/God/yourmotheronapogostick wrong" thread.

##### Share on other sites

If I must code up complicated matrix stuff is there one transformation matrix that I can use instead of 3 in a row?

##### Share on other sites

Not entirely as there will always be the representing matrix and the matrices through which you will apply transformations to said representing matrix! What language and what maths library are you using?

Merged post follows:

Consecutive posts merged

Matrices are really not that complicated if you aren't actually doing the underlying math .....

although the underlying math is a hell of a lot of fun and really impresses other people

Edited by buttacup
Consecutive posts merged.
##### Share on other sites

C++ std library. I have coded matrix multiplications before, but this is on just one point, in quite a simple way, where the point starts in a place defined by a simple equation before it is moved. I'm sure it doesn't need all the little things that could go wrong, take a long time to do, may require dependency on other libraries and are hard to test with matrix transformations. Especially as I don't have much of a maths background and this is the only part of the whole program that needs any part of the calculation.

##### Share on other sites

Use a transformation matrix and set the translation to the x,y,z coordinates.... you can do this.... it's something like this but not entirely! I would simplify the code but I am going out.... have fun. o.o

##### Share on other sites

For this:

z (up & down) = r sin(lat)

x = r cos(lat) cos(long)

y = r cos(lat) sin(long)

(0, 0, 0) is not in the center of the sphere, it's on an edge? and the sphere moves left and right depending on the latitude chosen? Might not be a particularly hard problem but seems odd.

Merged post follows:

Consecutive posts mergedSo the matrix way does get me all the angles and things specified in the first post as outputs when given the input parameters specified? Edited by alan2here
Consecutive posts merged.
##### Share on other sites

So the matrix way does get me all the angles and things specified in the first post as outputs when given the input parameters specified?

yes like so:

First begin with a view matrix....

The At vector usually denoting the point of focus of the view will now represent the center of the earth and simply be the origin like so

$\vec At(x,y,z) = \begin{matrix} 0\\ 0\\ 0 \end{matrix}$

The Up vector will now denote the direction in which the man is facing where north will be represented like so

$\vec Up(x,y,z) = \begin{matrix} 0\\ 1\\ 0 \end{matrix}$

this last point is especially relevant in that for your scenario you are going to want to maintain an orientation where the man consistently faces North and to do so you will have to reorient after each move

The Eye vector will be the x,y,z coordinates of the man in question in planar 3Dimmensional Cartesian Coordinates like so

$\vec Eye(x,y,z) = \begin{matrix} Man_x\\ Man_y\\ Man_z \end{matrix}$

Now the first thing you are going to want to do is build a view matrix following this simple formula:

$\vec Z_{axis} = normal(\vec At - \vec Eye)$

$\vec X_{axis} = normal(cross(\vec Up, \vec Z_{axis}))$

$\vec Y_{axis} = cross(\vec Z_{axis}, \vec X_{axis})$

$VIEW_{matrix} VIEW = \begin{matrix} X_{axis}.x & Y_{axis}.x & Z_{axis}.x & 0 \\ X_{axis}.y & Y_{axis}.y & Z_{axis}.y & 0 \\ X_{axis}.z & Y_{axis}.z & Z_{axis}.z & 0 \\ -dot(X_{axis}, eye) & -dot(Y_{axis}, eye) & -dot(Z_{axis}, eye) & 1 \end{matrix}$

the function you will call to build this matrix will do the above and as a consequence the resultant will be a scaled representation normalized to 1

continue....

So the trick with all this is that we invert the frame of reference we transform the man so that he is aligned with origin and the center of the world is orbiting around him!

To do this we must Transform the mans Up and Eye vectors by the VIEW Matrix just created like so

$\vec Eye = VIEW \cdot \begin{matrix} Eye_{x} \\ Eye_{y} \\ Eye_{z} \\ 1 \end{matrix}$

when done with a library transform you should have a fourth element in the vector added which is the normalization value which when normalized should be 1

k

So now you would build a rotation matrix for the x and y rotations which will correspond to the amount you wish to travel in the NWES directions use Pythagorean Theorom to find your x component and y components like so

travel 10000km 25º NE

you would use

$sin a = \frac{sin 25 \cdot sin 10000}{sin 90}$

and

$sin b = \frac{sin 65 \cdot sin 10000}{sin 90}$

keeping in mind the signs of the x and y values for each quadrant.... and also that this is spherical trig not linear [ edited for correctness I'm lazy sometimes :/ ]

I'm not including the equations to build a rotation matrix here no....

so now multiply the original $VIEW_{matrix}$ by the two $X_{rot}$ and $Y_{rot}$ matrices to rotate the center of the earth around the Man.

$NEWVIEW_{matrix} = VIEW_{matrix} \cdot X_{rot} \cdot Y_{rot}$

The inverse of the resulting matrix $NEWVIEW^{-1}_{matrix}$ contains the Cartesian coordinates in the m_14 m_24 and m_34 positions of the matrix like so

$NEWVIEW^{-1}_{matrix} = \begin{matrix} m_{11} & m_{12} & m_{13} & x \\ m_{21} & m_{22} & m_{23} & y \\ m_{31} & m_{32} & m_{33} & z \\ m_{41} & m_{42} & m_{43} & m_{44} \end{matrix}$

well scaled o.o

to find the longitude and latitude and reorient the man facing north and leaving you with a usable matrix to start all over with you must first

get the Eular rotation around $Y_{axis}$ by taking the inverted cosine of $m_{11}$. This angular value is also the longitudinal value if only offset by quadrant which you would have to set up properly .....

take this value and build a rotation matrix $Y_{rot}$ and find its inverse $Y^{-1}_{rot}$ and transform the above derived $NEWVIEW^{-1}_{matrix}$ with it retaining it in a new matrix

Repeat this step with the $X_{axis}$ taking the inverse cosine of $m_{22}$ this time to get the latitude and transform the retained matrix as opposed to the original

now from the retained matrix get the $Z_{axis}$ rotation angle by taking either the inverse cosine of $m_{11}$ or the inverse cosine of $m_{22}$ build a rotation matrix $Z_{rot}$ find its inverse and multiply the $NEWVIEW^{-1}_{matrix}$ to get the man oriented facing the north pole at a tangent to his position on the sphere $MAN_{oriented matrix}$..... this is rather important and I hope I did that right!

Finally build a scaling matrix like so

$Scale_{matrix} = \begin{matrix} X_{axis} & 0 & 0 \\ 0 & Y_{axis} & 0 \\ 0 & 0 & Z_{axis} \\ \end{matrix}$

and

$MAN_{matrix} = Scale_{matrix} \cdot MAN_{oriented matrix}$

to get the final representation of man which you can easily get the Eye from $MAN_{matrix}$ and At = {0.0f, 0.0f, 0.0f } and Up = {0.0f, 1.0f, 0.0f }

Its easier than it looks and I highly recommend this method until someone shows me a better way

Sorry if I made any mistakes ....... I dadn't try this myself so :\

thx bai

mistake with up if it is tangential it can't be Up = {0.0f, 1.0f, 0.0f } anywhere other than the equator ... oh noes

not too difficult to calculate though 'the retained matrix'

Edited by buttacup
##### Share on other sites

This is for a coding project I'm working on. It's not to do with men walking on spheres but it's a good way of describing the problem.

Can anyone produce equations, code or similar to calculate outputs for given inputs.

Inputs are:

x, y & z coordinates of the spheres center

start latitude coordinate of the man

start longitude coordinate of the man

start angle of the man

distance for the man to travel

For the following scenario:

Imagine the sphere centered at given coordinates where the north pole is at the topmost point. A man stands on the sphere at the latitude and longitude specified where 0 latitude is the north pole, 1 latitude is the south pole and ±1 longitude rotates east\west once around the sphere. He is facing a specified angle where 0 and 1 are facing the north pole, 0.5, 0.25 to the east, 0.75 to the west etc... he walks the specified distance around the sphere from his point of view in a straight line in the direction he is facing where 1 represents once round the largest part of the sphere such as the equator.

I then need to be able to get the following outputs:

final x, y & z coordinates of the man

final latitude coordinate of the man

final longitude coordinate of the man

final angle of the man

The mans start angle will definitely be equal to his final angle if the distance traveled is a multiple of 1 or he starts with a latitude of 0.5 and an angle of 0.25 or 0.75 representing him going either way round the equator. Similarly going away from or towards the poles without passing them would show no change in angle.

Here is C++ code than implements the Euler rotational matrix.

void CUtil::EulerAngleTranslation( double x, double y, double z, double a, double b, double c, double * ptransx, double * ptransy, double * ptransz )

{

a *= PICOVERSION;

b *= PICOVERSION;

c *= PICOVERSION;

*ptransz = (sin(b)*sin(a)) * x + (-sin(b)*cos(a)) * y + (cos(b)) * z ;

}

(x,y,z) is the original point in degrees.

(a,b,c) is the Euler angles.

##### Share on other sites

Does this work????

travel 10000km 25º NE

you would use

$sin a = \frac{sin 25 \cdot sin 10000}{sin 90}$

and

$sin b = \frac{sin 65 \cdot sin 10000}{sin 90}$

$\theta_{radians} = a/earths radius$

$\phi_{radians} = b/earths radius$

where $\theta$ and $\phi$ are in radians

convert to degrees and add to longitude and latitude accordingly and then convert spherical coordinates to Cartesian for Cartesian x,y,z values...

ftw????

##### Share on other sites

Thanks. I may try Buttacups solution, it could take a long time to implement.

Buttacup, does your latest post add onto the post before or is it a solution in itself?

Whats the Euler rotational matrix, a component of Buttacups solution or a whole solution?

final_latitude = latitude + (sin(angle) * distance)

final_longitude = longitude + (cos(angle) * distance)

final_x = radius * cos(final_latitude) * cos(final_longitude)

final_y = radius * cos(final_latitude) * sin(final_longitude)

final_angle = angle

This should be unfortunatly distorted as it presumes the N, S, E & W you would get on a map not a sphere. But it seems braudly correct, provided the equation from here that it is based on is correct.

Edited by alan2here
##### Share on other sites

My last post is independent! It uses the Sine Law for Spherical Triangles found as part of Spherical Trigonometry.... You calculate the longitudinal and latitudinal distances and find the rotation using arc length and then apply it. From there it is a simple conversion to Cartesian Coordinates to get your x,y,z in planar..... I still suggest matrices.... you might find working directly in trig on a pc to be a bit messy....

Merged post follows:

Consecutive posts merged

Whats the Euler rotational matrix, a component of Buttacups solution or a whole solution?

Euler angles are what a rotational matrix represents .... combined to three dimensions they are often referred to as pitch, yaw and roll which are specific examples thereof where they would be analogous to a rotation about each axis in three dimensional space.

Edited by buttacup
Consecutive posts merged.
##### Share on other sites

Cool, trigonometry is not that hard, the solution I preposed in my last post uses it.

travel 10000km 25º NE

you would use

$sin a = \frac{sin 25 \cdot sin 10000}{sin 90}$

and

$sin b = \frac{sin 65 \cdot sin 10000}{sin 90}$

$\theta_{radians} = a/earths radius$

$\phi_{radians} = b/earths radius$

where $\theta$ and $\phi$ are in radians

Ok, lets try that.

latitude = equator = 0.5

longitude = 0

angle = 0.3

distance = 1

$sin a = sin 0.3 * sin 1 = 0.25$

$sin b = sin 1.27 * sin 1 = 0.8$

Wolfram Alpha told me that "a = 2 * pi * n + sin^-1(n)".

sin^-1(n) = arcsin(n)?

a = 1.82

b = 5.95

o1 = 1.82/2 = 0.91

o2 = 5.95/2 = 2.98

convert to degrees and add to longitude and latitude accordingly and then convert spherical coordinates to Cartesian for Cartesian x, y, z values...

I'm not sure how to do this bit. Also nothing functions in degrees.

which values corispond to the required outputs?

Edited by alan2here
##### Share on other sites

$\sin{\frac{\pi}{2}}\neq .84$

and I made a mistake as I forewarned I would often do....

where I put the arc length it should have first been converted to degrees.... as in the lengths are actually degrees..... Sorry!

I'm a bow out now I learned much but have other work to do ..... thanks for the sesh Allan....

Edited by buttacup
##### Share on other sites

90 degrees is 1.57 radians, sin(1.57) = 1

Thanks, looks like I miscalculated that first time somehow.

I simplfied out the devide by 1.

How do I interface the parts together?

Green represents placeholder code that's not entirely accurate. Blue represents code that's not part of the forumuli yet.

sin_a = sin(angle) * sin(distance)

sin_b = sin(angle) * sin(distance)

a = 2 * pi * sin_a + arcsin(sin_a)

b = 2 * pi * sin_b + arcsin(sin_b)

o1 = a/2

o2 = b/2

final_latitude = latitude + (sin(angle) * distance)

final_longitude = longitude + (cos(angle) * distance)

final_x = radius * cos(final_latitude) * cos(final_longitude)

final_y = radius * cos(final_latitude) * sin(final_longitude)

final_angle = angle

Edited by alan2here
##### Share on other sites

sin_a = sin(angle) * sin(distance)

sin_b = sin(angle) * sin(distance)

it's not distance it is the angle between the radii and the points created by the distance so it would be distance over radius .....

what you currently have marked as the angle is the corresponding inner angle of the triangles corners delineated by the Spherical Trig Sine Law... apparently these do not add to 180º as I expected.... there is an excess. Needless to say I have surprisingly never touched Spherical Trig and/or Spherical Triangles... I guess I have some reading to do! Wiki is my only source at the moment on this stuff so I hope it's accurate. None of my books even mention, guess I need something specifically on trig :/

and to answer the question $\theta \; and \; \phi$ go in the green parts..... which because of my boo boo are actually a and b! I don't know what you mean by final angle though????

so like

$\theta = a = \arcsin{(\sin{(angle)} \cdot \sin{(\frac{distance}{radius})})}$

$S_{\epsilon}(x) \triangleq \left \{ y \in F^{n}: \left \| x - y \right \| = \epsilon \right \}$

Edited by buttacup
##### Share on other sites

Corrected below.

inputs:
sphere in space: x, y, z, radius
man on sphere: latitude, longitude
man's intention: angle, distance

final_latitude = latitude + (sin(angle) * distance)
final_longitude = longitude + (cos(angle) * distance)
final_z = z + (radius * sin(final_latitude))
final_x = x + (radius * cos(final_latitude) * cos(final_longitude))
final_y = y + (radius * cos(final_latitude) * sin(final_longitude))
final_angle = angle

outputs:
man on sphere: final_latitude, final_longitude, final_angle
man in space: final_z, final_x, final_y

angle is the angle the man is facing in radians relative to the north pole

final_angle is angle the man is facing in radians relative to the north pole after he has moved distance in angle direction

etc...

Edited by alan2here
##### Share on other sites

angle is the angle the man is facing in radians relative to the north pole

ummm those angles of which there are actually two there are the two angles that are not 90º that are of the inner angles of a Spherical Triangle where their sum is something on the order of slightly more than 180º

##### Share on other sites

Do 'a' and 'b' help the rest of the function at all?

btw the function should work for moving east around the sphere and north, it's only when moving diagonally that there may be a problem, which is that the man will curve towards the poles. The other problem is that final_angle will produce the wrong answer.

##### Share on other sites

a and b make things move diagonally if you don't care just move things in one axis either longitudinal are latitudinal.....

final_latitude = latitude + a

final_longitude = longitude + b

keeping in mind you will have to compensate for any artifacts the same as you would without a & b

Edited by buttacup
##### Share on other sites

inputs:
sphere in space: x, y, z, radius
man on sphere: latitude, longitude
man's intention: angle, distance

faulty_latitude_move = sin(angle) * distance
faulty_longitude_move = cos(angle) * distance

final_latitude = latitude + latitude_move
final_longitude = longitude + longitude_move
final_z = z + (radius * sin(final_latitude))
final_x = x + (radius * cos(final_latitude) * cos(final_longitude))
final_y = y + (radius * cos(final_latitude) * sin(final_longitude))
final_angle = angle

outputs:
man on sphere: final_latitude, final_longitude, final_angle
man in space: final_z, final_x, final_y

Horray, see the old version labled faulty and the new version at the top.

Now only one more part to do:

final_angle = angle

##### Share on other sites

If your final angle is the facing direction of the man it is sort of irrelevant using the above equations.... If he moves diagonally he is still facing north if he was already facing north.... the only time he won't be facing north is when he is facing south when he crosses the pole???? I'm not sure what you are looking for here??? Are you trying to do tracking so you can normalize the longitude and latitude in conjunction with the standard or are you looking at making the man look in directions??? If you are referring back to the matrix method I suggested using it has a deeper meaning.... I guess maybe...

My engine might be working next week, maybe I'll throw something together if it is; probably not no ..... drink moar coffee maybe I guess.... should be excited it hasn't been operational for three months :/

How are you presenting this? Just like in numbers?

##### Share on other sites

If your final angle is the facing direction of the man it is sort of irrelevant using the above equations. If he moves diagonally he is still facing north if he was already facing north. The only time he won't be facing north is when he is facing south when he crosses the pole?

Only on a map, not on a sphere or the "faulty_latitude_move" code would work. Continuing with the globe analogy he starts in England, turns to the north and moves some distance getting him close to the north pole, in this case his angle relative to the north pole would not change. If he were to move east or west putting him in the ocean or the direction of Russia the result would be the same.

However if he turns to face north east then moves you would not expect his angle relative to the north pole to stay the same. It's going to start at 1/8th of a turn clockwise and end up at 1/4 of a turn when he is north of Russia, imagine this happening on a globe.

How are you presenting this?

Very visually. It's going to be easy to see if it has worked.

## Create an account

Register a new account