angle_2d.lua


NAME
    angle_2d

FUNCTION
    angle_2d(x0, y0, x1, y1)

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

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

OUTPUTS
    phi - angle in degree
    d   - length of the vector

SOURCE

require("register")

function angle_2d(x0, y0, x1, y1)
	local dx = x1 - x0
	local dy = y1 - y0
	local d2 = math.sqrt(dx * dx + dy * dy)
	if (d2 ~= 0) then
		local phi = 57.29577951 * math.acos(dx / d2)
		if dy >= 0 then
			return phi, d2
		else
			return -phi, d2
		end
    end
end