From: Natalie Sin Hwee on
Hello,

i have two lines:
- line 1 is between (0,3) and (4,3)
- line 2 is between (0,2) and (3,0)

How can i find the angle between these two lines because their origin is not (0,0) so i cant use the vector method to find the angle.

all i've managed to do is plot the two lines :
%line 1
x1=[0 4];
y1=[3 3];

line1=plot(x1,y1,'r')
hold on

% %line 2
x2=[0 3];
y2=[2 0];
line2=plot (x2,y2)

Please help
Thank you
Natalie
From: Matt J on
"Natalie Sin Hwee " <sin.ng09(a)imperial.ac.uk> wrote in message <hno4am$6ui$1(a)fred.mathworks.com>...
> Hello,
>
> i have two lines:
> - line 1 is between (0,3) and (4,3)
> - line 2 is between (0,2) and (3,0)
>
> How can i find the angle between these two lines because their origin is not (0,0) so i cant use the vector method to find the angle.
========

The angle between two lines is the angle between their direction vectors. The direction vectors of two lines can always be found by subtracting the position vectors of two points on the line

DirVector1=[4,3]-[0,3];
DirVector2=[0,2]-[3,0];

Angle=acos( dot(DirVector1,DirVector2)/norm(DirVector1)/norm(DirVector2) );
From: Roger Stafford on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hno669$aj9$1(a)fred.mathworks.com>...
> .........
> DirVector1=[4,3]-[0,3];
> DirVector2=[0,2]-[3,0];
>
> Angle=acos( dot(DirVector1,DirVector2)/norm(DirVector1)/norm(DirVector2) );
----------
The atan2 function is more robust accuracy-wise than acos when dealing with vectors that could be very nearly parallel. A glance at a plot of acos over its full range shows why. (Not in this particular case of course.)

v1 = [4,3] - [0,3];
v2 = [3,0] - [0,2];
angle = atan2(norm(cross(v1,v2)),dot(v1,v2));

Roger Stafford
From: Natalie Sin Hwee on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hnocom$c9h$1(a)fred.mathworks.com>...
> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hno669$aj9$1(a)fred.mathworks.com>...
> > .........
> > DirVector1=[4,3]-[0,3];
> > DirVector2=[0,2]-[3,0];
> >
> > Angle=acos( dot(DirVector1,DirVector2)/norm(DirVector1)/norm(DirVector2) );
> ----------
> The atan2 function is more robust accuracy-wise than acos when dealing with vectors that could be very nearly parallel. A glance at a plot of acos over its full range shows why. (Not in this particular case of course.)
>
> v1 = [4,3] - [0,3];
> v2 = [3,0] - [0,2];
> angle = atan2(norm(cross(v1,v2)),dot(v1,v2));
>
> Roger Stafford

Thansk for replying ^^

can i just ask how does using atan2 differ from atan

i tried
theta= atan((y2-y1)/(x2-x1))-atan((y4-y3)/(x4-x3))

thanks!
natalie
From: Roger Stafford on
"Natalie Sin Hwee " <sin.ng09(a)imperial.ac.uk> wrote in message <hnqh84$p5k$1(a)fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hnocom$c9h$1(a)fred.mathworks.com>...
> > .....
> > angle = atan2(norm(cross(v1,v2)),dot(v1,v2));
> > ......
>
> can i just ask how does using atan2 differ from atan
> i tried
> theta= atan((y2-y1)/(x2-x1))-atan((y4-y3)/(x4-x3))
> ......

My apologies. The expression I gave really applies to a three-dimensional case. For a two-dimensional case a determinant can replace the cross product function and 'abs' replaces 'norm':

A) angle = atan2(abs(det([v1,v2])),dot(v1,v2));

(where v1 and v2 are column vectors.) If you were to append a third dimension onto the vectors with z1=z2=z3=z4=0, (thereby having all points in the x-y plane) and use the normed cross product in the expression I gave before, it would reduce to this. Of course A) can also be written as

angle = atan2(abs((x2-x1)*(y4-y3)-(y2-y1)*(x4-x3)), ...
(x2-x1)*(x4-x3)+(y2-y1)*(y4-y3));

where v1 = [x2-x1;y2-y1] and v2 = [x4-x3;y4-y3].

Formula A) can be interpreted as giving the inner (non-negative) angle at the vertex of a triangle formed between two vectors with their bases both at this vertex and the two vector endpoints being the other two vertices. Such inner angles must of course lie between 0 and pi.

This does not always give the same answer as the expression you wrote, Natalie. Notice that if you interchange the points (x1,y1) and (x2,y2), or else (x3,y3) and (x4,y4), your expression will remain unaltered, whereas A) will be changed to the supplement of the previous angle. Your expression has a range from -pi to +pi and A) ranges from 0 to +pi.

If you remove the 'abs' from expression A):

B) angle = atan2(det([v1,v2]),dot(v1,v2));

this gives the angle measured from v1 around to v2 with the understanding that a counterclockwise direction is positive and a clockwise direction is negative. Its range is then from -pi to +pi.

However even then B) is not equivalent to yours. As stated before, your expression does not change in value if v1 or v2 is reversed in sign, whereas the expression B) above switches to the opposite quadrant - that is, it increases or decreases by pi, whichever remains in range. All three expressions are therefore following different rules. What you use depends on what properties you wish your angle to possess.

However, I suspect that you may not want to use one that does not reflect a switch to the opposite direction of either of your vectors. I cannot think offhand of an application that would require such a strange rule.

Roger Stafford