Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Find angle between two lines
Date: Thu, 8 Jul 2010 19:04:05 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 33
Message-ID: <i157f5$mkt$1@fred.mathworks.com>
References: <hno4am$6ui$1@fred.mathworks.com> <hno669$aj9$1@fred.mathworks.com> <hnocom$c9h$1@fred.mathworks.com> <hnqh84$p5k$1@fred.mathworks.com> <hnrncl$mit$1@fred.mathworks.com> <i14qq5$2rp$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1278615845 23197 172.30.248.38 (8 Jul 2010 19:04:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 8 Jul 2010 19:04:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:651555

"Natalie Sin Hwee " <sin.ng09@imperial.ac.uk> wrote in message <i14qq5$2rp$1@fred.mathworks.com>...
> Dear Roger,
> 
> thanks for getting back to me. I'm working on this bit of my code again.
> 
> I was wondering if there was a way to find the angles all anti clockwise because
> i dont want to have 0 to pi and 0 to -pi due to some loop bits making it complicated.
> 
> I am now using:
>         theta_rad(t,u)= (atan2(norm(cross(v1,v2)),dot(v1,v2)));
> but it finds the smallest angle, either clockwise or anti clockwise.
> 
> is there a way so that it calculates angles only in 1 direction?
> 
> Thank you
> 
> Regards,
> Natalie
- - - - - - - -
  The expression you wrote in this last posting with the cross function in it can only apply to three dimensional space.  The cross product is not defined in two dimensions.  However, in three dimensions the notion of moving counterclockwise between vectors is no longer a meaningful one.

  In two dimensions if you have two column vectors va and vb and you wish to find the angle moving counterclockwise from va towards vb, and you don't want negative angles, it will necessarily be an angle that ranges from 0 to 2*pi.  You can compute it as follows:

 ang = mod( atan2( det([va,vb]) , dot(va,vb) ) , 2*pi );

If va and vb are row vectors then the argument of the determinant changes to [va;vb].

  In your original March posting you had the vectors defined by a pair of points for each vector.  If vector va points from point P1 to P2 and vector vb points from point P3 to P4, where P1 = (x1,y1), P2 = (x2,y2), P3 = (x3,y3), and P4 = (x4,y4), then you can use this formula:

 ang = mod( atan2( (x2-x1)*(y4-y3)-(y2-y1)*(x4-x3) , ...
                   (x2-x1)*(x4-x3)+(y2-y1)*(y4-y3) ) , 2*pi);

Roger Stafford