Skip to content

Instantly share code, notes, and snippets.

@sukesh-ak
Last active November 13, 2022 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sukesh-ak/fcdf69570f44616fd0f66bcf4052a026 to your computer and use it in GitHub Desktop.
Save sukesh-ak/fcdf69570f44616fd0f66bcf4052a026 to your computer and use it in GitHub Desktop.
Spherical to Cartesian coordinates
/// Converts Spherical to Cartesian coordinates
int earthRadius = 6367; //radius in km
private Cartesian convertSphericalToCartesian(double latitude, double longitude)
{
var lat = DegreesToRadians(latitude);
var lon = DegreesToRadians(longitude);
var x = earthRadius * Math.Cos(lat) * Math.Cos(lon);
var y = earthRadius * Math.Cos(lat) * Math.Sin(lon);
var z = earthRadius * Math.Sin(lat);
return new Cartesian(x, y, z);
}
private class Cartesian
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Cartesian(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment