Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Angle between two vectors

Subject: Angle between two vectors

From: y Mehta

Date: 9 Jul, 2007 17:16:29

Message: 1 of 77

How do I find the angle between two unit vectors a and b? I know I
can find cosine theta by the following formula:

theta = acos(dot(a,b));

However, how do I know whether the angle is actually theta, or -theta
or pi-theta or pi+theta??

Notice that the vectors are in three dimension (3d).

Thanks,
-YM

Subject: Angle between two vectors

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 10 Jul, 2007 02:57:36

Message: 2 of 77

In article <ef5ce9c.-1@webcrossing.raydaftYaTP>, "y Mehta"
<mehtayogesh@gmail.(DOT).com> wrote:

> How do I find the angle between two unit vectors a and b? I know I
> can find cosine theta by the following formula:
>
> theta = acos(dot(a,b));
>
> However, how do I know whether the angle is actually theta, or -theta
> or pi-theta or pi+theta??
>
> Notice that the vectors are in three dimension (3d).
>
> Thanks,
> -YM
---------------------
  It is usually understood that the angle between two three-dimensional
vectors is measured by the shortest great circle path between them, which
means that it must lie between 0 and pi radians. To get such an answer,
the best method, in my opinion, is this:

 angle = atan2(norm(cross(a,b)),dot(a,b));

Since the first argument must be non-negative, the angle will lie
somewhere in the two first quadrants, and thus be between 0 and pi. This
formula remains valid even if a and b are not unit vectors.

  Your 'acos' formula gives the correct answer with unit vectors as it
stands, but it encounters an accuracy problem for angles that are near 0
or pi. This is the main reason for my preference for the 'atan2' method.

Roger Stafford

Subject: Angle between two vectors

From: Greg Heath

Date: 10 Jul, 2007 00:30:26

Message: 3 of 77

On Jul 9, 5:16 pm, "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote:
> How do I find the angle between two unit vectors a and b? I know I
> can find cosine theta by the following formula:
>
> theta = acos(dot(a,b));
>

Invalid since it is possible that abs(dot(a,b)) > 1.

costheta = dot(a,b)/(norm(a)*norm(b));
theta = acos(costheta);

will give you the anser in the interval [0,pi].

> However, how do I know whether the angle is
> actually theta, or -theta or pi-theta or pi+theta??

Angles between vectors only lie in the interval [0,pi].

> Notice that the vectors are in three dimension (3d).

Dimensionality of the original space is irrelevant. As long as
norm(a)*norm(b) > 0, the vectors uniquely define a 2-d space when
dot(a,b) ~= 0 and a unique 1-d space otherwise.

Hope this helps.

Greg

Subject: Angle between two vectors

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 10 Jul, 2007 08:27:08

Message: 4 of 77

In article <1184052626.324470.175540@n2g2000hse.googlegroups.com>, Greg
Heath <heath@alumni.brown.edu> wrote:

> On Jul 9, 5:16 pm, "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote:
> > How do I find the angle between two unit vectors a and b? I know I
> > can find cosine theta by the following formula:
> >
> > theta = acos(dot(a,b));
>
> Invalid since it is possible that abs(dot(a,b)) > 1.
>
> costheta = dot(a,b)/(norm(a)*norm(b));
> theta = acos(costheta);
>
> will give you the anser in the interval [0,pi].
>
> > However, how do I know whether the angle is
> > actually theta, or -theta or pi-theta or pi+theta??
>
> Angles between vectors only lie in the interval [0,pi].
>
> > Notice that the vectors are in three dimension (3d).
>
> Dimensionality of the original space is irrelevant. As long as
> norm(a)*norm(b) > 0, the vectors uniquely define a 2-d space when
> dot(a,b) ~= 0 and a unique 1-d space otherwise.
>
> Hope this helps.
>
> Greg
---------------
  Greg, Y Mehta did specifically state that a and b are unit vectors, so
his formula is in fact correct as it stands, though subject to increasing
errors as its dot product approaches +1 or -1. When a and b are nearly
parallel, the same kind of trouble occurs with the formula you have given
here. In such cases there is a real need to combine the scalar dot
product with the vector cross product in order to make use of both the
sine and cosine in calculating the angle accurately, which is what the
'tan2' formula does.

Roger Stafford

Subject: Angle between two vectors

From: Greg Heath

Date: 10 Jul, 2007 14:55:26

Message: 5 of 77

On Jul 10, 4:27 am, ellieandrogerxy...@mindspring.com.invalid (Roger
Stafford) wrote:
> In article <1184052626.324470.175...@n2g2000hse.googlegroups.com>, Greg
>
>
>
>
>
> Heath <h...@alumni.brown.edu> wrote:
> > On Jul 9, 5:16 pm, "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote:
> > > How do I find the angle between two unit vectors a and b? I know I
> > > can find cosine theta by the following formula:
>
> > > theta = acos(dot(a,b));
>
> > Invalid since it is possible that abs(dot(a,b)) > 1.
>
> > costheta = dot(a,b)/(norm(a)*norm(b));
> > theta = acos(costheta);
>
> > will give you the anser in the interval [0,pi].
>
> > > However, how do I know whether the angle is
> > > actually theta, or -theta or pi-theta or pi+theta??
>
> > Angles between vectors only lie in the interval [0,pi].
>
> > > Notice that the vectors are in three dimension (3d).
>
> > Dimensionality of the original space is irrelevant. As long as
> > norm(a)*norm(b) > 0, the vectors uniquely define a 2-d space when
> > dot(a,b) ~= 0 and a unique 1-d space otherwise.
>
> > Hope this helps.
>
> > Greg
>
> ---------------
> Greg, Y Mehta did specifically state that a and b are unit vectors, so
> his formula is in fact correct as it stands, though subject to increasing
> errors as its dot product approaches +1 or -1. When a and b are nearly
> parallel, the same kind of trouble occurs with the formula you have given
> here. In such cases there is a real need to combine the scalar dot
> product with the vector cross product in order to make use of both the
> sine and cosine in calculating the angle accurately, which is what the
> 'tan2' formula does.

Thanks.

For some problem in the past (probably single precision?) I got better
accuracy using

sign(sintheta)*acos(costheta)

instead of atan2.

Hope this helps.

Greg

Hope this helps.

Subject: Angle between two vectors

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 10 Jul, 2007 23:22:36

Message: 6 of 77

In article <1184104526.774530.54800@n60g2000hse.googlegroups.com>, Greg
Heath <heath@alumni.brown.edu> wrote:

> For some problem in the past (probably single precision?) I got better
> accuracy using
>
> sign(sintheta)*acos(costheta)
>
> instead of atan2.
---------------
  I don't know why that would be better, Greg. The acos(costheta) is
subject to the same problem as before. When costheta is very near 1, the
accuracy is very much inferior to that of 'atan2'. I would think this
would be equally true in single precision.

  Here's a concrete example of what I am referring to:

 format long
 a = pi*(1-1/10^8); % Choose an angle a little below pi
 a2 = atan2(sin(a),cos(a)); % Use atan2
 a3 = acos(cos(a)); % Use acos
 [a;a2;a3;a-a2;a-a3] % Compare results

 a = pi/2*1.123; % Now select an angle near pi/2
 a2 = atan2(sin(a),cos(a)); % atan2 again
 a3 = acos(cos(a)); % Then acos
 [a;a2;a3;a-a2;a-a3] % Make the same comparisons

 ans =

   3.14159262217387
   3.14159262217387
   3.14159262378747
                  0
  -0.00000000161360

 ans =

   1.76400427499067
   1.76400427499067
   1.76400427499067
   0.00000000000000
   0.00000000000000

As you see, there is a very distinct loss of accuracy in 'acos' for angles
near pi. Some seven entire decimal places have been lost - that is,
errors are several million times as large as normal. On the other hand,
the angle near pi/2 yields the customary 1 in 2^52 accuracy.

Roger Stafford

Subject: Angle between two vectors

From: Yogesh Mehta

Date: 16 Jul, 2007 01:46:40

Message: 7 of 77

Thanks Greg and Roger. Your inputs were very useful for me.

Subject: Angle between two vectors

From: salih tuna

Date: 10 Dec, 2007 11:59:47

Message: 8 of 77

hello,
how can i calculate the angles so that they are in the range
0-360 degrees?
thanks
salih


"y Mehta" <mehtayogesh@gmail.(DOT).com> wrote in message
<ef5ce9c.-1@webcrossing.raydaftYaTP>...
> How do I find the angle between two unit vectors a and b?
I know I
> can find cosine theta by the following formula:
>
> theta = acos(dot(a,b));
>
> However, how do I know whether the angle is actually
theta, or -theta
> or pi-theta or pi+theta??
>
> Notice that the vectors are in three dimension (3d).
>
> Thanks,
> -YM

Subject: Angle between two vectors

From: salih tuna

Date: 10 Dec, 2007 12:00:24

Message: 9 of 77

hello,
how can i calculate the angles so that they are in the range
0-360 degrees?
thanks
salih


"y Mehta" <mehtayogesh@gmail.(DOT).com> wrote in message
<ef5ce9c.-1@webcrossing.raydaftYaTP>...
> How do I find the angle between two unit vectors a and b?
I know I
> can find cosine theta by the following formula:
>
> theta = acos(dot(a,b));
>
> However, how do I know whether the angle is actually
theta, or -theta
> or pi-theta or pi+theta??
>
> Notice that the vectors are in three dimension (3d).
>
> Thanks,
> -YM

Subject: Angle between two vectors

From: Roger Stafford

Date: 10 Dec, 2007 18:35:28

Message: 10 of 77

"salih tuna" <salihtuna@gmail.com> wrote in message <fjj9nj$fia
$1@fred.mathworks.com>...
> hello,
> how can i calculate the angles so that they are in the range 0-360 degrees?
> thanks
> salih
>
> "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote in message
> <ef5ce9c.-1@webcrossing.raydaftYaTP>...
> > How do I find the angle between two unit vectors a and b? I know I
> > can find cosine theta by the following formula:
> >
> > theta = acos(dot(a,b));
> >
> > However, how do I know whether the angle is actually theta, or -theta
> > or pi-theta or pi+theta??
> >
> > Notice that the vectors are in three dimension (3d).
> >
> > Thanks,
> > -YM
--------
  Y Mehta's question involved angles between vectors in three-dimensional
space. I can think of no reasonable definition for a canonical angle between
such vectors which ranges from 0 to 360 degrees (0 to 2*pi radians.)

  However, if you are in two-dimensional space, then you can speak of the
non-negative angle measured counterclockwise from vector a to vector b,
and this would give the range you have requested. If a = [x1,y1] and b =
[x2,y2], then such an angle is given in matlab by:

 angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi radians

(Multiply this answer by 180/pi to get degrees.)

Roger Stafford

Subject: Angle between two vectors

From: salih tuna

Date: 11 Dec, 2007 11:20:21

Message: 11 of 77

Hi,
thanks a lot for your reply. yes they are in 2d, sorry i
forgot to mention.
i tried to apply the formula but i am getting wrong result.
for example i want to calculate the angle between a = [1 1]
and b = [0 -1] which is 225 degrees. with this formulae i
got 243.4. i couldn't see where i am doing the mistake.
thanks a lot in advance
salih


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
wrote in message <fjk0tg$jli$1@fred.mathworks.com>...
> "salih tuna" <salihtuna@gmail.com> wrote in message
<fjj9nj$fia
> $1@fred.mathworks.com>...
> > hello,
> > how can i calculate the angles so that they are in the
range 0-360 degrees?
> > thanks
> > salih
> >
> > "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote in message
> > <ef5ce9c.-1@webcrossing.raydaftYaTP>...
> > > How do I find the angle between two unit vectors a and
b? I know I
> > > can find cosine theta by the following formula:
> > >
> > > theta = acos(dot(a,b));
> > >
> > > However, how do I know whether the angle is actually
theta, or -theta
> > > or pi-theta or pi+theta??
> > >
> > > Notice that the vectors are in three dimension (3d).
> > >
> > > Thanks,
> > > -YM
> --------
> Y Mehta's question involved angles between vectors in
three-dimensional
> space. I can think of no reasonable definition for a
canonical angle between
> such vectors which ranges from 0 to 360 degrees (0 to 2*pi
radians.)
>
> However, if you are in two-dimensional space, then you
can speak of the
> non-negative angle measured counterclockwise from vector a
to vector b,
> and this would give the range you have requested. If a =
[x1,y1] and b =
> [x2,y2], then such an angle is given in matlab by:
>
> angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi
radians
>
> (Multiply this answer by 180/pi to get degrees.)
>
> Roger Stafford

Subject: Angle between two vectors

From: Roger Stafford

Date: 11 Dec, 2007 13:56:20

Message: 12 of 77

"salih tuna" <salihtuna@gmail.com> wrote in message <fjlrpl$gii
$1@fred.mathworks.com>...
> Hi,
> thanks a lot for your reply. yes they are in 2d, sorry i
> forgot to mention.
> i tried to apply the formula but i am getting wrong result.
> for example i want to calculate the angle between a = [1 1]
> and b = [0 -1] which is 225 degrees. with this formulae i
> got 243.4. i couldn't see where i am doing the mistake.
> thanks a lot in advance
> salih
>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> wrote in message <fjk0tg$jli$1@fred.mathworks.com>...
> > angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi
--------
  I certainly owe you an apology, Salih. That formula I gave you is very, very
wrong. I can't imagine what I was thinking about when I wrote it. Chalk it up
to momentary insanity! :-) The correct computation should be as follows.

  Assuming a = [x1,y1] and b = [x2,y2] are two vectors with their bases at the
origin, the non-negative angle between them measured counterclockwise
from a to b is given by

 angle = mod(atan2(x1*y2-x2*y1,x1*x2+y1*y2),2*pi);

  As you can see, this bears a close relationship to the three-dimensional
formula I wrote last July 10. The quantities, x1*y2-x2*y1 and x1*x2+y1*y2
are, respectively, the sine and cosine of the counterclockwise angle from
vector a to vector b, multiplied by the product of their norms - that is, their
cross product and the dot product restricted to two dimensions. The 'atan2'
function then gives the angle between them ranging from -pi to +pi, and the
'mod' operation changes this so as to range from 0 to 2*pi, as you requested.

Roger Stafford

Subject: Angle between two vectors

From: salih tuna

Date: 11 Dec, 2007 15:29:00

Message: 13 of 77

Roger hi,
thanks a lot for your help but i am afraid something is
still missing. i tried the formulae on the same example of
a = [1 1]and b = [0 -1] (both passing through origin).
the answer i got is 315 instead of 225.
sorry i am taking a lot of your time :)
thanks
salih

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
wrote in message <fjm4u4$i8o$1@fred.mathworks.com>...
> "salih tuna" <salihtuna@gmail.com> wrote in message
<fjlrpl$gii
> $1@fred.mathworks.com>...
> > Hi,
> > thanks a lot for your reply. yes they are in 2d, sorry i
> > forgot to mention.
> > i tried to apply the formula but i am getting wrong result.
> > for example i want to calculate the angle between a = [1 1]
> > and b = [0 -1] which is 225 degrees. with this formulae i
> > got 243.4. i couldn't see where i am doing the mistake.
> > thanks a lot in advance
> > salih
> >
> > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> > wrote in message <fjk0tg$jli$1@fred.mathworks.com>...
> > > angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi
> --------
> I certainly owe you an apology, Salih. That formula I
gave you is very, very
> wrong. I can't imagine what I was thinking about when I
wrote it. Chalk it up
> to momentary insanity! :-) The correct computation
should be as follows.
>
> Assuming a = [x1,y1] and b = [x2,y2] are two vectors
with their bases at the
> origin, the non-negative angle between them measured
counterclockwise
> from a to b is given by
>
> angle = mod(atan2(x1*y2-x2*y1,x1*x2+y1*y2),2*pi);
>
> As you can see, this bears a close relationship to the
three-dimensional
> formula I wrote last July 10. The quantities, x1*y2-x2*y1
and x1*x2+y1*y2
> are, respectively, the sine and cosine of the
counterclockwise angle from
> vector a to vector b, multiplied by the product of their
norms - that is, their
> cross product and the dot product restricted to two
dimensions. The 'atan2'
> function then gives the angle between them ranging from
-pi to +pi, and the
> 'mod' operation changes this so as to range from 0 to
2*pi, as you requested.
>
> Roger Stafford
>
>

Subject: Angle between two vectors

From: Roger Stafford

Date: 11 Dec, 2007 19:32:07

Message: 14 of 77

"salih tuna" <salihtuna@gmail.com> wrote in message <fjmabs$9fh
$1@fred.mathworks.com>...
> Roger hi,
> thanks a lot for your help but i am afraid something is
> still missing. i tried the formulae on the same example of
> a = [1 1]and b = [0 -1] (both passing through origin).
> the answer i got is 315 instead of 225.
> sorry i am taking a lot of your time :)
> thanks
> salih
-----
Hi Salih. I get 225 degrees for that example with a = [1,1] and b = [0,-1],
which is correct. Are you sure you didn't have x2 and y1 interchanged by
mistake? That would get you an erroneous answer of 315. You should have x1
= 1, y1 = 1, x2 = 0, and y2 = -1.

Roger Stafford

Subject: Angle between two vectors

From: baris kazar

Date: 28 Dec, 2007 16:11:38

Message: 15 of 77

"Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <fjk0tg$jli$1@fred.mathworks.com>...
> "salih tuna" <salihtuna@gmail.com> wrote in message
<fjj9nj$fia
> $1@fred.mathworks.com>...
> > hello,
> > how can i calculate the angles so that they are in the
range 0-360 degrees?
> > thanks
> > salih
> >
> > "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote in
message
> > <ef5ce9c.-1@webcrossing.raydaftYaTP>...
> > > How do I find the angle between two unit vectors a
and b? I know I
> > > can find cosine theta by the following formula:
> > >
> > > theta = acos(dot(a,b));
> > >
> > > However, how do I know whether the angle is actually
theta, or -theta
> > > or pi-theta or pi+theta??
> > >
> > > Notice that the vectors are in three dimension (3d).
> > >
> > > Thanks,
> > > -YM
> --------
> Y Mehta's question involved angles between vectors in
three-dimensional
> space. I can think of no reasonable definition for a
canonical angle between
> such vectors which ranges from 0 to 360 degrees (0 to
2*pi radians.)
>
> However, if you are in two-dimensional space, then you
can speak of the
> non-negative angle measured counterclockwise from vector
a to vector b,
> and this would give the range you have requested. If a
= [x1,y1] and b =
> [x2,y2], then such an angle is given in matlab by:
>
> angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to
2*pi radians
>
> (Multiply this answer by 180/pi to get degrees.)
>
> Roger Stafford

Hi,-
 how can we generalize this to 3-D vectors? Think of a
plane on 3-D space and you have vectors on this plane. I
wanna know the angle between 2 vectors in the range 0-2pi.
or at least -pi to pi. I have 3 vectors. One (First)
vector is the same all the time. I wanna know the relative
positions of the other two vector wrt the firsy one. Thus,
i need angles in the range 0 to 2pi or -pi to pi.
Thanks in advance

Subject: Angle between two vectors

From: Bruno Luong

Date: 28 Dec, 2007 18:22:57

Message: 16 of 77

"baris kazar" <mbkazar.nospam@gmail.com> wrote in message
<fl377q$4ip$1@fred.mathworks.com>...

>
> Hi,-
> how can we generalize this to 3-D vectors? Think of a
> plane on 3-D space and you have vectors on this plane. I
> wanna know the angle between 2 vectors in the range 0-2pi.

The problem is depends on where you look (from above or from
below the plane), you will see the angle between vectors on
this plane reverse the sign (draw them on a transparent
glass and try to look from both sides). In 3D there is
nothing that could tell us whereas looking from one way or
from another is more "correct".

Think like a fish or a bird, not like a man.

Bruno

Subject: Angle between two vectors

From: baris kazar

Date: 28 Dec, 2007 18:50:50

Message: 17 of 77

"Bruno Luong" <b.luong@fogale.fr> wrote in message <fl3eu1
$d3$1@fred.mathworks.com>...
> "baris kazar" <mbkazar.nospam@gmail.com> wrote in message
> <fl377q$4ip$1@fred.mathworks.com>...
>
> >
> > Hi,-
> > how can we generalize this to 3-D vectors? Think of a
> > plane on 3-D space and you have vectors on this plane.
I
> > wanna know the angle between 2 vectors in the range 0-
2pi.
>
> The problem is depends on where you look (from above or
from
> below the plane), you will see the angle between vectors
on
> this plane reverse the sign (draw them on a transparent
> glass and try to look from both sides). In 3D there is
> nothing that could tell us whereas looking from one way
or
> from another is more "correct".
>
> Think like a fish or a bird, not like a man.
>
> Bruno
>

Hi Bruno,
 Thanks, but i partially dont agree with you. If you
choose a reference of point which the start point of the
first vector, it is not important for me whether you look
from above or below the plane. I need relative positions
but not the absolute positions. I think i indicated this
above.
Anyways, if someone comes up with an idea, please post
here. I am very curious about a solution.
Best regards

Subject: Angle between two vectors

From: Roger Stafford

Date: 28 Dec, 2007 20:36:34

Message: 18 of 77

"baris kazar" <mbkazar.nospam@gmail.com> wrote in message <fl377q$4ip
$1@fred.mathworks.com>...
> Hi,-
> how can we generalize this to 3-D vectors? Think of a
> plane on 3-D space and you have vectors on this plane. I
> wanna know the angle between 2 vectors in the range 0-2pi.
> or at least -pi to pi. I have 3 vectors. One (First)
> vector is the same all the time. I wanna know the relative
> positions of the other two vector wrt the firsy one. Thus,
> i need angles in the range 0 to 2pi or -pi to pi.
> Thanks in advance
---------
  As Bruno has pointed out, the angle between two three-dimensional vectors
depends on which sense one gives to a vector orthogonal to their plane. It
isn't clear what you meant by, "the relative positions of the other two vector
wrt the firsy one." The vector cross product of the second two vectors will be
a vector orthogonal to their plane. Perhaps you mean that the angle between
them is to be considered positive if this cross product lies on the same side
of the plane as this first vector, and negative otherwise. If that is the case,
then let your first, second, and third vectors be designated as x, y, and z,
respectively. A matlab formula for calculating the angle between y and z will
then be:

 c = cross(y,z);
 angleyz = sign(dot(x,c))*atan2(norm(c),dot(y,z));

The value of 'angleyz' will range from -pi to +pi. If you want it to range from
0 to 2*pi, then apply the 'mod' function as I did on Dec. 11 in this thread.

Roger Stafford

Subject: Angle between two vectors

From: baris kazar

Date: 28 Dec, 2007 20:50:02

Message: 19 of 77

"Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <fl3moi$pvc$1@fred.mathworks.com>...
> "baris kazar" <mbkazar.nospam@gmail.com> wrote in
message <fl377q$4ip
> $1@fred.mathworks.com>...
> > Hi,-
> > how can we generalize this to 3-D vectors? Think of a
> > plane on 3-D space and you have vectors on this plane.
I
> > wanna know the angle between 2 vectors in the range 0-
2pi.
> > or at least -pi to pi. I have 3 vectors. One (First)
> > vector is the same all the time. I wanna know the
relative
> > positions of the other two vector wrt the firsy one.
Thus,
> > i need angles in the range 0 to 2pi or -pi to pi.
> > Thanks in advance
> ---------
> As Bruno has pointed out, the angle between two three-
dimensional vectors
> depends on which sense one gives to a vector orthogonal
to their plane. It
> isn't clear what you meant by, "the relative positions
of the other two vector
> wrt the firsy one." The vector cross product of the
second two vectors will be
> a vector orthogonal to their plane. Perhaps you mean
that the angle between
> them is to be considered positive if this cross product
lies on the same side
> of the plane as this first vector, and negative
otherwise. If that is the case,
> then let your first, second, and third vectors be
designated as x, y, and z,
> respectively. A matlab formula for calculating the
angle between y and z will
> then be:
>
> c = cross(y,z);
> angleyz = sign(dot(x,c))*atan2(norm(c),dot(y,z));
>
> The value of 'angleyz' will range from -pi to +pi. If
you want it to range from
> 0 to 2*pi, then apply the 'mod' function as I did on
Dec. 11 in this thread.
>
> Roger Stafford
>
Hi Roger,-
  yes, this is one step closer to what i need but not
exactly. Let's take a numeric example:
x=(1,0,0); y=(1,0,1) and z=(1,0-1)
let's call the angle between x and y theta.
Then i wanna get 2pi-theta for the angle between x and z.
i dont have access y and z at the same time.
hope that this problem statement is clear.
Thanks much for your reply
Best regards

Subject: Angle between two vectors

From: Bruno Luong

Date: 28 Dec, 2007 21:38:03

Message: 20 of 77

"baris kazar" <mbkazar.nospam@gmail.com> wrote in message
> yes, this is one step closer to what i need but not
> exactly. Let's take a numeric example:
> x=(1,0,0); y=(1,0,1) and z=(1,0-1)
> let's call the angle between x and y theta.
> Then i wanna get 2pi-theta for the angle between x and z.
> i dont have access y and z at the same time.
> hope that this problem statement is clear.

Assuming a human being (e.g., all the birds, or you) can go
anywhere in R3, and can look in any direction (a fair
assumption isn't it?).

VIEW1: Goto the point (0,-1,0) look toward the direction
(0,1,0): you see angle(x,y)=-pi/4, and angle(x,z)=pi/4.

VIEW2: Goto the point (0,+1,0) look toward the direction
(0,-1,0): you see angle(x,y)=+pi/4, and angle(x,z)=-pi/4.

In both cases you have angle(x,y) = -angle(x,z) modulo 2*pi,
as your wish expressed above with "theta".

BUT
CASE1/VIEW1: angle(x,y)=-pi/4; angle(x,z)=pi/4; angle(y,z)=pi/2.

CASE2/VIEW2: angle(x,y)=+pi/4; angle(x,z)=-pi/4;
angle(y,z)=-pi/2.
 
ALL angles change the sign when you change the side. You
simply cannot decide which is a *right* way, because the
opposite is also right as much.

THERE IS NO UNIQUE WAY TO DEFINE ANGLE IN R3.

Bruno

Subject: Angle between two vectors

From: Roger Stafford

Date: 28 Dec, 2007 21:52:57

Message: 21 of 77

"baris kazar" <mbkazar.nospam@gmail.com> wrote in message <fl3nhq$3tc
$1@fred.mathworks.com>...
> Hi Roger,-
> yes, this is one step closer to what i need but not
> exactly. Let's take a numeric example:
> x=(1,0,0); y=(1,0,1) and z=(1,0-1)
> let's call the angle between x and y theta.
> Then i wanna get 2pi-theta for the angle between x and z.
> i dont have access y and z at the same time.
> hope that this problem statement is clear.
> Thanks much for your reply
> Best regards
--------
  You will have to try harder to explain your problem, if I am to understand
you, Baris. The example you gave has x, y, and z all in the same plane. You
didn't state previously that all your vectors are coplanar. Are they? But
whether they are or not, this doesn't explain how you would define the angle
theta between x and y. It could be plus pi/4 or it could be minus pi/4 (or
7/4*pi.) Which one would you choose and according to what criterion? It
would depend on which side of the plane, in this case the x-z plane, is
regarded as its positive side - along the plus y-axis, or along the negative
side of the y-axis. Also it depends on whether you are moving from x
towards y or from y towards x if you are adhering to right-hand cross
product direction conventions.

  Select two arbitrary vectors in three-dimensional space (x1,y1,z1) and
(x2,y2,z2) and try to think of a consistent way of defining the angle between
them without reference to any other vector that would allow this quantity to
range over the full four quadrants, 0 to 2*pi. I think you will find this a
difficult thing to do in any way that could reasonably be considered canonical.
In two dimensions, there is a clearly defined counterclockwise direction from
vector x to vector y which would give you the range you desire. In three
dimensions, you lose the sense of what is a "counterclockwise" direction. You
can go from x to y along either of two great circle paths and one direction will
give the supplement angle to the opposite direction. If you always select the
shortest path, then your angle range is restricted to [0,pi].

  If one vector of each pair is restricted to a particular fixed vector, as your
previous wordage seemed to imply, I still don't see what criterion you wish to
use to define these angles. For example, you can move from the fixed vector
by one degree in all possible directions giving a cone, but which half of these
angles should be adjusted so as to be the supplements (that is 359 degrees,)
of those on the opposite side? If you are restricting your vectors to all be
coplanar, then which side of such a plane is to be considered its "positive"
side?

Roger Stafford

Subject: Angle between two vectors

From: baris kazar

Date: 29 Dec, 2007 05:47:16

Message: 22 of 77

"Bruno Luong" <b.luong@fogale.fr> wrote in message
<fl3qbq$l6j$1@fred.mathworks.com>...
> THERE IS NO UNIQUE WAY TO DEFINE ANGLE IN R3.
>
> Bruno

Hi Bruno,
i did not say THERE IS.
Best regards,

Subject: Angle between two vectors

From: baris kazar

Date: 29 Dec, 2007 05:49:03

Message: 23 of 77

"Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <fl3r7p$5nt$1@fred.mathworks.com>...
> You will have to try harder to explain your problem,
if I am to understand
> you, Baris. The example you gave has x, y, and z all in
the same plane. You
> didn't state previously that all your vectors are
coplanar. Are they? But

I think i did:
> > Hi,-
> > how can we generalize this to 3-D vectors? Think of a
> > plane on 3-D space and you have vectors on this plane.
I
> > wanna know the angle between 2 vectors in the range 0-
2pi.

I think i might not have done a good in explaining the
problem. Sorry about that.
Best regards,

Subject: Angle between two vectors

From: baris kazar

Date: 29 Dec, 2007 05:53:08

Message: 24 of 77

"Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <fl3r7p$5nt$1@fred.mathworks.com>...
> "baris kazar" <mbkazar.nospam@gmail.com> wrote in
message <fl3nhq$3tc
> $1@fred.mathworks.com>...
> > Hi Roger,-
> > yes, this is one step closer to what i need but not
> > exactly. Let's take a numeric example:
> > x=(1,0,0); y=(1,0,1) and z=(1,0-1)
> > let's call the angle between x and y theta.
> > Then i wanna get 2pi-theta for the angle between x and
z.
> > i dont have access y and z at the same time.
> > hope that this problem statement is clear.
> > Thanks much for your reply
> > Best regards
> --------
> You will have to try harder to explain your problem,
if I am to understand
> you, Baris. The example you gave has x, y, and z all in
the same plane. You
> didn't state previously that all your vectors are
coplanar. Are they? But
> whether they are or not, this doesn't explain how you
would define the angle
> theta between x and y. It could be plus pi/4 or it
could be minus pi/4 (or
> 7/4*pi.) Which one would you choose and according to
what criterion? It
> would depend on which side of the plane, in this case
the x-z plane, is
> regarded as its positive side - along the plus y-axis,
or along the negative
> side of the y-axis. Also it depends on whether you are
moving from x
> towards y or from y towards x if you are adhering to
right-hand cross
> product direction conventions.
>
> Select two arbitrary vectors in three-dimensional
space (x1,y1,z1) and
> (x2,y2,z2) and try to think of a consistent way of
defining the angle between
> them without reference to any other vector that would
allow this quantity to
> range over the full four quadrants, 0 to 2*pi. I think
you will find this a
> difficult thing to do in any way that could reasonably
be considered canonical.
> In two dimensions, there is a clearly defined
counterclockwise direction from
> vector x to vector y which would give you the range you
desire. In three
> dimensions, you lose the sense of what is
a "counterclockwise" direction. You
> can go from x to y along either of two great circle
paths and one direction will
> give the supplement angle to the opposite direction. If
you always select the
> shortest path, then your angle range is restricted to
[0,pi].
>
> If one vector of each pair is restricted to a
particular fixed vector, as your
> previous wordage seemed to imply, I still don't see what
criterion you wish to
> use to define these angles. For example, you can move
from the fixed vector
> by one degree in all possible directions giving a cone,
but which half of these
> angles should be adjusted so as to be the supplements
(that is 359 degrees,)
> of those on the opposite side? If you are restricting
your vectors to all be
> coplanar, then which side of such a plane is to be
considered its "positive"
> side?
>
> Roger Stafford
>

Hi Roger,-
 these are excellent questions.
one can choose to look at the plane from +ive infinity
(above) or from -ive infinity (below).

I know that in 3D there is no notion of CW or CCW. However,
you can define it locally wrt a point/vector as long as
they are on a plane by defining from which side you are
looking at.

Anyways, i dont wanna confuse anyone any more.
I will find another solution to this problem.
Best regards

Subject: Angle between two vectors

From: Bruno Luong

Date: 29 Dec, 2007 09:16:29

Message: 25 of 77

"baris kazar" <mbkazar.nospam@gmail.com> wrote in message
<fl4nc4$47i$1@fred.mathworks.com>...

>
> However,
> you can define it locally wrt a point/vector as long as
> they are on a plane by defining from which side you are
> looking at.

That's a good way. Pick a side.

That's easy.

1. Select two arbitrary vectors in the set, e.g., the first
(U) and the second (V) vectors, assumming they are all unit
vectors. Normalize them if they aren't.

2. Next compute the cross product N = U x V. This gives you
a normal vector N pointing ONE side of the plane.

3. Take a cross product W = N x U. Now you have {U,W} which
is a orthonormal basis vectors of the plane. In other word,
Span(U,V) = Span(U,W), <U,W>=0, |U|=1, |W|=1.

4. Compute the angle as of any vector Z (in the plane as
following):

theta = atan2(<W.Z>,<U.Z>) % <- Do not change U or W

This gives you ONE consistent way to define angle (only for
vectors belong to the plane).

PS: In a more abstract way, you can also accomplish steps
1->3 by using Gram-Smidth orthogonalization, or QR (help qr).

Bruno

Subject: Angle between two vectors

From: Bruno Luong

Date: 29 Dec, 2007 09:46:12

Message: 26 of 77

"Bruno Luong" <b.luong@fogale.fr> wrote in message
<fl539d$np9$1@fred.mathworks.com>...
> "baris kazar" <mbkazar.nospam@gmail.com> wrote in message
> <fl4nc4$47i$1@fred.mathworks.com>...
>
> >
> > However,
> > you can define it locally wrt a point/vector as long as
> > they are on a plane by defining from which side you are
> > looking at.
>
> That's a good way. Pick a side.
>
> That's easy.
>
> 1. Select two arbitrary vectors in the set, e.g., the first
> (U) and the second (V) vectors, assumming they are all unit
> vectors. Normalize them if they aren't.
>
> 2. Next compute the cross product N = U x V. This gives you
> a normal vector N pointing ONE side of the plane.
>
> 3. Take a cross product W = N x U. Now you have {U,W} which
> is a orthonormal basis vectors of the plane. In other word,
> Span(U,V) = Span(U,W), <U,W>=0, |U|=1, |W|=1.
>
> 4. Compute the angle as of any vector Z (in the plane as
> following):
>
> theta = atan2(<W.Z>,<U.Z>) % <- Do not change U or W
>
> This gives you ONE consistent way to define angle (only for
> vectors belong to the plane).
>
> PS: In a more abstract way, you can also accomplish steps
> 1->3 by using Gram-Smidth orthogonalization, or QR (help qr).
>
> Bruno
>

U=[1 0 0]';
V=[1 0 1]'; % example above

[Q R]=qr([U V],0);
angle=@(v) atan2(dot(Q(:,2),v),dot(Q(:,1),v));

>> angle(U)

     0

>> angle(V)

   -0.7854

>> angle([1 0 -1])

    0.7854

Bruno

Subject: Angle between two vectors

From: Uzair Siddiqui

Date: 12 Apr, 2009 17:03:01

Message: 27 of 77

Hi

I am just stuck in calculating angle between two points in Flash.

I am using this

angle =Math.abs(Math.atan2(x-x2,y-y2) * 180/Math.PI) ;

But I am getting the angles between 0-180 instead of 0-360.

Can you please help.

I need it badly.

Thanks

Subject: Angle between two vectors

From: Roger Stafford

Date: 12 Apr, 2009 19:35:01

Message: 28 of 77

"Uzair Siddiqui" <uzair@meragsm.com> wrote in message <grt6s5$2hh$1@fred.mathworks.com>...
> I am just stuck in calculating angle between two points in Flash.
> I am using this
> angle =Math.abs(Math.atan2(x-x2,y-y2) * 180/Math.PI) ;
> But I am getting the angles between 0-180 instead of 0-360.

  When you say "angle between two points", I assume here you mean the angle measured counterclockwise from the positive x-axis around to the vector pointing from point P2 = (x2,y2) to point P = (x,y), creating an angle anywhere from 0 to 2*pi radians or 360 degrees. Is that what you mean?

  If so, it can be obtained in degrees with matlab by

 angle = 180/pi*mod(atan2(y-y2,x-x2),2*pi);

which lies between 0 and 360 degrees. (I presume this formula remains valid in Flash.)

  In your formula the arguments x-x2 and y-y2 are interchanged from this and the answer would lie between -180 and +180 without the 'mod' operation.

Roger Stafford

Subject: Angle between two vectors

From: Uzair Siddiqui

Date: 13 Apr, 2009 04:16:01

Message: 29 of 77

Thanks A Lot Roger

But What is "mod" function doing in Matlab ?

I hope it just return the remainder after division ?

Another Issue i am facing is that the result is clockwise, but i want anticlockwise angle.

Thanks



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <grtfp5$oal$1@fred.mathworks.com>...
> "Uzair Siddiqui" <uzair@meragsm.com> wrote in message <grt6s5$2hh$1@fred.mathworks.com>...
> > I am just stuck in calculating angle between two points in Flash.
> > I am using this
> > angle =Math.abs(Math.atan2(x-x2,y-y2) * 180/Math.PI) ;
> > But I am getting the angles between 0-180 instead of 0-360.
>
> When you say "angle between two points", I assume here you mean the angle measured counterclockwise from the positive x-axis around to the vector pointing from point P2 = (x2,y2) to point P = (x,y), creating an angle anywhere from 0 to 2*pi radians or 360 degrees. Is that what you mean?
>
> If so, it can be obtained in degrees with matlab by
>
> angle = 180/pi*mod(atan2(y-y2,x-x2),2*pi);
>
> which lies between 0 and 360 degrees. (I presume this formula remains valid in Flash.)
>
> In your formula the arguments x-x2 and y-y2 are interchanged from this and the answer would lie between -180 and +180 without the 'mod' operation.
>
> Roger Stafford

Subject: Angle between two vectors

From: Roger Stafford

Date: 13 Apr, 2009 05:03:34

Message: 30 of 77

"Uzair Siddiqui" <uzair@meragsm.com> wrote in message <gruea1$nep$1@fred.mathworks.com>...
> But What is "mod" function doing in Matlab ?
> I hope it just return the remainder after division ?
> Another Issue i am facing is that the result is clockwise, but i want anticlockwise angle.

  No, you are mistaken, Uzair. The formula I gave you gives the counterclockwise angle, provided of course that the x-y axes are oriented in the conventional manner with the positive y-axis being ninety degrees counterclockwise from the positive x-axis. Try it out and see.

  For example, let P = (x,y) = (1,1) and P2 = (x2,y2) = (0,0). The vector from P2 to P is clearly rotated 45 degrees counterclockwise from the x-axis toward the y-axis. The result of the formula is also 45 degrees.

  For angles from 'atan2' between 0 and pi radians, the 'mod' operator here leaves them unchanged, but those from -pi to 0 have 2*pi added to place them in the range from pi to 2*pi, as per your request. In degrees that means the answer will range from 0 to 360 degrees.

Roger Stafford

Subject: Angle between two vectors

From: Uzair Siddiqui

Date: 13 Apr, 2009 07:17:02

Message: 31 of 77

Thanks again Roger.

What I am doing is that i am adding 2*Math.PI to the angle if it is negative.

In this way I am getting the 0-360 angle but it is clockwise.

Can you please explain the mod function, or provide an alternative to this in c++ or java, as that will be more undestandable to me.

Regards
Uzair
Pakistan

Subject: Angle between two vectors

From: Cato

Date: 23 Oct, 2009 17:03:04

Message: 32 of 77

Thank you very much for posting these messages, Mr. Stafford. These definitely helped!!

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <grtfp5$oal$1@fred.mathworks.com>...
> "Uzair Siddiqui" <uzair@meragsm.com> wrote in message <grt6s5$2hh$1@fred.mathworks.com>...
> > I am just stuck in calculating angle between two points in Flash.
> > I am using this
> > angle =Math.abs(Math.atan2(x-x2,y-y2) * 180/Math.PI) ;
> > But I am getting the angles between 0-180 instead of 0-360.
>
> When you say "angle between two points", I assume here you mean the angle measured counterclockwise from the positive x-axis around to the vector pointing from point P2 = (x2,y2) to point P = (x,y), creating an angle anywhere from 0 to 2*pi radians or 360 degrees. Is that what you mean?
>
> If so, it can be obtained in degrees with matlab by
>
> angle = 180/pi*mod(atan2(y-y2,x-x2),2*pi);
>
> which lies between 0 and 360 degrees. (I presume this formula remains valid in Flash.)
>
> In your formula the arguments x-x2 and y-y2 are interchanged from this and the answer would lie between -180 and +180 without the 'mod' operation.
>
> Roger Stafford

Subject: Angle between two vectors

From: duachuot Son

Date: 10 Jan, 2010 18:21:17

Message: 33 of 77

Hi Roger,

Could you please explaing to me why using atan2 for the angle from positive x-axix to this coordinate (cosine,sine) is the answer?

I believe it has something to do with the unit circle but can't figure it out. In my understanding, the cosine will still compute the small angle. Suppose sine will do the same too.

Thanks a lot,
Son.


> > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> > wrote in message <fjk0tg$jli$1@fred.mathworks.com>...
> > > angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi
> --------
> I certainly owe you an apology, Salih. That formula I gave you is very, very
> wrong. I can't imagine what I was thinking about when I wrote it. Chalk it up
> to momentary insanity! :-) The correct computation should be as follows.
>
> Assuming a = [x1,y1] and b = [x2,y2] are two vectors with their bases at the
> origin, the non-negative angle between them measured counterclockwise
> from a to b is given by
>
> angle = mod(atan2(x1*y2-x2*y1,x1*x2+y1*y2),2*pi);
>
> As you can see, this bears a close relationship to the three-dimensional
> formula I wrote last July 10. The quantities, x1*y2-x2*y1 and x1*x2+y1*y2
> are, respectively, the sine and cosine of the counterclockwise angle from
> vector a to vector b, multiplied by the product of their norms - that is, their
> cross product and the dot product restricted to two dimensions. The 'atan2'
> function then gives the angle between them ranging from -pi to +pi, and the
> 'mod' operation changes this so as to range from 0 to 2*pi, as you requested.
>
> Roger Stafford
>
>

Subject: Angle between two vectors

From: Benjamin McCrite

Date: 24 Feb, 2010 00:41:05

Message: 34 of 77

 Greg Heath <heath@alumni.brown.edu> wrote in message <1184052626.324470.175540@n2g2000hse.googlegroups.com>...
> On Jul 9, 5:16 pm, "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote:
> > How do I find the angle between two unit vectors a and b? I know I
> > can find cosine theta by the following formula:
> >
> > theta = acos(dot(a,b));
> >
>
> Invalid since it is possible that abs(dot(a,b)) > 1.
>
> costheta = dot(a,b)/(norm(a)*norm(b));
> theta = acos(costheta);
>
> will give you the anser in the interval [0,pi].
>
> > However, how do I know whether the angle is
> > actually theta, or -theta or pi-theta or pi+theta??
>
> Angles between vectors only lie in the interval [0,pi].
>
> > Notice that the vectors are in three dimension (3d).
>
> Dimensionality of the original space is irrelevant. As long as
> norm(a)*norm(b) > 0, the vectors uniquely define a 2-d space when
> dot(a,b) ~= 0 and a unique 1-d space otherwise.
>
> Hope this helps.
>
> Greg
>


Can you tell me what norm(),cross() and dot() do?

Subject: Angle between two vectors

From: Nathan

Date: 24 Feb, 2010 00:46:13

Message: 35 of 77

On Feb 23, 4:41 pm, "Benjamin McCrite" <dragonmast...@hotmail.com>
wrote:
>  Greg Heath <he...@alumni.brown.edu> wrote in message <1184052626.324470.175...@n2g2000hse.googlegroups.com>...
>
>
>
> > On Jul 9, 5:16 pm, "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote:
> > > How do I find the angle between two unit vectors a and b? I know I
> > > can find cosine theta by the following formula:
>
> > > theta = acos(dot(a,b));
>
> > Invalid since it is possible that abs(dot(a,b)) > 1.
>
> > costheta = dot(a,b)/(norm(a)*norm(b));
> > theta = acos(costheta);
>
> > will give you the anser in the interval [0,pi].
>
> > > However, how do I know whether the angle is
> > > actually theta, or -theta or pi-theta or pi+theta??
>
> > Angles between vectors only lie in the interval [0,pi].
>
> > > Notice that the vectors are in three dimension (3d).
>
> > Dimensionality of the original space is irrelevant. As long as
> > norm(a)*norm(b) > 0, the vectors uniquely define a 2-d space when
> > dot(a,b) ~= 0 and a unique 1-d space otherwise.
>
> > Hope this helps.
>
> > Greg
>
> Can you tell me what norm(),cross() and dot() do?

Can you read the documentation?
Norm:
The norm of a matrix is a scalar that gives some measure of the
magnitude of the elements of the matrix. The norm function calculates
several different types of matrix norms:

n = norm(A) returns the largest singular value of A, max(svd(A)).

n = norm(A,p) returns a different kind of norm, depending on the value
of p.
...


Cross:
C = cross(A,B) returns the cross product of the vectors A and B. That
is, C = A x B. A and B must be 3-element vectors. If A and B are
multidimensional arrays, cross returns the cross product of A and B
along the first dimension of length 3.

C = cross(A,B,dim) where A and B are multidimensional arrays, returns
the cross product of A and B in dimension dim. A and B must have the
same size, and both size(A,dim) and size(B,dim) must be 3.
...


Dot:
C = dot(A,B) returns the scalar product of the vectors A and B. A and
B must be vectors of the same length. When A and B are both column
vectors, dot(A,B) is the same as A'*B.

For multidimensional arrays A and B, dot returns the scalar product
along the first non-singleton dimension of A and B. A and B must have
the same size.

C = dot(A,B,dim) returns the scalar product of A and B in the
dimension dim.
...


Before asking what functions do, try to read and understand the
documentation for them. To view the documentation, you can type:
doc FUNCTIONNAME

Where, in this case, FUNCTIONNAME is either cross, dot, or norm

-Nathan

Subject: Angle between two vectors

From: Beho nashed

Date: 16 Jun, 2010 23:43:04

Message: 36 of 77

Nathan <ngreco32@gmail.com> wrote in message <8db784bf-a96e-431b-b5c6-d6836905d350@o16g2000prh.googlegroups.com>...
> On Feb 23, 4:41 pm, "Benjamin McCrite" <dragonmast...@hotmail.com>
> wrote:
> >  Greg Heath <he...@alumni.brown.edu> wrote in message <1184052626.324470.175...@n2g2000hse.googlegroups.com>...

Hello Nathan,
I am trying to calculate the angle between two lines, I found this topic, and I have a quick question.
Here's my code:
I = imread('angle.jpg');
figure, imshow(I);
[xp, yp] = ginput;
line(xp,yp,'color',[0 1 0]);
cal_angle = atan2(norm(cross(xp,yp)),dot(xp,yp))
cal_angle2 = acosd(dot(xp,yp)/(norm(xp)*norm(yp)))

I found those two equations online, but I don't think they're right. the first one should be right, I remember it from calculus!

I select three points, get two lines. the angle should be greater than 90, but it always comes smaller than 90!!!
I am not sure, what am I doing wrong here?

Thanks

Subject: Angle between two vectors

From: Beho nashed

Date: 16 Jun, 2010 23:43:04

Message: 37 of 77

Nathan <ngreco32@gmail.com> wrote in message <8db784bf-a96e-431b-b5c6-d6836905d350@o16g2000prh.googlegroups.com>...
> On Feb 23, 4:41 pm, "Benjamin McCrite" <dragonmast...@hotmail.com>
> wrote:
> >  Greg Heath <he...@alumni.brown.edu> wrote in message <1184052626.324470.175...@n2g2000hse.googlegroups.com>...

Hello Nathan,
I am trying to calculate the angle between two lines, I found this topic, and I have a quick question.
Here's my code:
I = imread('angle.jpg');
figure, imshow(I);
[xp, yp] = ginput;
line(xp,yp,'color',[0 1 0]);
cal_angle = atan2(norm(cross(xp,yp)),dot(xp,yp))
cal_angle2 = acosd(dot(xp,yp)/(norm(xp)*norm(yp)))

I found those two equations online, but I don't think they're right. the first one should be right, I remember it from calculus!

I select three points, get two lines. the angle should be greater than 90, but it always comes smaller than 90!!!
I am not sure, what am I doing wrong here?

Thanks

Subject: Angle between two vectors

From: Roger Stafford

Date: 17 Jun, 2010 00:47:03

Message: 38 of 77

"Beho nashed" <beho86@yahoo.com> wrote in message <hvbni8$s0h$1@fred.mathworks.com>...
> I am trying to calculate the angle between two lines, I found this topic, and I have a quick question.
> Here's my code:
> I = imread('angle.jpg');
> figure, imshow(I);
> [xp, yp] = ginput;
> line(xp,yp,'color',[0 1 0]);
> cal_angle = atan2(norm(cross(xp,yp)),dot(xp,yp))
> cal_angle2 = acosd(dot(xp,yp)/(norm(xp)*norm(yp)))
>
> I found those two equations online, but I don't think they're right. the first one should be right, I remember it from calculus!
>
> I select three points, get two lines. the angle should be greater than 90, but it always comes smaller than 90!!!
> I am not sure, what am I doing wrong here?
>
> Thanks
- - - - - - - - - -
  Based on your statement, "I select three points", it looks as though you are entering three quantities each for xp and yp. Unfortunately the 'cross' function which requires vectors with three components interprets each of these as a three-dimensional space vector and not three points in two-dimensional space. The same holds for 'dot' in this case. That means both your angles are wrong.

  If you have three points P1 = (x1,y1), P2 = (x2,y2), and P3 = (x3,y3) in two dimensional space, the inner angle at P1 between lines P2P1 and P3P1 is given by

 atan2(abs(det([x1,x2,x3;y1,y2,y3;1,1,1])),(x2-x1)*(x3-x1)+(y2-y1)*(y3-y1))

The angle from atan2 is given in radians.

Roger Stafford

Subject: Angle between two vectors

From: Beho nashed

Date: 17 Jun, 2010 01:00:23

Message: 39 of 77

Hello Roger,
Thanks, so to use this equation, I should change my code to :
[x,y] = ginput;

angle = 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)))

I am not sure, I meant x(1) so it will use first value in array x, am I right?

Thanks,

Subject: Angle between two vectors

From: Roger Stafford

Date: 17 Jun, 2010 02:55:10

Message: 40 of 77

"Beho nashed" <beho86@yahoo.com> wrote in message <hvbs37$a30$1@fred.mathworks.com>...
> Hello Roger,
> Thanks, so to use this equation, I should change my code to :
> [x,y] = ginput;
>
> angle = 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)))
>
> I am not sure, I meant x(1) so it will use first value in array x, am I right?
>
> Thanks,
- - - - - - - - -
  Yes, that looks right if you make sure to enter the vertex of the angle you are finding first so that it goes into P1 = [x(1),y(1)], then followed by the other two points on the two respective lines.

  For brevity you can write

 P1 = [x(1),y(1)]; % The point at the angle vertex
 P2 = [x(2),y(2)];
 P3 = [x(3),y(3)];

 ang = atan2(abs(det(P2-P1;P3-P1])),dot(P2-P1,P3-P1));

It will give you the same answer (in radians.)

Don't use the word 'angle' for your angle variable. It is a reserved word in matlab and the confusion could cause errors in your program when you least expect it.

Roger Stafford

Subject: Angle between two vectors

From: Beho nashed

Date: 18 Jun, 2010 01:49:05

Message: 41 of 77

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hvc2qe$59b$1@fred.mathworks.com>...
> "Beho nashed" <beho86@yahoo.com> wrote in message <hvbs37$a30$1@fred.mathworks.com>...
> > Hello Roger,
> > Thanks, so to use this equation, I should change my code to :
> > [x,y] = ginput;
> >
> > angle = 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)))
> >
> > I am not sure, I meant x(1) so it will use first value in array x, am I right?
> >
> > Thanks,
> - - - - - - - - -

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!!!

http://drop.io/rlv072i (image 3, called test)
I pick three points, two points on the surface (on the left side) and the third on the slope ( the white one), making two lines with an angle bigger than 90 deg!

Thanks in advance Roger

Subject: Angle between two vectors

From: Roger Stafford

Date: 18 Jun, 2010 04:38:03

Message: 42 of 77

"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

Subject: Angle between two vectors

From: Beho nashed

Date: 18 Jun, 2010 14:41:07

Message: 43 of 77

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hvet7b$b6c$1@fred.mathworks.com>...
> "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!!!


Hello Roger,
I actually noticed the bracket and fixed it. I just noticed something in the image displayed. the Y-axis starts from the top!!!

I tried to use set(gca,'ydir','normal') , but it flips the picture and the axis, not just the axis. I am not sure how to flip the axis only, I believe that's the problem.

Thanks

Subject: Angle between two vectors

From: us

Date: 18 Jun, 2010 15:55:07

Message: 44 of 77

"Beho nashed" <beho86@yahoo.com> wrote in message <hvg0i3$khr$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hvet7b$b6c$1@fred.mathworks.com>...
> > "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!!!
>
>
> Hello Roger,
> I actually noticed the bracket and fixed it. I just noticed something in the image displayed. the Y-axis starts from the top!!!
>
> I tried to use set(gca,'ydir','normal') , but it flips the picture and the axis, not just the axis. I am not sure how to flip the axis only, I believe that's the problem.
>
> Thanks

one of the solutions

     axis image;

us

Subject: Angle between two vectors

From: Beho

Date: 18 Jun, 2010 16:48:48

Message: 45 of 77

can I just use this (2 points)

I = imread('angle.jpg');
figure, imshow(I);
axis image;
[x, y] = ginput;
line(x,y,'color',[0 1 0]);

ang1= 180 - subspace(x,y)*180/pi

Subject: Angle between two vectors

From: Roger Stafford

Date: 18 Jun, 2010 22:51:05

Message: 46 of 77

Beho <newparadize2002@gmail.com> wrote in message <e615968b-18d6-489f-bbfc-343a546f8869@g19g2000yqc.googlegroups.com>...
> can I just use this (2 points)
>
> I = imread('angle.jpg');
> figure, imshow(I);
> axis image;
> [x, y] = ginput;
> line(x,y,'color',[0 1 0]);
>
> ang1= 180 - subspace(x,y)*180/pi

  No, I don't think that would work for what you are doing. The angle given by subspace is never greater than 90 degrees (pi/2,) so if you subtract it from 180, it will never be less than 90. I understood that you wanted the angle between two (directed) vectors which can range anywhere between 0 and 180.

  If you only want the smaller of the two angles between two lines without regard to direction along the lines and if both lines pass through the origin, then subspace, (without the subtraction from 180,) would indeed give you that answer.

  Note: As I pointed out in another thread a week ago, there is an erroneous statement made in MathWorks' description of subspace. They state that, "If A and B are column vectors of unit length, this is the same as acos(A'*B)." It would actually be acos(abs(A'*B)), and that is why the angle from subspace can never be greater than 90 degrees (pi/2.)

Roger Stafford

Subject: Angle between two vectors

From: Beho nashed

Date: 19 Jun, 2010 16:05:22

Message: 47 of 77

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hvgt8p$ss7$1@fred.mathworks.com>...
> Beho <newparadize2002@gmail.com> wrote in message <e615968b-18d6-489f-bbfc-343a546f8869@g19g2000yqc.googlegroups.com>...
> > can I just use this (2 points)
> >

Hello Roger,
so how to change the code just to measure the angle, between the two lines.
should I use two points.

I used subspace, I selected two points (giving an approximate 45 deg), but it gave me 4 degree and sometimes 14 deg, I am not gonna use this one.

I am not sure Roger how to calculate the angle? is the y-axis messing up the angle?
Thanks

Subject: Angle between two vectors

From: Roger Stafford

Date: 19 Jun, 2010 17:40:22

Message: 48 of 77

"Beho nashed" <beho86@yahoo.com> wrote in message <hvips2$nuh$1@fred.mathworks.com>...
> Hello Roger,
> so how to change the code just to measure the angle, between the two lines.
> should I use two points.
> I used subspace, I selected two points (giving an approximate 45 deg), but it gave me 4 degree and sometimes 14 deg, I am not gonna use this one.
> I am not sure Roger how to calculate the angle? is the y-axis messing up the angle?
> Thanks
- - - - - - - - - -
  When speaking of the angle between two lines it is necessary to distinguish between two possible meanings. If the lines are "undirected", meaning that there is no preferred direction along either line, then there are two angles between the lines to be considered, one being the supplement of the other, and their sum is 180 degrees (or pi radians.) Usually the smaller of the two is chosen as being the defined angle between the lines, and that is the one given by matlab's 'subspace' function for two lines through the origin. Such an angle cannot exceed 90 degrees by definition.

  On the other hand there is the concept of the angle between two directed lines, meaning that there is a preferred "positive" direction along each line. This is equivalent to the notion of the angle between two vectors which have a common base point. Such an angle can range from 0 to 180 degrees. It is this latter kind of angle that I have been giving the formulas for. Matlab's 'atan2' function is actually capable of producing angles anywhere between minus pi and plus pi radians, but in all these formulas, the first argument was always non-negative, so that restricts the angle to the first two quadrants, and 'atan2' can then only produce results for these that range from zero to pi radians (0 to 180 in degrees.)

  In neither kind of situation can one find the angle with only two points! You will always need either two points on each line to establish a direction for each one, or else the point of intersection of the two lines and one other point on each line. If both the two lines pass through the coordinate origin, then that point of intersection is the origin and only two other points on the lines are required.

  If the lines are considered as directed, then on each line the coordinates of the point less far out in the positive direction are subtracted from those of the other point farther out, and this establishes the components of the two vectors needed to determine the angle between the directed lines.

  I hope this finally winds up this thread about finding the "angle between two vectors" which has now gone on for forty-eight articles and stretched over a span of almost three years. That is quite enough in my opinion for such an elementary concept.

Roger Stafford

Subject: Angle between two vectors

From: Bard Romstad

Date: 17 Jul, 2010 18:50:04

Message: 49 of 77

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <fl3moi$pvc$1@fred.mathworks.com>...
> "baris kazar" <mbkazar.nospam@gmail.com> wrote in message <fl377q$4ip
> $1@fred.mathworks.com>...
> > Hi,-
> > how can we generalize this to 3-D vectors? Think of a
> > plane on 3-D space and you have vectors on this plane. I
> > wanna know the angle between 2 vectors in the range 0-2pi.
> > or at least -pi to pi. I have 3 vectors. One (First)
> > vector is the same all the time. I wanna know the relative
> > positions of the other two vector wrt the firsy one. Thus,
> > i need angles in the range 0 to 2pi or -pi to pi.
> > Thanks in advance
> ---------
> As Bruno has pointed out, the angle between two three-dimensional vectors
> depends on which sense one gives to a vector orthogonal to their plane. It
> isn't clear what you meant by, "the relative positions of the other two vector
> wrt the firsy one." The vector cross product of the second two vectors will be
> a vector orthogonal to their plane. Perhaps you mean that the angle between
> them is to be considered positive if this cross product lies on the same side
> of the plane as this first vector, and negative otherwise. If that is the case,
> then let your first, second, and third vectors be designated as x, y, and z,
> respectively. A matlab formula for calculating the angle between y and z will
> then be:
>
> c = cross(y,z);
> angleyz = sign(dot(x,c))*atan2(norm(c),dot(y,z));
>
> The value of 'angleyz' will range from -pi to +pi. If you want it to range from
> 0 to 2*pi, then apply the 'mod' function as I did on Dec. 11 in this thread.
>
> Roger Stafford
>

This will fail if all three vectors x, y and z lie on the same plane because then dot(x,c)=0 (and sign(0)=0 -> angleyz = 0).
Example:
x = [1 0 1];
y = [0 1 1];
z = [0.5 0.5 1];
x=x/norm(x);y=y/norm(y);z=z/norm(z);
c = cross(y,z);
angleyz = sign(dot(x,c))*atan2(norm(c),dot(y,z))

Any suggestions?

Bård Romstad

Subject: Angle between two vectors

From: Roger Stafford

Date: 17 Jul, 2010 21:31:03

Message: 50 of 77

"Bard Romstad" <this.is.not.a.real.address@gmail.com> wrote in message <i1su0s$deu$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <fl3moi$pvc$1@fred.mathworks.com>...
> > "baris kazar" <mbkazar.nospam@gmail.com> wrote in message <fl377q$4ip
> > $1@fred.mathworks.com>...
> > > Hi,-
> > > how can we generalize this to 3-D vectors? Think of a
> > > plane on 3-D space and you have vectors on this plane. I
> > > wanna know the angle between 2 vectors in the range 0-2pi.
> > > or at least -pi to pi. I have 3 vectors. One (First)
> > > vector is the same all the time. I wanna know the relative
> > > positions of the other two vector wrt the firsy one. Thus,
> > > i need angles in the range 0 to 2pi or -pi to pi.
> > > Thanks in advance
> > ---------
> > As Bruno has pointed out, the angle between two three-dimensional vectors
> > depends on which sense one gives to a vector orthogonal to their plane. It
> > isn't clear what you meant by, "the relative positions of the other two vector
> > wrt the firsy one." The vector cross product of the second two vectors will be
> > a vector orthogonal to their plane. Perhaps you mean that the angle between
> > them is to be considered positive if this cross product lies on the same side
> > of the plane as this first vector, and negative otherwise. If that is the case,
> > then let your first, second, and third vectors be designated as x, y, and z,
> > respectively. A matlab formula for calculating the angle between y and z will
> > then be:
> >
> > c = cross(y,z);
> > angleyz = sign(dot(x,c))*atan2(norm(c),dot(y,z));
> >
> > The value of 'angleyz' will range from -pi to +pi. If you want it to range from
> > 0 to 2*pi, then apply the 'mod' function as I did on Dec. 11 in this thread.
> >
> > Roger Stafford
> >
>
> This will fail if all three vectors x, y and z lie on the same plane because then dot(x,c)=0 (and sign(0)=0 -> angleyz = 0).
> Example:
> x = [1 0 1];
> y = [0 1 1];
> z = [0.5 0.5 1];
> x=x/norm(x);y=y/norm(y);z=z/norm(z);
> c = cross(y,z);
> angleyz = sign(dot(x,c))*atan2(norm(c),dot(y,z))
>
> Any suggestions?
>
> Bård Romstad
- - - - - - - - - -
  Well of course it fails, based as it is on a desperate guess as to what Baris Kazar meant when he said (back on December 28, 2007) "the relative positions of the other two vector wrt the firsy one"! This was an attempt on my part to interpret his statement as indicating that the first vector establishes which side of the plane containing the second two vectors is to be considered as its "positive" side. If that first vector lies in this plane, then of course it fails to do so.

  With three 3D vectors all lying in a plane through their origin, even if one of them is regarded as some sort of reference, there is no inherent "clockwise" or "counterclockwise" sense to angles in that plane. Only some reference direction outside the plane or else a pair of "standard" vectors within the plane can do that. When we say "clockwise" it is said from the point of view of a person seeing a clock from outside the plane of the clock hands from the usual side of the clock. If a transparent clock is viewed from its rear, what was the clockwise direction now becomes the counterclockwise direction (if the backward numerals are ignored.) The famous "right-hand rule" depends on one's right-hand thumb lying outside the plane of one's curled finger directions.

  Bard, if you want a formula for the angle between Baris Kazar's second two vectors ranging from 0 to 2*pi, please explain to me your idea of what you think he meant by the above quotation given that the first vector lies in their plane.

Roger Stafford

Subject: Angle between two vectors

From: Bard Romstad

Date: 18 Jul, 2010 10:55:04

Message: 51 of 77

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i1t7en$gvq$1@fred.mathworks.com>...
> "Bard Romstad" <this.is.not.a.real.address@gmail.com> wrote in message <i1su0s$deu$1@fred.mathworks.com>...
> > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <fl3moi$pvc$1@fred.mathworks.com>...
> > > "baris kazar" <mbkazar.nospam@gmail.com> wrote in message <fl377q$4ip
> > > $1@fred.mathworks.com>...
> > > > Hi,-
> > > > how can we generalize this to 3-D vectors? Think of a
> > > > plane on 3-D space and you have vectors on this plane. I
> > > > wanna know the angle between 2 vectors in the range 0-2pi.
> > > > or at least -pi to pi. I have 3 vectors. One (First)
> > > > vector is the same all the time. I wanna know the relative
> > > > positions of the other two vector wrt the firsy one. Thus,
> > > > i need angles in the range 0 to 2pi or -pi to pi.
> > > > Thanks in advance
> > > ---------
> > > As Bruno has pointed out, the angle between two three-dimensional vectors
> > > depends on which sense one gives to a vector orthogonal to their plane. It
> > > isn't clear what you meant by, "the relative positions of the other two vector
> > > wrt the firsy one." The vector cross product of the second two vectors will be
> > > a vector orthogonal to their plane. Perhaps you mean that the angle between
> > > them is to be considered positive if this cross product lies on the same side
> > > of the plane as this first vector, and negative otherwise. If that is the case,
> > > then let your first, second, and third vectors be designated as x, y, and z,
> > > respectively. A matlab formula for calculating the angle between y and z will
> > > then be:
> > >
> > > c = cross(y,z);
> > > angleyz = sign(dot(x,c))*atan2(norm(c),dot(y,z));
> > >
> > > The value of 'angleyz' will range from -pi to +pi. If you want it to range from
> > > 0 to 2*pi, then apply the 'mod' function as I did on Dec. 11 in this thread.
> > >
> > > Roger Stafford
> > >
> >
> > This will fail if all three vectors x, y and z lie on the same plane because then dot(x,c)=0 (and sign(0)=0 -> angleyz = 0).
> > Example:
> > x = [1 0 1];
> > y = [0 1 1];
> > z = [0.5 0.5 1];
> > x=x/norm(x);y=y/norm(y);z=z/norm(z);
> > c = cross(y,z);
> > angleyz = sign(dot(x,c))*atan2(norm(c),dot(y,z))
> >
> > Any suggestions?
> >
> > Bård Romstad
> - - - - - - - - - -
> Well of course it fails, based as it is on a desperate guess as to what Baris Kazar meant when he said (back on December 28, 2007) "the relative positions of the other two vector wrt the firsy one"! This was an attempt on my part to interpret his statement as indicating that the first vector establishes which side of the plane containing the second two vectors is to be considered as its "positive" side. If that first vector lies in this plane, then of course it fails to do so.
>
> With three 3D vectors all lying in a plane through their origin, even if one of them is regarded as some sort of reference, there is no inherent "clockwise" or "counterclockwise" sense to angles in that plane. Only some reference direction outside the plane or else a pair of "standard" vectors within the plane can do that. When we say "clockwise" it is said from the point of view of a person seeing a clock from outside the plane of the clock hands from the usual side of the clock. If a transparent clock is viewed from its rear, what was the clockwise direction now becomes the counterclockwise direction (if the backward numerals are ignored.) The famous "right-hand rule" depends on one's right-hand thumb lying outside the plane of one's curled finger directions.
>
> Bard, if you want a formula for the angle between Baris Kazar's second two vectors ranging from 0 to 2*pi, please explain to me your idea of what you think he meant by the above quotation given that the first vector lies in their plane.
>
> Roger Stafford

Hi Roger,
Giving it a second look I'm not quite certain what Baris ment after all, but what I am trying to find is how to calculate the signed angle (-pi to pi) between two vectors, A and B, projected onto the vertical plane passing through vector C, viewed from the direction defined by cross(C,[0 0 1]).
To clarify (or confuse):
In my specific problem A and B are surface normals of two neighbouring patches on a 2.5D topographic surface (no caves/overhangs -> normals are always pointing upwards). I want to investigate the convexity of the topographic surface along a given direction (defined by the horizontal component of C). My idea was to do this by finding the angle between A and B (relative to C).

Subject: Angle between two vectors

From: Bard Romstad

Date: 18 Jul, 2010 14:36:03

Message: 52 of 77

> Hi Roger,
> Giving it a second look I'm not quite certain what Baris ment after all, but what I am trying to find is how to calculate the signed angle (-pi to pi) between two vectors, A and B, projected onto the vertical plane passing through vector C, viewed from the direction defined by cross(C,[0 0 1]).
> To clarify (or confuse):
> In my specific problem A and B are surface normals of two neighbouring patches on a 2.5D topographic surface (no caves/overhangs -> normals are always pointing upwards). I want to investigate the convexity of the topographic surface along a given direction (defined by the horizontal component of C). My idea was to do this by finding the angle between A and B (relative to C).

Having thought a little more about this I guess a more general formulation of my question is how to find the signed angle (-pi to pi) between two vectors, A and B, projected onto the plane defined by a normal vector N (which in the specific case above was cross(C,[0 0 1])), viewed from the direction of N.

Bård

Subject: Angle between two vectors

From: Roger Stafford

Date: 18 Jul, 2010 17:13:05

Message: 53 of 77

"Bard Romstad" <this.is.not.a.real.address@gmail.com> wrote in message <i1v3gj$8rh$1@fred.mathworks.com>...
> Having thought a little more about this I guess a more general formulation of my question is how to find the signed angle (-pi to pi) between two vectors, A and B, projected onto the plane defined by a normal vector N (which in the specific case above was cross(C,[0 0 1])), viewed from the direction of N.
>
> Bård
- - - - - - - - - - -
  Yes, that makes sense. I presume when you say, "viewed from the direction of N", you mean in accordance with the right-hand rule: if your right-hand thumb points along the direction of N, then your fingers curl in the direction of positive angle change. And when you say, "signed angle between A and B" you mean the angle measured from the projection of A toward the projection of B but restricted to the interval -pi to +pi.

  That can be done this way:

 N = N/norm(N); % Make N a unit vector
 ang = atan2(dot(cross(A,B),N),dot(cross(A,N),cross(B,N)));

Note that this will fail if either A or B is parallel to N, as you would expect, since the projection of that respective vector onto the plane would then be zero and therefore the angle would be undefined.

Roger Stafford

Subject: Angle between two vectors

From: Bard R.

Date: 18 Jul, 2010 18:38:03

Message: 54 of 77

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i1vcn1$s7p$1@fred.mathworks.com>...
> "Bard Romstad" <this.is.not.a.real.address@gmail.com> wrote in message <i1v3gj$8rh$1@fred.mathworks.com>...
> > Having thought a little more about this I guess a more general formulation of my question is how to find the signed angle (-pi to pi) between two vectors, A and B, projected onto the plane defined by a normal vector N (which in the specific case above was cross(C,[0 0 1])), viewed from the direction of N.
> >
> > Bård
> - - - - - - - - - - -
> Yes, that makes sense. I presume when you say, "viewed from the direction of N", you mean in accordance with the right-hand rule: if your right-hand thumb points along the direction of N, then your fingers curl in the direction of positive angle change. And when you say, "signed angle between A and B" you mean the angle measured from the projection of A toward the projection of B but restricted to the interval -pi to +pi.
>
> That can be done this way:
>
> N = N/norm(N); % Make N a unit vector
> ang = atan2(dot(cross(A,B),N),dot(cross(A,N),cross(B,N)));
>
> Note that this will fail if either A or B is parallel to N, as you would expect, since the projection of that respective vector onto the plane would then be zero and therefore the angle would be undefined.
>
> Roger Stafford

This is exactly what I was looking for. And so much more compact than the solution I had put together during the day. It works if A and B are opposite (I had to do an extra check for that as well) and for my application N is by definition never parallell to neither A nor B.
Thanx a lot!

Bård

Subject: Angle between two vectors

From: Roger Stafford

Date: 18 Jul, 2010 19:52:03

Message: 55 of 77

"Bard R." <this.is.not.a.real.address@gmail.com> wrote in message <i1vhmb$1rq$1@fred.mathworks.com>...
> This is exactly what I was looking for. And so much more compact than the solution I had put together during the day. It works if A and B are opposite (I had to do an extra check for that as well) and for my application N is by definition never parallell to neither A nor B.
> Thanx a lot!
> Bård
- - - - - - - - - - - -
  Well, if A and B, or even their projections, are in opposite directions, the outcome can suddenly flip from one extreme to the other depending on rounding. A roundoff one way gets +pi as a result and in the other direction flips the answer over to -pi. I don't see any good way out of that problem. It is more or less inherent in the nature of numerical computation of angles. A digital computer cannot even compute the exact value of pi itself.

Roger Stafford

Subject: Angle between two vectors

From: Loren_Shure

Date: 19 Jul, 2010 12:21:39

Message: 56 of 77


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:i1vm13$ok8$1@fred.mathworks.com...
> "Bard R." <this.is.not.a.real.address@gmail.com> wrote in message
> <i1vhmb$1rq$1@fred.mathworks.com>...
>> This is exactly what I was looking for. And so much more compact than the
>> solution I had put together during the day. It works if A and B are
>> opposite (I had to do an extra check for that as well) and for my
>> application N is by definition never parallell to neither A nor B.
>> Thanx a lot!
>> B책rd
> - - - - - - - - - - - -
> Well, if A and B, or even their projections, are in opposite directions,
> the outcome can suddenly flip from one extreme to the other depending on
> rounding. A roundoff one way gets +pi as a result and in the other
> direction flips the answer over to -pi. I don't see any good way out of
> that problem. It is more or less inherent in the nature of numerical
> computation of angles. A digital computer cannot even compute the exact
> value of pi itself.
>
> Roger Stafford

Have you checked out the function subspace:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/subspace.html ?


--
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Angle between two vectors

From: us

Date: 19 Jul, 2010 12:38:04

Message: 57 of 77

"Loren_Shure" <loren.shure@mathworks.com> wrote in message <i21g0k$ker$1@fred.mathworks.com>...
>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
> message news:i1vm13$ok8$1@fred.mathworks.com...
> > "Bard R." <this.is.not.a.real.address@gmail.com> wrote in message
> > <i1vhmb$1rq$1@fred.mathworks.com>...
> >> This is exactly what I was looking for. And so much more compact than the
> >> solution I had put together during the day. It works if A and B are
> >> opposite (I had to do an extra check for that as well) and for my
> >> application N is by definition never parallell to neither A nor B.
> >> Thanx a lot!
> >> B책rd
> > - - - - - - - - - - - -
> > Well, if A and B, or even their projections, are in opposite directions,
> > the outcome can suddenly flip from one extreme to the other depending on
> > rounding. A roundoff one way gets +pi as a result and in the other
> > direction flips the answer over to -pi. I don't see any good way out of
> > that problem. It is more or less inherent in the nature of numerical
> > computation of angles. A digital computer cannot even compute the exact
> > value of pi itself.
> >
> > Roger Stafford
>
> Have you checked out the function subspace:
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/subspace.html ?
>
>
> --
> Loren
> http://blogs.mathworks.com/loren/
> http://matlabwiki.mathworks.com/MATLAB_FAQ

dearest loren
if you carefully wade through this thread you'll find out that it has been discussed at length, already...

:-)
urs

Subject: Angle between two vectors

From: Roger Stafford

Date: 19 Jul, 2010 17:19:04

Message: 58 of 77

"Loren_Shure" <loren.shure@mathworks.com> wrote in message <i21g0k$ker$1@fred.mathworks.com>...
> Have you checked out the function subspace:
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/subspace.html ?
> --
> Loren
> http://blogs.mathworks.com/loren/
> http://matlabwiki.mathworks.com/MATLAB_FAQ
- - - - - - - - -
  Yes, Loren, as Urs has said, that was discussed in article #46 of this overly-long thread. The person proposing to use subspace was seeking an angle that ranges from 0 to pi (180 deg) as in the inner angle in triangles, but subspace would not give an angle beyond pi/2 in this case. For example

 a = subspace([1;0],[-1;1]) ---> 1/4*pi

whereas

 a = acos([1;0],[-1;1]) --> 3/4*pi

  This reminds me. As I pointed out in that #46 article, Mathworks' documents are still in error in claiming about subspace "If A and B are column vectors of unit length, this is the same as acos(A'*B)." This error was present in my manual for system 4a back in 1994 and it still persists to this day in their most recent documentation. It should read acos(abs(A'*B)). Have you any influence with the documentation department to get them to finally make a correction on this misleading statement?

Roger Stafford

Subject: Angle between two vectors

From: Loren Shure

Date: 20 Jul, 2010 11:54:36

Message: 59 of 77


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:i221e8$3du$1@fred.mathworks.com...
> "Loren_Shure" <loren.shure@mathworks.com> wrote in message
> <i21g0k$ker$1@fred.mathworks.com>...
>> Have you checked out the function subspace:
>> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/subspace.html ?
>> --
>> Loren
>> http://blogs.mathworks.com/loren/
>> http://matlabwiki.mathworks.com/MATLAB_FAQ
> - - - - - - - - -
> Yes, Loren, as Urs has said, that was discussed in article #46 of this
> overly-long thread. The person proposing to use subspace was seeking an
> angle that ranges from 0 to pi (180 deg) as in the inner angle in
> triangles, but subspace would not give an angle beyond pi/2 in this case.
> For example
>
> a = subspace([1;0],[-1;1]) ---> 1/4*pi
>
> whereas
>
> a = acos([1;0],[-1;1]) --> 3/4*pi
>
> This reminds me. As I pointed out in that #46 article, Mathworks'
> documents are still in error in claiming about subspace "If A and B are
> column vectors of unit length, this is the same as acos(A'*B)." This
> error was present in my manual for system 4a back in 1994 and it still
> persists to this day in their most recent documentation. It should read
> acos(abs(A'*B)). Have you any influence with the documentation department
> to get them to finally make a correction on this misleading statement?
>
> Roger Stafford
>

Roger-

Will enter a request to update the doc...

--
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Angle between two vectors

From: Robert Phillips

Date: 7 Dec, 2010 17:27:18

Message: 60 of 77

Roger,


This thread is very interesting! It directly applies to a problem I'm tackling, as you may have read in other threads.

I try to calculate cosine values using the formula

cos(theta) = (dot(A,B)) / (|A|*|B|),

but for A = [M-by-3] and B = [1-by-3] & a=|A| b=|B|

    % Calculate cosines of angles between a_E & r_Es vectors.
    cos(theta) = cellfun(@sum,...
                 (mat2cell((A(:,1:3).*(repmat(B(i,:),...
                                               size(A(:,1:3),1),1))),...
                           ones(1,size(A(:,1:3),1)),3)))./(a*b);

And I get cosine values ranging between 0 and +/- 2.667, for B lines parallel to A.
In fact, it's desirable if B is parallel to A.

How might you suggest calculating the cosine value, or even calculating the angle between, A=[M-by-3] & B=[1-by-3]? (without loops)

Subject: Angle between two vectors

From: Geant Bepi

Date: 17 Dec, 2010 15:51:05

Message: 61 of 77

Hey guys!

sorry for posting something irrelevant to the topic being discussed here..

but I need some help!
 
I invite the brains here to look at my simple problem mentioned below and help me with some tips;
link: http://www.mathworks.com/matlabcentral/newsreader/view_thread/297252#804997

appreciate any knowledge you'd like to share on the matter
thanks heaps!

Subject: Angle between two vectors

From: Marwan N

Date: 10 Mar, 2011 07:04:05

Message: 62 of 77

hey guys,

I went thro the codes u all gave several times....all are different ways for the same pupose....which finally gave me a head spin after all those cods in my head. As a simple conclusion can someone answer me the following:

1. how do we calculate the angle (consider both the obtuse and acute angle) between two vectors (consider that they intersect at origin or in other words, they originate from origin)

2.the vectors start at the origin and terminate at the points X=[coresponding coeff of i j k] and Y=[corresponding coeff of i j k]

3.the results (obtuse and acute angle) should lie between 0 and pi.

any bright ideas anyone??

Subject: Angle between two vectors

From: Bruno Luong

Date: 10 Mar, 2011 07:56:04

Message: 63 of 77

"Marwan N" wrote in message <il9t55$en6$1@fred.mathworks.com>...
> hey guys,
>
> I went thro the codes u all gave several times....all are different ways for the same pupose....which finally gave me a head spin after all those cods in my head. As a simple conclusion can someone answer me the following:
>
> 1. how do we calculate the angle (consider both the obtuse and acute angle) between two vectors (consider that they intersect at origin or in other words, they originate from origin)
>
> 2.the vectors start at the origin and terminate at the points X=[coresponding coeff of i j k] and Y=[corresponding coeff of i j k]
>
> 3.the results (obtuse and acute angle) should lie between 0 and pi.
>
> any bright ideas anyone??

>> a=randn(2,1)

a =

    1.4897
    1.4090

>> b=randn(2,1)

b =

    1.4172
    0.6715

% Pad 0 to extend in 3D if needed
>> a(3)=0
>> b(3)=0

% Roger method

>> angle = atan2(norm(cross(a,b)),dot(a,b))

angle =

    0.3151

angle will be [0,pi] since norm(cross(a,b)) >= 0.

Bruno

Subject: Angle between two vectors

From: Loren Shure

Date: 10 Mar, 2011 12:21:44

Message: 64 of 77


"Marwan N" <munna860@gmail.com> wrote in message
news:il9t55$en6$1@fred.mathworks.com...
> hey guys,
>
> I went thro the codes u all gave several times....all are different ways
> for the same pupose....which finally gave me a head spin after all those
> cods in my head. As a simple conclusion can someone answer me the
> following:
>
> 1. how do we calculate the angle (consider both the obtuse and acute
> angle) between two vectors (consider that they intersect at origin or in
> other words, they originate from origin)
>
> 2.the vectors start at the origin and terminate at the points
> X=[coresponding coeff of i j k] and Y=[corresponding coeff of i j k]
>
> 3.the results (obtuse and acute angle) should lie between 0 and pi.
>
> any bright ideas anyone??
>

Will the subspace function do what you want?
http://www.mathworks.com/help/techdoc/ref/subspace.html

--
Loren
http://blogs.mathworks.com/loren/
http://www.mathworks.com/matlabcentral/

Subject: Angle between two vectors

From: Bruno Luong

Date: 10 Mar, 2011 13:38:05

Message: 65 of 77

"Loren Shure" <loren.shure@mathworks.com> wrote in message <ilafo2$nnp$1@fred.mathworks.com>...
>
> "Marwan N" <munna860@gmail.com> wrote in message
> news:il9t55$en6$1@fred.mathworks.com...
> > hey guys,
> >
> > I went thro the codes u all gave several times....all are different ways
> > for the same pupose....which finally gave me a head spin after all those
> > cods in my head. As a simple conclusion can someone answer me the
> > following:
> >
> > 1. how do we calculate the angle (consider both the obtuse and acute
> > angle) between two vectors (consider that they intersect at origin or in
> > other words, they originate from origin)
> >
> > 2.the vectors start at the origin and terminate at the points
> > X=[coresponding coeff of i j k] and Y=[corresponding coeff of i j k]
> >
> > 3.the results (obtuse and acute angle) should lie between 0 and pi.
> >
> > any bright ideas anyone??
> >
>
> Will the subspace function do what you want?
> http://www.mathworks.com/help/techdoc/ref/subspace.html
>

subspace() will always reduce the resultant angle between [0,pi/2]. I'm not sure this is appropriate for geometry computation.

Bruno

Subject: Angle between two vectors

From: Marwan N

Date: 10 Mar, 2011 14:14:05

Message: 66 of 77

thnks Bruno.....finally the thread has conclusion.

Subject: Angle between two vectors

From: philippa

Date: 16 Aug, 2011 11:17:31

Message: 67 of 77

Wondering if anyone can please help me with yet another problem calculating angles in matlab!

Ill set the scene:
I am simulating erosion on volcano slopes. I have a bathymetry dataset for a volcano - a matrix of land heights in the form a grid. Suppose for simplicity the grid is 100km x 100km and i have a measurement of land height (z) at every 1km. So we are in 3D.

I want to calculate the angle of the volcano sides - so essentially I will have a matrix of angles in which the slope angle is calculated between a point and the point directly above it (so looking from the bottom of the volcano towards the peak). Im struggling because on the East slope, the peak will be to the West, whereas at the South, it will be to the North. I thus wonder whether I will need to calculate the angles using vertical cross sections of the volcano and rotate the matrix through 360 degrees.

Some help would be greatly appreciated as honestly i dont know where to start! and let me know if what ive said doesnt make sense!
Thank you!!

Subject: Angle between two vectors

From: Roger Stafford

Date: 16 Aug, 2011 16:36:14

Message: 68 of 77

"philippa" wrote in message <j2djkb$j0o$1@newscl01ah.mathworks.com>...
> Wondering if anyone can please help me with yet another problem calculating angles in matlab!
>
> Ill set the scene:
> I am simulating erosion on volcano slopes. I have a bathymetry dataset for a volcano - a matrix of land heights in the form a grid. Suppose for simplicity the grid is 100km x 100km and i have a measurement of land height (z) at every 1km. So we are in 3D.
>
> I want to calculate the angle of the volcano sides - so essentially I will have a matrix of angles in which the slope angle is calculated between a point and the point directly above it (so looking from the bottom of the volcano towards the peak). Im struggling because on the East slope, the peak will be to the West, whereas at the South, it will be to the North. I thus wonder whether I will need to calculate the angles using vertical cross sections of the volcano and rotate the matrix through 360 degrees.
>
> Some help would be greatly appreciated as honestly i dont know where to start! and let me know if what ive said doesnt make sense!
> Thank you!!
- - - - - - - -
  You can use the matlab 'gradient' function with a two-dimensional input. Be sure to read its documentation. The angle of steepest gradient you seek can be found as the arc tangent of the gradient magnitude:

 [Fx,Fy] = gradient(Z);
 ang = atan(sqrt(Fx.^2+Fy.^2));

where Fx and Fy are the two components of the gradient.

  In case you need it the azimuth angle of steepest gradient measured counterclockwise from the x-axis is given by

 azi = atan2(Fy,Fx);

  These angles are both in radian measure.

  I believe on the boundaries the gradient function is only a first order approximation while in the interior it is second order. For higher order approximations there are undoubtedly some to be found in the file exchange.

Roger Stafford

Subject: Angle between two vectors

From: Roger Stafford

Date: 16 Aug, 2011 16:46:26

Message: 69 of 77

"Roger Stafford" wrote in message <j2e69u$njg$1@newscl01ah.mathworks.com>...
> You can use the matlab 'gradient' function with a two-dimensional input. Be sure to read its documentation. The angle of steepest gradient you seek can be found as the arc tangent of the gradient magnitude:
>
> [Fx,Fy] = gradient(Z);
> ang = atan(sqrt(Fx.^2+Fy.^2));
>
> where Fx and Fy are the two components of the gradient.
>
> In case you need it the azimuth angle of steepest gradient measured counterclockwise from the x-axis is given by
>
> azi = atan2(Fy,Fx);
>
> These angles are both in radian measure.
>
> I believe on the boundaries the gradient function is only a first order approximation while in the interior it is second order. For higher order approximations there are undoubtedly some to be found in the file exchange.
>
> Roger Stafford
- - - - - - - -
Note: Make sure the units of vertical Z elevation are the same as the horizontal units between adjacent points of Z.

Roger Stafford

Subject: Angle between two vectors

From: Jonathan

Date: 19 Aug, 2011 22:55:11

Message: 70 of 77

"y Mehta" <mehtayogesh@gmail.(DOT).com> wrote in message <ef5ce9c.-1@webcrossing.raydaftYaTP>...
> How do I find the angle between two unit vectors a and b? I know I
> can find cosine theta by the following formula:
>
> theta = acos(dot(a,b));
>
> However, how do I know whether the angle is actually theta, or -theta
> or pi-theta or pi+theta??
>
> Notice that the vectors are in three dimension (3d).
>
> Thanks,
> -YM

You don't need to know the angle between [0,2pi]. All you need is:
angle = acos( dot(a,b) / (norm(a)*norm(b)) );

Then if you want to rotate:
 * a towards b: you should rotate a of +'angle' radians around the axis cross(a,b)
 * b towards a: you should rotate b of +'angle' radians around the axis cross(b,a)

The cross-product will have the right orientation depending on what you want to do.

Subject: Angle between two vectors

From: Roger Stafford

Date: 20 Aug, 2011 00:25:09

Message: 71 of 77

"Jonathan " <Johnhadida@hotmail.fr> wrote in message <j2mpke$4cu$1@newscl01ah.mathworks.com>...
> "y Mehta" <mehtayogesh@gmail.(DOT).com> wrote in message <ef5ce9c.-1@webcrossing.raydaftYaTP>...
> > How do I find the angle between two unit vectors a and b? I know I
> > can find cosine theta by the following formula:
> >
> > theta = acos(dot(a,b));
> >
> > However, how do I know whether the angle is actually theta, or -theta
> > or pi-theta or pi+theta??
> >
> > Notice that the vectors are in three dimension (3d).
> >
> > Thanks,
> > -YM
>
> You don't need to know the angle between [0,2pi]. All you need is:
> angle = acos( dot(a,b) / (norm(a)*norm(b)) );
>
> Then if you want to rotate:
> * a towards b: you should rotate a of +'angle' radians around the axis cross(a,b)
> * b towards a: you should rotate b of +'angle' radians around the axis cross(b,a)
>
> The cross-product will have the right orientation depending on what you want to do.
- - - - - - - - - - -
  Jonathan, this thread was begun over four years ago and has now stretched to no less than 70 articles. Moreover it concerns a very elementary subject in which every conceivable aspect of the original question seems to have been thoroughly explored. Don't you think we ought to let the thread finally come to a peaceful termination?

Roger Stafford

Subject: Angle between two vectors

From: Dylan

Date: 12 Oct, 2011 18:54:26

Message: 72 of 77

"Roger Stafford" wrote in message <fjm4u4$i8o$1@fred.mathworks.com>...
> "salih tuna" <salihtuna@gmail.com> wrote in message <fjlrpl$gii
> $1@fred.mathworks.com>...
> > Hi,
> > thanks a lot for your reply. yes they are in 2d, sorry i
> > forgot to mention.
> > i tried to apply the formula but i am getting wrong result.
> > for example i want to calculate the angle between a = [1 1]
> > and b = [0 -1] which is 225 degrees. with this formulae i
> > got 243.4. i couldn't see where i am doing the mistake.
> > thanks a lot in advance
> > salih
> >
> > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> > wrote in message <fjk0tg$jli$1@fred.mathworks.com>...
> > > angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi
> --------
> I certainly owe you an apology, Salih. That formula I gave you is very, very
> wrong. I can't imagine what I was thinking about when I wrote it. Chalk it up
> to momentary insanity! :-) The correct computation should be as follows.
>
> Assuming a = [x1,y1] and b = [x2,y2] are two vectors with their bases at the
> origin, the non-negative angle between them measured counterclockwise
> from a to b is given by
>
> angle = mod(atan2(x1*y2-x2*y1,x1*x2+y1*y2),2*pi);
>
> As you can see, this bears a close relationship to the three-dimensional
> formula I wrote last July 10. The quantities, x1*y2-x2*y1 and x1*x2+y1*y2
> are, respectively, the sine and cosine of the counterclockwise angle from
> vector a to vector b, multiplied by the product of their norms - that is, their
> cross product and the dot product restricted to two dimensions. The 'atan2'
> function then gives the angle between them ranging from -pi to +pi, and the
> 'mod' operation changes this so as to range from 0 to 2*pi, as you requested.
>
> Roger Stafford
>
>

Hate to belabor this thread, but I'm not getting the above equation to work. It works for the one test case you mention in a later thread a=(1,1) and b=(0,-1), but not much else. For example a=(0,1) and b=(1,0) gives 180. I can get the angle easily by using atan2(x2,y2) - atan2(x1,y1) but I haven't thought of a clever way to deal with wrapping without using a couple of if-then statements. (I want the answer to fall between -180 and 180)

thanks.

Subject: Angle between two vectors

From: Dylan

Date: 12 Oct, 2011 19:13:10

Message: 73 of 77

"Dylan" wrote in message <j74np2$m9p$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <fjm4u4$i8o$1@fred.mathworks.com>...
> > "salih tuna" <salihtuna@gmail.com> wrote in message <fjlrpl$gii
> > $1@fred.mathworks.com>...
> > > Hi,
> > > thanks a lot for your reply. yes they are in 2d, sorry i
> > > forgot to mention.
> > > i tried to apply the formula but i am getting wrong result.
> > > for example i want to calculate the angle between a = [1 1]
> > > and b = [0 -1] which is 225 degrees. with this formulae i
> > > got 243.4. i couldn't see where i am doing the mistake.
> > > thanks a lot in advance
> > > salih
> > >
> > > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> > > wrote in message <fjk0tg$jli$1@fred.mathworks.com>...
> > > > angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi
> > --------
> > I certainly owe you an apology, Salih. That formula I gave you is very, very
> > wrong. I can't imagine what I was thinking about when I wrote it. Chalk it up
> > to momentary insanity! :-) The correct computation should be as follows.
> >
> > Assuming a = [x1,y1] and b = [x2,y2] are two vectors with their bases at the
> > origin, the non-negative angle between them measured counterclockwise
> > from a to b is given by
> >
> > angle = mod(atan2(x1*y2-x2*y1,x1*x2+y1*y2),2*pi);
> >
> > As you can see, this bears a close relationship to the three-dimensional
> > formula I wrote last July 10. The quantities, x1*y2-x2*y1 and x1*x2+y1*y2
> > are, respectively, the sine and cosine of the counterclockwise angle from
> > vector a to vector b, multiplied by the product of their norms - that is, their
> > cross product and the dot product restricted to two dimensions. The 'atan2'
> > function then gives the angle between them ranging from -pi to +pi, and the
> > 'mod' operation changes this so as to range from 0 to 2*pi, as you requested.
> >
> > Roger Stafford
> >
> >
>
> Hate to belabor this thread, but I'm not getting the above equation to work. It works for the one test case you mention in a later thread a=(1,1) and b=(0,-1), but not much else. For example a=(0,1) and b=(1,0) gives 180. I can get the angle easily by using atan2(x2,y2) - atan2(x1,y1) but I haven't thought of a clever way to deal with wrapping without using a couple of if-then statements. (I want the answer to fall between -180 and 180)
>
> thanks.

Now I have to confess that I was using Excel to do this calculation which takes it's inputs as atan2(x,y). Matlab has them switched to atan2(y,x) which seems backward to me, but oh well. Sorry for my confusion.

Subject: Angle between two vectors

From: Bjorn Gustavsson

Date: 18 Oct, 2011 14:39:26

Message: 74 of 77

"Dylan" wrote in message <j74os6$qcj$1@newscl01ah.mathworks.com>...
>
[snip]
>
> Now I have to confess that I was using Excel to do this calculation which takes it's inputs
> as atan2(x,y). Matlab has them switched to atan2(y,x) which seems backward to me,
> but oh well. Sorry for my confusion.
>
Well it is not "just" matlab that has them switched. The history of atan2 suggests that if anything it is the other way around:

http://en.wikipedia.org/wiki/Atan2

I think of it as this for a mnemonic: atan(y/x) -> atan2(y,x), just replace the "/" with a ","

See also: man atan2

Subject: Angle between two vectors

From: Sergio

Date: 16 Feb, 2012 17:25:40

Message: 75 of 77

Very useful!
Thanks :-)

"salih tuna" <salihtuna@gmail.com> wrote in message <fjmabs$9fh$1@fred.mathworks.com>...
> Roger hi,
> thanks a lot for your help but i am afraid something is
> still missing. i tried the formulae on the same example of
> a = [1 1]and b = [0 -1] (both passing through origin).
> the answer i got is 315 instead of 225.
> sorry i am taking a lot of your time :)
> thanks
> salih
>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> wrote in message <fjm4u4$i8o$1@fred.mathworks.com>...
> > "salih tuna" <salihtuna@gmail.com> wrote in message
> <fjlrpl$gii
> > $1@fred.mathworks.com>...
> > > Hi,
> > > thanks a lot for your reply. yes they are in 2d, sorry i
> > > forgot to mention.
> > > i tried to apply the formula but i am getting wrong result.
> > > for example i want to calculate the angle between a = [1 1]
> > > and b = [0 -1] which is 225 degrees. with this formulae i
> > > got 243.4. i couldn't see where i am doing the mistake.
> > > thanks a lot in advance
> > > salih
> > >
> > > "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> > > wrote in message <fjk0tg$jli$1@fred.mathworks.com>...
> > > > angle = mod(atan2(y2-y1,x2-x1),2*pi); % Range: 0 to 2*pi
> > --------
> > I certainly owe you an apology, Salih. That formula I
> gave you is very, very
> > wrong. I can't imagine what I was thinking about when I
> wrote it. Chalk it up
> > to momentary insanity! :-) The correct computation
> should be as follows.
> >
> > Assuming a = [x1,y1] and b = [x2,y2] are two vectors
> with their bases at the
> > origin, the non-negative angle between them measured
> counterclockwise
> > from a to b is given by
> >
> > angle = mod(atan2(x1*y2-x2*y1,x1*x2+y1*y2),2*pi);
> >
> > As you can see, this bears a close relationship to the
> three-dimensional
> > formula I wrote last July 10. The quantities, x1*y2-x2*y1
> and x1*x2+y1*y2
> > are, respectively, the sine and cosine of the
> counterclockwise angle from
> > vector a to vector b, multiplied by the product of their
> norms - that is, their
> > cross product and the dot product restricted to two
> dimensions. The 'atan2'
> > function then gives the angle between them ranging from
> -pi to +pi, and the
> > 'mod' operation changes this so as to range from 0 to
> 2*pi, as you requested.
> >
> > Roger Stafford
> >
> >
>

Subject: Angle between two vectors

From: Amit

Date: 17 Mar, 2012 11:02:17

Message: 76 of 77

"Roger Stafford" wrote in message <hvet7b$b6c$1@fred.mathworks.com>...
> "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

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.

Subject: Angle between two vectors

From: Roger Stafford

Date: 17 Mar, 2012 23:10:29

Message: 77 of 77

"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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us