Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Angle between two vectors
Date: Tue, 11 Dec 2007 13:56:20 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 37
Message-ID: <fjm4u4$i8o$1@fred.mathworks.com>
References: <ef5ce9c.-1@webcrossing.raydaftYaTP> <fjj9nj$fia$1@fred.mathworks.com> <fjk0tg$jli$1@fred.mathworks.com> <fjlrpl$gii$1@fred.mathworks.com>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1197381380 18712 172.30.248.35 (11 Dec 2007 13:56:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 11 Dec 2007 13:56:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:441974


"salih tuna" <salihtuna@gmail.com> wrote in message <fjlrpl$gii
$1@fred.mathworks.com>...
> Hi,
> thanks a lot for your reply. yes they are in 2d, sorry i
> forgot to mention.
> i tried to apply the formula but i am getting wrong result.
> for example i want to calculate the angle between a = [1 1]
> and b = [0 -1] which is 225 degrees. with this formulae i
> got 243.4. i couldn't see where i am doing the mistake.
> thanks a lot in advance
> salih
> 
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> wrote in message <fjk0tg$jli$1@fred.mathworks.com>...
> >  angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi
--------
  I certainly owe you an apology, Salih.  That formula I gave you is very, very 
wrong.  I can't imagine what I was thinking about when I wrote it.  Chalk it up 
to momentary insanity!  :-)  The correct computation should be as follows.

  Assuming a = [x1,y1] and b = [x2,y2] are two vectors with their bases at the 
origin, the non-negative angle between them measured counterclockwise 
from a to b is given by

 angle = mod(atan2(x1*y2-x2*y1,x1*x2+y1*y2),2*pi);

  As you can see, this bears a close relationship to the three-dimensional 
formula I wrote last July 10.  The quantities, x1*y2-x2*y1 and x1*x2+y1*y2 
are, respectively, the sine and cosine of the counterclockwise angle from 
vector a to vector b, multiplied by the product of their norms - that is, their 
cross product and the dot product restricted to two dimensions.  The 'atan2' 
function then gives the angle between them ranging from -pi to +pi, and the 
'mod' operation changes this so as to range from 0 to 2*pi, as you requested.

Roger Stafford