0
$\begingroup$

I am trying to write airplane tracking software and I need to turn North/South Velocity and East/West velocity into a 0-360 degree heading.

For example: Plane 1 is travelling North-West and has 220 mph groundspeed North/South and -440 mph groundspeed East/West. What is the heading in compass degrees and how do I calculate that?

edit: made example more specific to output heading in compass degree's

2 Answers 2

0

I did this programmatically in Node JavaScript ES6:

let calculateHeading = function(nsVelocity, ewVelocity) {
    /*
    Converts velocity North/South East/West velocity and turns it into a 360 degree compass heading
        Input: nsVelocity, ewVelocity
        Output: compass heading degree 0 - 360, 0/360 is North
     */

    try {
        // Calculates the heading in degrees based off north south velcoity and east west velocity
        let fnsVelocity = parseFloat(nsVelocity);
        let fewVelocity = parseFloat(ewVelocity);

        let velocity = calculateVelocity(nsVelocity, ewVelocity);

        if (velocity < 0.000001) {
            velocity = 1.0;
        }

        // get angle of vector from x-coordinate in degrees
        let rotation = Math.round(Math.acos( Math.abs(fewVelocity) / velocity ) / 0.0174533);

        // angle as calculated is a magnitude, always positive
        // use the two velocities' signs to adjust angle in four possible quadrants
        if (fnsVelocity >= 0.0) {
            if (fewVelocity >= 0.0) {
                rotation = Math.abs(rotation - 90);
            } else {
                rotation = rotation + 270;
            }
        } else {
            if (fewVelocity >= 0.0) {
                rotation = rotation + 90;
            } else {
                rotation = rotation + 180;
            }
        }
        return Math.round(rotation);

    } catch (e) {
        return 0;
    }

};

let calculateVelocity = function(velocityNs, velocityEw) {
    /*
     Calculates velocity from components
     Args:
        velocityNs: Float north-south velocity knots
        velocityEw: Float east-west velocity knots
     Returns:
        Float velocity knots
     */
    try {
        var fEwVelocity = parseFloat(velocityEw);
        var fNsVelocity = parseFloat(velocityNs);
        return Math.round(Math.sqrt(Math.pow(fNsVelocity, 2) + Math.pow(fEwVelocity, 2)));
    } catch (e) {
        return 0;
    }

};
0

The angle you want is $$\tan^{-1}\frac{v_{SN}}{v_{WE}}$$

As for the quadrants, many programming languages have a two-argument arctangent function precisely so as to get you into the right quadrant without any effort and also handle the pathological cases of “due north” and “due south”. It is quite likely that Python does too.