Skip to content

Instantly share code, notes, and snippets.

@mebens
Created March 7, 2013 08:40
Show Gist options
  • Save mebens/5106500 to your computer and use it in GitHub Desktop.
Save mebens/5106500 to your computer and use it in GitHub Desktop.
A Lua script that adds polar vectors, without compensation for significant figures.
#!/usr/bin/env lua
function exit(msg, code)
print(msg)
os.exit(code or 1)
end
function main()
if #arg < 4 then exit("Please provide a magnitude and angle for two polar vectors") end
local m1, a1, m2, a2 = unpack(arg)
m1, a1, m2, a2 = tonumber(m1), math.rad(tonumber(a1)), tonumber(m2), math.rad(tonumber(a2))
local x1 = m1 * math.cos(a1)
local y1 = m1 * math.sin(a1)
local x2 = m2 * math.cos(a2)
local y2 = m2 * math.sin(a2)
local x, y = x1 + x2, y1 + y2
local angle = math.deg(math.atan2(y, x))
if angle < 0 then angle = angle + 360 end
print("Magnitude: " .. math.sqrt(x ^ 2 + y ^ 2))
print("Angle: " .. angle)
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment