angle_3d.lua


NAME
    angle_3d

FUNCTION
    angle_3d(x0, y0, z0, x1, y1, z1)

NOTES
    Calculate the angle between a 3D vector and the x-axis.

INPUTS
    x0, y0, z0 - coordinate of the start point
    x1, y1, z1 - coordinate of the end point

OUTPUTS
    theta - angle in degree (equvalent to latitude)
    phi   - angle in degree (equvalent to longitude)
    d     - length of the vector

SOURCE

require("register")

function angle_3d(x0, y0, z0, x1, y1, z1)
    local dx = x1 - x0
    local dy = y1 - y0
    local dz = z1 - z0
    local d3 = math.sqrt(dx * dx + dy * dy + dz * dz)
    local d2 = math.sqrt(dx * dx + dy * dy)
    if (d2 ~= 0 and d3 ~= 0) then
        local theta = 90 - 57.29577951 * math.asin(dz / d3)
        local phi = 57.29577951 * math.acos(dx / d2)
        if dy >= 0 then
            return theta, phi, d3
        else
            return theta, -phi, d3
        end
    end
end