Path: news.mathworks.com!not-for-mail
From: "salih tuna" <salihtuna@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Angle between two vectors
Date: Tue, 11 Dec 2007 15:29:00 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 62
Message-ID: <fjmabs$9fh$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> <fjm4u4$i8o$1@fred.mathworks.com>
Reply-To: "salih tuna" <salihtuna@gmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1197386940 9713 172.30.248.37 (11 Dec 2007 15:29:00 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 11 Dec 2007 15:29:00 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1061324
Xref: news.mathworks.com comp.soft-sys.matlab:441995


Roger hi,
thanks a lot for your help but i am afraid something is
still missing. i tried the formulae on the same example of 
a = [1 1]and b = [0 -1] (both passing through origin).
the answer i got is 315 instead of 225.
sorry i am taking a lot of your time :)
thanks
salih

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
wrote in message <fjm4u4$i8o$1@fred.mathworks.com>...
> "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
> 
>