Skip to content

Instantly share code, notes, and snippets.

@mebens
Created March 7, 2013 08:40
Show Gist options
  • Select an option

  • Save mebens/5106500 to your computer and use it in GitHub Desktop.

Select an option

Save mebens/5106500 to your computer and use it in GitHub Desktop.

Revisions

  1. Michael Ebens created this gist Mar 7, 2013.
    25 changes: 25 additions & 0 deletions add-polars.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/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()