trans_3d.lua


NAME
    trans_3d

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

NOTES
    Calculate 3D transformation from (x0, y0, z0) to (x1, y1, z1).

INPUTS
    x0, y0, z0 - numbers specify the start point
    x1, y1, z1 - numbers specify the end point

OUTPUTS
    Distance and rotation angles (degree) around z-, and y-axis (equivalent to latitude and longitude)

SOURCE

function trans_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 d3, theta, phi
        else
            return d3, theta, -phi
        end
    end
end