Convert a direction into an angle on our compass I have a new position vector and old x and z coordinates and I need to determine the angle traveled. So far I am getting the slopes absolute value and then using that to generate an angle. Based on some tests it appears that the slope values converge to 0 when moving West (90 degrees) and East (270 degrees) they also seem to converge on infinity as the slope approaches North (180 degrees) and South (360 degrees aka 0 degrees). Furthermore, the slope seems to be 1 when it lands on an ordinal direction (NW, NE, SW, SE).

It also appears that the following equation correctly defines the angle for the NW_North section but I am not sure about the other 7 sections any thoughts?
$$θ=90(2−2^{−x})$$
public static int getNormalizedMovementAngle(Vector3d vector, double x, double z) {
final double zDifference = vector.getZ() - z;
final double xDifference = vector.getX() - x;
final double slope = Math.abs(zDifference / xDifference);

if(vector.getX() == x) {
     if(vector.getZ() > z) return 0;
     return 180;
}
else if(vector.getX() < x) {
    if(vector.getZ() == z) return 90;
    else if(vector.getZ() < z) {
        if(slope == 1) return 135;
         //Does this actually work correctly handle all West_NW cases??
         return Math.toIntExact(Math.round(90 * (2 - Math.pow(2, -slope))));

        if(slope == 1) return 45;
        //This needs to handle both SW sections
    }
    else {
        if(vector.getZ() == z) return 270;
        else if(vector.getZ() < z) {
            if (slope == 1) return 225;
            //This needs to handle both sections and idk if adding 90 will fix it...
            return Math.toIntExact(Math.round((90 * (2 - Math.pow(2, -slope))) + 90));
        }

        if(slope == 1) return 315;
        //Is adding 180 even correct does it handle both less than and greater than 1 cases?
        return Math.toIntExact(Math.round((90 * (2 - Math.pow(2, -slope))) + 180));
    }
}

 A: The function you are looking for in 2D is
$$\theta = \operatorname{atan2}\left(\Delta Z,-\Delta X \right)$$
which is the 2-argument form of arctangent; like $\arctan(\Delta Z / - \Delta X)$, but takes both components signs into consideration, and covers a full rotation ($\arctan$ only covers half a circle).
If we use a more common coordinate system, with $+x$ rightwards at $0°$, $+y$ upwards at $90°$, $-y$ downwards at $-90°$, and $-x$ leftwards at $\pm 180°$, in Javascript:
function direction_degrees(fromX, fromY, toX, toY) {
    return Math.atan2(toY - fromY, toX - fromX) * 180.0 / Math.PI;
}

since in JS, Math.atan2(y, x) returns the angle from origin to (x, y) in radians (-Math.PI to +Math.PI).

In 3D, we normally use the standard Earth-centric, Earth-fixed (ECEF) coordinate system with origin at the center of mass of Earth, positive $z$ axis passing through North pole, negative $z$ axis passing through South pole, and positive $x$ axis passing through prime meridian ($0°$) on the equator, with $xy$ plane on the equator.  This means that negative $x$ axis passes through the date line ($180°$/$180°$), and positive $y$ axis is at $90°E$, negative $y$ axis at $90°W$.
Compass direction is well defined at every point except at the North and South poles, where the only possible direction is South or North, respectively.
We can define the compass direction from any point in ECEF coordinates to any other point in ECEF coordinates, as long as the starting point is not at North or South pole, not too close to the center of mass of Earth (but that is thousands of kilometers deep in the iron core of Earth so no worries), and the end point is not antipodal (exactly on the other side of the center of mass of Earth, origin) from the start point.
This is somewhat complicated by the fact that in the most commonly used Earth model (in GPS for example), WGS 84, Earth is not a sphere but a slightly flattened ellipsoid with larger diameter at the equator than the distance between North and South poles.  This is a commonly explored and solved problem, as it is exactly the heading provided by GPS from one point to another.
