Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: Angle between two vectors Date: Sat, 17 Mar 2012 23:10:29 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 62 Message-ID: <jk35l4$sb4$1@newscl01ah.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> <hvet7b$b6c$1@fred.mathworks.com> <jk1qvp$492$1@newscl01ah.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-04-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1332025829 29028 172.30.248.35 (17 Mar 2012 23:10:29 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sat, 17 Mar 2012 23:10:29 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:761324 "Amit " <av_pachore@yahoo.com> wrote in message <jk1qvp$492$1@newscl01ah.mathworks.com>... > Hello Roger, > I want to find the angle between line1 as vertex-p2 and line2 as vertex-points stored in array either in clockwise or anticlockwise.But did not getting angles with following code: > > vertex=[186,130]; > p2=[278,155]; > x=[87 91 103 175];%x co-ordinates of points > y=[134 104 76 31];%y co-ordinates of points > > [r,c]=size(x); > ang=zeros(size(x)); > > for i=1:c > pt=[x(i),y(i)]; > ang(i)=mod(atan2(abs(det([p2-vertex;pt-vertex])),dot(p2-vertex,pt-vertex)),2*pi); > end; > > ang=180/pi*ang; > display(ang); > > This gives the result as > ang = > > 162.4839 179.8963 162.1543 111.5426 > > if these angles are only anticlockwise then pt[175,31] would have angle more than 180 degrees > or if it is clockwise [87,134] would have angle more than 180 degrees. > Please help me with this. > Thanks in advance. - - - - - - - - - - In two dimensional space there is a difference between, on the one hand finding the angles, say, within a triangle which always lie between 0 and pi radians (0 and 180 degrees), and on the other hand finding the angle between two vectors with a common base starting from one of them and rotating counterclockwise (or sometimes clockwise) until first encountering the other one. The latter gives an angle that is anywhere between 0 and 2*pi radians (0 and 360 degrees), or according to a different convention, between -pi and +pi radians (-180 and +180 degrees.) In using matlab's 'atan2' function properly it is immportant to distinguish between these different meanings. In your problem if you want to rotate counterclockwise from the vector p2-v (using 'v' instead of 'vertex') until first encountering the vector pt-v, expecting an angle between 0 and 360 degrees, the appropriate formula would be: ang = mod(atan2(det([p2-v;pt-v]),dot(p2-v,pt-v)),2*pi)*180/pi; Note that the 'abs' operation is missing here. That only applies if you are seeking results that are restricted to lie between 0 and pi as in the angles of a triangle. The 'mod' operation is necessary here since 'atan2' yields answer between -pi and +pi, so the negative answers need to have 2*pi added to them. If the rotation is counterclockwise from pt-v to p2-v or if it is clockwise from p2-v to pt-v, the two arguments pt-v and p2-v would be reversed in the above formula. If your convention is that going beyond pi radians is regarded as negative rotation, then you would leave out the 'mod' operation. In the example you gave with pt = [ 87,134; 91,104; 103, 76; 175, 31]; p2 = [278,155]; v = [186,130]; the formula above for rotating counterclockwise from p2-v to pt-v would give the four answers: 162.4839 180.1037 197.8457 248.4574 Reversing the arguments (that is, going clockwise or from pt-v to p2-v) would give: 197.5161 179.8963 162.1543 111.5426 Without the 'mod' operation the counterclockwise from p2-v to pt-v answers would be: 162.4839 -179.8963 -162.1543 -111.5426 Roger Stafford