Prev: how to determine if the shortest distance from one point to a line is inside the line segment or outside
Next: help
From: Natalie Sin Hwee on 8 Jul 2010 11:06 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
From: Natalie Sin Hwee on 8 Jul 2010 11:28 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
From: Roger Stafford on 8 Jul 2010 15:04 "Natalie Sin Hwee " <sin.ng09(a)imperial.ac.uk> wrote in message <i14qq5$2rp$1(a)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
From: Natalie Sin Hwee on 8 Jul 2010 17:35 "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i157f5$mkt$1(a)fred.mathworks.com>... > "Natalie Sin Hwee " <sin.ng09(a)imperial.ac.uk> wrote in message <i14qq5$2rp$1(a)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 Dear Roger, That worked brilliantly! thank you SO much! u're a life saver!! ^^ Thank you very much!!! Regards, Natalie
From: Natalie Sin Hwee on 8 Jul 2010 17:37
Sorry i forgot to ask, If i was to apply this into 3D, how can i apply ang = mod( atan2( det([v1;v2]) , dot(v1,v2) ) , 2*pi ); if e.g. v1= [1,2,3] v2= [4,5,6] I tried but it says: ??? Error using ==> det Matrix must be square. Thanks Natalie |