ignorance and intolerance in computing communties

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Thu May 3 12:04:22 EDT 2007


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, If[Sign at y === 1, π/2, -π/2],
>         If[y == 0, If[Sign at x === 1, 0, π],
>           If[Sign at y === 1, ArcCos at x, 2 π - ArcCos at x]
>           ]
>         ]
>       ]
>     ]

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



More information about the Python-list mailing list