Tuesday, September 27, 2016

c# - How do I translate a spherical coordinate to a Cartesian one?


Could someone point me in the right direction as to how this might be achieved? 3D math / geometry often throws me.


I'm looking for something like this (ideally in C#):


public Vector3 getCartesianFor(float elevation, float asimuth, float polar)
{
return ????;
}


Answer



http://blog.nobel-joergensen.com/2010/10/22/spherical-coordinates-in-unity/


public static void SphericalToCartesian(float radius, float polar, float elevation, out Vector3 outCart){
float a = radius * Mathf.Cos(elevation);
outCart.x = a * Mathf.Cos(polar);
outCart.y = radius * Mathf.Sin(elevation);
outCart.z = a * Mathf.Sin(polar);
}



public static void CartesianToSpherical(Vector3 cartCoords, out float outRadius, out float outPolar, out float outElevation){
if (cartCoords.x == 0)
cartCoords.x = Mathf.Epsilon;
outRadius = Mathf.Sqrt((cartCoords.x * cartCoords.x)
+ (cartCoords.y * cartCoords.y)
+ (cartCoords.z * cartCoords.z));
outPolar = Mathf.Atan(cartCoords.z / cartCoords.x);
if (cartCoords.x < 0)
outPolar += Mathf.PI;
outElevation = Mathf.Asin(cartCoords.y / outRadius);

}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...