Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Angle between two vectors
Date: Fri, 18 Jun 2010 04:38:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 28
Message-ID: <hvet7b$b6c$1@fred.mathworks.com>
References: <ef5ce9c.-1@webcrossing.raydaftYaTP> <1184052626.324470.175540@n2g2000hse.googlegroups.com> <8db784bf-a96e-431b-b5c6-d6836905d350@o16g2000prh.googlegroups.com> <hvbni8$s0h$1@fred.mathworks.com> <hvbra7$lcp$1@fred.mathworks.com> <hvbs37$a30$1@fred.mathworks.com> <hvc2qe$59b$1@fred.mathworks.com> <hvejah$ab6$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1276835883 11468 172.30.248.37 (18 Jun 2010 04:38:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 18 Jun 2010 04:38:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:646024

"Beho nashed" <beho86@yahoo.com> wrote in message <hvejah$ab6$1@fred.mathworks.com>...
> Hello Roger,
> I keep selecting points for an angle bigger than 90 degree, but the program is giving me an angle less than 90 deg!!!
> .......

  There must be something amiss, either with the way you are selecting your points or the way their coordinates are being collected.  I have done a copy and paste on the three different forms of the formulas we have discussed and after supplying a missing left bracket in the last one, (sorry about that) they all give the same answer which can be anywhere between zero and pi radians, or in degrees, anywhere between 0 and 180 degrees.  I illustrate with one example here which yields the correct answer of 135 degrees (after multiplying by 180/pi to transform from radians to degrees) for all three formulas.

P1 =  [2,1];
P2 =  [6,3];
P3 = [-7,4];
x = [P1(1);P2(1);P3(1)];
y = [P1(2);P2(2);P3(2)];
x1 = x(1); y1 = y(1);
x2 = x(2); y2 = y(2);
x3 = x(3); y3 = y(3);
ang1 = atan2(abs(det([x1,x2,x3;y1,y2,y3;1,1,1])), ...
            (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1));
ang2 = atan2(abs(det([x(1),x(2),x(3);y(1),y(2),y(3);1,1,1])), ...
            (x(2)-x(1))*(x(3)-x(1))+(y(2)-y(1))*(y(3)-y(1)));
ang3 = atan2(abs(det([P2-P1;P3-P1])),dot(P2-P1,P3-P1));

180/pi*[ang1;ang2;ang3] =

   135
   135
   135

Roger Stafford