lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Apr 5, 2012 at 00:40, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>>>> To calculate the angle beteween the two vectors, you sould do:
>>>>
>>>> math.atan2(b[2],b[1]) - math.atan2(a[2],a[1])
>>>>
>>>>
>>>
>>> Not that simple.  That could give anything between ± 2 pi, whereas angle should
>>> be between ± pi.
>>
>> By adding/subtracting 2pi accordingly one can make result to be in (-pi,+pi].
>>
>
> The formula
>
>     math.atan2 (a[1]*b[2]-a[2]*b[1],a[1]*b[1]+a[2]*b[2])
>
> makes only one call to math.atan2, requires no post-processing, and has slightly
> better roundoff properties.

Good points. An obvious optimal solution for those who remember what
cross-product and dot-product  are:-)

--Leo--