Page 1 of 1

Determine Vector between 2 coordinates

Posted: Tue Feb 23, 2010 1:52 am
by roflamingo
I am trying to find the formula for this.... maybe I am phrasing wrong.

E.g. starting at Moscow, calculate what compass-bearing would you need to travel to eventually reach Paris?

Bonus points if the solution is already in LUA.

Posted: Tue Feb 23, 2010 2:39 am
by DinoSteve

Code: Select all

function Bearing(x1, y1, x2, y2)
    local dx = x2 - x1
    local dy = y2 - y1

    return math.atan2(dx, dy)
end


Results in radians, not degrees

Posted: Tue Feb 23, 2010 2:49 am
by Ace Rimmer
What happened to math.deg?

Posted: Tue Feb 23, 2010 2:57 am
by DinoSteve
Ace Rimmer wrote:What happened to math.deg?


Since all the trig functions work with radians it's best to stick to them. Only use math.deg for outputting results. That way you don't waste CPU converting types needlessly, less code, and you don't risk accidentally forgetting to convert back.

Posted: Tue Feb 23, 2010 3:00 am
by Ace Rimmer
Thanks for the clarifications. :D