Created
March 7, 2013 08:40
-
-
Save mebens/5106500 to your computer and use it in GitHub Desktop.
A Lua script that adds polar vectors, without compensation for significant figures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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