2017-09-15 93 views
0

我想在一个球体上点一个点,然后将该点转换为相应的经度和纬度。我碰到这个问题,涉及到我做的几乎一模一样堆栈交换了后来Vector3到纬度经度

3D coordinates on a sphere to Latitude and Longitude

使用这个功能

public static LatLon FromVector3(Vector3 position, float sphereRadius) 
    { 
     float lat = (float)Math.Acos(position.Y/sphereRadius); //theta 
     float lon = (float)Math.Atan(position.X/position.Z); //phi 
     return new LatLon(lat, lon); 
    } 

但功能总是在范围内的某处返回值对于lat为0到3,对于经度为-1到1。

我使用的程序是Unity,有谁知道为什么我可能会得到这些不正确的值?我将射线命中世界矢量和球体半径作为参数传递,球体以世界0,0,0为中心。

非常感谢所有可能帮助您的人!

更新的代码

public static Vector2 GetLatLonFromVector3 (Vector3 position, float sphereRadius){ 
     float lat = (float)Mathf.Acos(position.y/sphereRadius); //theta 
     float lon = (float)Mathf.Atan2(position.x, position.z); //phi 
     lat *= Mathf.Rad2Deg; 
     lon *= Mathf.Rad2Deg; 
     return new Vector2(lat, lon); 
    } 

我现在正在运行中的问题是该值没有被正确地象限,他们在反映出来。在弧度

+0

如果Z分量很小,最好使用'Atan2(position.X,position.Z)'。 – meowgoesthedog

+0

当我切换到Atan2时,我得到完全不同的值,为什么? –

+0

因为'Atan'不考虑你的“点”[X,Z]所在的象限;它也只覆盖了180度的角度,而不是360度(这是phi的域) – meowgoesthedog

回答

2

反余弦和反正切返回值,从-π(-3.1 ...)到π(3.1 ...)。为了转换为度数,除以π并乘以180. Unity甚至有一个有用的常数,Mathf.Rad2Deg使其变得容易。

public static LatLon FromVector3(Vector3 position, float sphereRadius) 
{ 
    float lat = (float)Math.Acos(position.Y/sphereRadius); //theta 
    float lon = (float)Math.Atan(position.X/position.Z); //phi 
    return new LatLon(lat * Mathf.Rad2Deg, lon * Mathf.Rad2Deg); 
}