Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Lua script that adds polar vectors, without compensation for significant figures.
#!/usr/bin/env lua
function exit(msg, code)
os.exit(code or 1)
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.