Xah Lee schreef:
> Xah Lee wrote:
> «
> ...
> “Ignorance And Intolerance In Online Computing Communities”
> http://xahlee.org/Netiquette_dir/ignorance_intolerance.html
> 
> ...  As i have indicated in my post, it is non-trivial to implement a
> function that returns the positive angle of a vector....
> »
> 
> I have now coded this. I think it is probably the most algorithmically
> optimal, and rather much simpler than i originally thought. Here's the
> Mathematica code:
> 
> vectorAngle[{a1_, a2_}] := Module[{x, y},
>     {x, y} = {a1, a2}/Sqrt[a1^2 + a2^2] // N;
>     If[x == 0 && y == 0, "fucked",
>       If[x == 0, [EMAIL PROTECTED] === 1, π/2, -π/2],
>         If[y == 0, [EMAIL PROTECTED] === 1, 0, π],
>           [EMAIL PROTECTED] === 1, [EMAIL PROTECTED], 2 π - [EMAIL PROTECTED]
>           ]
>         ]
>       ]
>     ]

I might be wrong of course, but can't you just use atan2? Only problem 
is that it returns negative angles for quadrants 3 and 4, but that is 
easily solved. In Python:

from math import atan2, pi, fmod
def vectorAngle(x, y):
     return fmod(atan2(y, x) + 2*pi, 2*pi)

No conditionals in sight.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to