Re: Urgent
- To: mathgroup at smc.vnet.net
 - Subject: [mg27279] Re: Urgent
 - From: "Allan Hayes" <hay at haystack.demon.co.uk>
 - Date: Fri, 16 Feb 2001 03:58:31 -0500 (EST)
 - References: <9686u0$3qi@smc.vnet.net>
 - Sender: owner-wri-mathgroup at wolfram.com
 
Here are two possibilites for 2D polygons
Both will give the angles in the correct quadrant.
angles2c will give the angles in the range  (-Pi ,2Pi] and
angles2r will give them in the range (-2Pi,2Pi). To convert them to the
range (-Pi ,2Pi]  we could use Mod[ . , 2Pi, -Pi] but this will slow the
computation down markedly.
angles2c[verts_]:=
  Arg[RotateLeft[#]/#&[RotateLeft[#]-#&[Complex@@@verts]]]
angles2r[verts_]:=(RotateLeft[#]-#)&[ArcTan@@@(RotateLeft[#]-#&[verts])]
The following will work for any dimensions, but it will give answers in the
range [0,Pi] and so leaves it ambiguous in 2D whether the true answer is for
example Pi/2 or -Pi/2.
angles[verts_] :=
  MapThread[ArcCos[(#1 .#2)/Sqrt[(#1.#1)(#2.#2)]] &,
    {RotateLeft[#], #} &[{1, -1}.({ RotateLeft[#], #} &[verts])]]
Here are some timings
verts = Table[Random[Real, {-1, 1}], {100000}, {2}];
angles2c[verts];//Timing
        {10.22 Second,Null}
angles2r[verts];//Timing
        {8.08 Second,Null}
angles[verts];//Timing
        {62.01 Second,Null}
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"oda" <adj at ensm-douai.fr> wrote in message news:9686u0$3qi at smc.vnet.net...
> Hi , all
>
> I'm currently use Mathematica 4.0 and I need to compute
> fastly the angle between  the vertices of a polygon.
> The statement of the problem is :
>
> Let be (P) a polygon with n vertices point, I want to calulate
> the angle (T) between all consecutif edge.
>
> (P) = {P1, P2, P3,.............,Pn}
> P1 is a 2 or 3 dimension point.
> angles between (PiPi+1, Pi+1Pi+2) i = 1, n-2;
>
> I have about (100000 points in polygon)
>
> Thanks in advance
>