Sunday, September 13, 2020

Orbital velocity and altitude calculations in C#

Just thought I'd share a C# snippet for calculating nominal orbital speed if you have the center mass and orbital radius. And to go the other way if you have the center mass and speed to get the nominal orbital radius.


public static class OrbitalCalculations
{
    public const double G = 0.0000000000667d;
    public static double NominalOrbitalSpeed(double centerMass, double orbitalRadius)
    {
        if (orbitalRadius.IsZero())
            return 0;
        return Math.Sqrt(G * centerMass / orbitalRadius);
    }
    public static double NominalOrbitalRadius(double centerMass, double speed)
    {
        if (speed.IsZero())
            return 0;
        return G * centerMass / (speed * speed);
    }
    public static bool IsZero(this double d, double tolerance = 0.0001)
    {
        return Math.Abs(d) < tolerance;
    }
}
If you want the math behind and more: 
Hope this helps someone out there! : )