I've got the following information:
There exists a sphere with origin (0,0,0) and radius R. After doing a ray-sphere intersection I know a point (XYZ) in 3D space that is on the sphere (the exact position in 3D space where the line pierces the sphere hull).
For my program I'd like to calculate the Latitude and Longitude of the XYZ point on the sphere, but I can't think (or Google) up a way to do this easily.
So in short, the function that I'm trying to write is this:
public static LatLon FromVector3(Vector3 position, float sphereRadius)
{
return Latitude and Longitude
}
Does anybody know how to do this? As a reference this Wiki SVG file might be helpful:
Update:
Thanks for all the helpful answers, so in the end I went with this code:
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);
}
Now I've got to think of which answer helped me the most to accept :P.
I guess it should not be difficult to find the spherical polar coordinates from x,y,z (3d-coordinate system).
r
is always constant if it's on surface.(90 - θ) your latitude (negative means it's on the bottom) as it's measured from top.
φ is your longitude. (but not quite sure about longitude system)
Also check this diagram from wikipedia.
This helped using Javascript/THREE.js:
After working on getting a straightforward solution to placing objects on a sphere using lat/lng, I came up with a simple class to let you do it using three.js.
https://github.com/scottbyrns/Three.js-Geospatial-Mapping
This is back-of-envelope work but:
Using formulas with
atan2()
is more convenient. You don't have to add/subtract pi/2 or care about sign issues in different quadrants or division by zero.lat
will be >0 in the northern hemispherelat
will be <0 in the southern hemispherelng
will be >0 in the eastern hemispherelng
will be <0 in the western hemisphereEdit - having reread you question my answer isn't necessarily applicable, but I'll leave it up for reference.
It depends how accurate you wan to be an dwhat purpose you are going to use the result for. There is no single latitude and logitude system, eg WGS84 (USA GPS) or ETRS89 (European GPS) differ slightly and are diverging as the Atlantic Ocean widens.
http://www.ordnancesurvey.co.uk/oswebsite/gps/information/coordinatesystemsinfo/guidecontents/guide5.html
http://www.ordnancesurvey.co.uk/oswebsite/gps/information/coordinatesystemsinfo/guidecontents/guide6.html
Finally this should address your question directly.
http://www.ordnancesurvey.co.uk/oswebsite/gps/information/coordinatesystemsinfo/guidecontents/guideb.html
or
http://www.ordnancesurvey.co.uk/oswebsite/gps/docs/convertingcoordinates3D.pdf