Google Answers Logo
View Question
 
Q: Calculating the angle between two vectors (c / c++) ( No Answer,   1 Comment )
Question  
Subject: Calculating the angle between two vectors (c / c++)
Category: Computers > Programming
Asked by: odc-ga
List Price: $5.00
Posted: 29 Mar 2004 13:49 PST
Expires: 30 Mar 2004 13:52 PST
Question ID: 321912
Given the point's v1<x, y, z> and v2<x, y, z>. I want to know what the
angle is in all directions to get from v1 to v2 in degrees. I need an
example code snippet.

Request for Question Clarification by mathtalk-ga on 29 Mar 2004 16:24 PST
Hi, odc-ga:

The subject says "angle between two vectors" but your Question refers
to v1 and v2 as "point's", presumably in 3D.

Are you asking for the measure of the planar angle from v1 to v2
(considered as vectors drawn from the origin to the given endpoints)? 
I'm a bit thrown off by the phrase "what the angle is in all
directions".  With two vectors there's really only one angle to
compute.

It's probably easiest in C/C++ to compute the angle in radians, but
converting radians to degrees is just an easy second step.

regards, mathtalk-ga

Clarification of Question by odc-ga on 29 Mar 2004 22:16 PST
First of all, thanks so far!

But let me clarification my goal and the reason i said "the angle is in all
directions".

I'm trying to make an cylinder in 3d which is following a path. The
path is defined by a couple of vectors (or better said points in this
case).

Now its easy to create an circle around an point in space and connect
te dots to the next circle along the path. But my problem is that i
need to rotate the circle so it's facing the next one in the path.

Thats why i said that i needed the angle in all directions from 1
point in space to another point in space.

I really hope it's a bit clear what i need. Thanks in advance!

Request for Question Clarification by mathtalk-ga on 30 Mar 2004 02:28 PST
Sorry, but I'm still unclear on the Question.  You have two points in
space, v1 and v2.  These define a line passing through the two points,
and if a radius were specified, then we could define a cylinder of
that radius about that line.

But even with a radius, I'm not sure exactly how an angle "in all
directions" fits into the picture.  The coordinates of the cylinder
could be parameterized in terms of one angle (of rotation about the
axis) plus one other argument (representing "distance" along the axis
from v1 to v2).

regards, mathtalk-ga

Clarification of Question by odc-ga on 30 Mar 2004 02:55 PST
Okay, givven the following points:

	v1<0, 0, 0>
	v2<0, 0, 50>
	v3<50, 0, 50>
	v4<50, 0, 0>
	
	v2    v3
	.-----.
	|     |
	|     |
	.     .
    v1    v4

   (horizontal is the X-axis, vertical is the Z-axis)
    
Now i want to draw a cylinder which starts at v1, goes to v2, v3 and
ends at v4. With the 4 points above i want to produce an cylinder like
this:

    ____________________
   |\                  /|
   |  \ v2        v3 /  |
   |    \ ________ /    |
   |     |        |     |
   |     |        |     |
   |     |        |     |
   |  v1 |        |  v4 |
   -------        -------

As you can see, the plane of v1 is pointing to v2, v2 is at an 45
degree angle, v3 is at an 45 degree angle and v4 is straight again
pointing outwards.

The angle between v1 and v2 is 90 degrees around the Y-axis. The angle
between v2 and v3 is zero. Divide both and you get the rotation of the
plane at v2 which is 45 degrees. But now i want to know how to
calculate that. And ofcourse i want know the angle for the X-axis and
Z-axis aswell because it has to be 3d cylinder.

So in other words: i need the rotation (in degrees of each plane) so i
can rotate the circle so it's in line with the path it has to follow.

(i hope my humble ascii art comes through correctly :-)

Request for Question Clarification by mathtalk-ga on 30 Mar 2004 03:08 PST
I think one issue here is that the "path" from v1 to v2 to v3 to v4
isn't "smooth".  If there were a smooth path (in the simplest case, a
straight line) then you could define a "circle" lying in a plane
perpendicular to the path.

But when the path takes a "bend" (as when sharply turning at v2 from
along the line from v1 to v2 to a line from v2 to v3) one loses a
well-defined notion of perpendicularity at that "cornerpoint".

regards, mathtalk-ga

Clarification of Question by odc-ga on 30 Mar 2004 04:13 PST
True, that's why i need to know how to rotate towards the next point
along the path.

In theory it's pretty simple: (in kind of pseudo code, and really simply said)

For each point in the path
  Get the rotation to the next point of the path
  Add the new rotation to the one of the previous point and divide it by 2

  For each point of the circle 
    Rotate with the radius around the 'rotation axis'
    Draw an line from that point to the same point in the previous
circle from the path
  Endfor
Endfor

But now getting that to work ... i'm getting pretty desperate.

Request for Question Clarification by mathtalk-ga on 30 Mar 2004 05:09 PST
The problem is now fairly clear to me.  You have a path consisting of
multiple line segments, and at each "turning point" along the path you
want to define (for some fixed radius R) a circle centered on that
point.  You also want to define a notion of matching points from one
circle to the next along the path.

I think for the price offered, I'd only be willing to suggest an
approach to defining the circles at each turning point, and I think
this would not really be enough help in terms of what you are trying
to do.  Those connecting lines from a point on one circle to the "same
point in the previous circle" are essential, it seems, to your
project, and just developing a good notation to discuss that from a
programming standpoint is going to take more time than would usually
be associated with $5.

As food for thought, however, you might picture these as "stovepipe"
segments, in which the generating lines of the cylindrical surfaces
run parallel to their associated line segments.  In that case you
would have, not circles, but ellipses at the junctures between these
stovepipe segments.

best wishes, mathtalk-ga

Clarification of Question by odc-ga on 30 Mar 2004 05:26 PST
So what kind of price are you thinking about? And what can i expect
for it? Some working code (c/c++ & opengl) or an good explaination of
how to do it?

Request for Question Clarification by mathtalk-ga on 30 Mar 2004 06:18 PST
Hi, odc-ga:

You can view my previous Answers by clicking on my "hyperlinked" name,
if that helps to address what you might get.  Working/tested code (in
whatever programming language) is what I try to produce on programming
questions, although there may be a few "code snippet" kinds of
Questions in there.

Any Researcher with a special interest in your project would be aided
by knowing what programming platform you are targeting.

As far as pricing goes, Google Answers provides a guideline for pricing here:

[Google Answers - How to Price Your Question]
http://answers.google.com/answers/pricing.html

regards, mathtalk-ga

Clarification of Question by odc-ga on 30 Mar 2004 11:37 PST
I've read the 'How to Price Your Question' already but i don't know
how much work ik will be for you to make the code.

And the price i gave for the question now was only for the formula (in
c/c++ code) which would give me the 'angle in all directions'.

But i thought about that 'angle in all directions' and i only need
need to know how much i would have to rotate around te y-axis and
around the x-axis. Because you can follow the complete path with only
those two angle's.

Request for Question Clarification by mathtalk-ga on 30 Mar 2004 12:53 PST
Hi, odc-ga:

There's already a Comment by darmit-ga which gives a formula and
suggestions about C/C++ implementation for the measure of the angle
betweeen two vectors.

In our Clarification exchanges you have discussed circles, lines,
cylinders, and paths, but little if anything has been said to define
angles.  While I think I got a clear enough picture of the surface
surrounding a given "piecewise linear" path you wish to construct, the
concept of "angle in all directions" is still a mystery to me, and I
do not see what sense you mean in saying that "you can follow the
complete path" with only an angle of rotation around the x-axis and an
angle of rotation around the y-axis.

One possible interpretation is that of directional cosines.  To pass
along the line segment from one point v1 = (x1,y1,z1) to another v2 =
(x2,y2,z2), one adds to v1 some multiple of v2 - v1 =
(x2-x1,y2-y1,z2-z1).  That is, the points on this line segment form
precisely the set:

{ v1 + t(v2-v1) | 0 <= t <= 1 }

Now if we assume v1 and v2 are not equal, so v2 - v1 is nonzero, then
one can scale v2 - v1 so as to have unit length:

  u = ( 1/L ) (v2 - v1)

where L = || v2 - v1 || is the usual Euclidean length of v2 - v1:

L^2 = (x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2.

Then u can be expressed as a "directional cosine":

  u = (cos(A), cos(B), cos(C))

subject to cos^2(A) + cos^2(B) + cos^2(C) = 1.  However this is just a
fancy way of restating that u has unit length, notwithstanding the
observation that A,B,C can be given a geometrical interpretation as
angles (and the vector u as a point on the unit sphere).

Now I'm at a loss for further suggestions.  Perhaps another Researcher
will be able to take up you Question with greater success.

best wishes, mathtalk-ga

Clarification of Question by odc-ga on 30 Mar 2004 13:52 PST
mathtalk-ga, thanks for all your help!

But i found the answer to my question. And it has to do with the last
thought i posted:

"But i thought about that 'angle in all directions' and i only need
need to know how much i would have to rotate around te y-axis and
around the x-axis. Because you can follow the complete path with only
those two angle's."

Thinking about that ... that's just like rotation in 2d. To rotate
from one point in 2d to another point in 2d all you need is the
direction and the length.

To get the direction i needed all you have to do is:

vector3d v = nextPosition - position;
float xrot = atan2(v.Y, v.X) * 180 / 3.1415926535897932384;
float yrot = atan2(v.Z, v.X) * 180 / 3.1415926535897932384;

To get the distance to the next point in the path:

vector2d v1 = vector2df(position.X, position.Y);
vector2d v2 = vector2df(position.X, position.Z);

float length = v1.getLength() / v2.getLength();

And you can go through each point in the path.

I'm really sorry for being so vague with what i wanted to do and
mismatching vectors, points, path's, plane's etc ... Now i kwow what
is what and i will never use those wrong again :)
Answer  
There is no answer at this time.

Comments  
Subject: Re: Calculating the angle between two vectors (c / c++)
From: darmit-ga on 29 Mar 2004 20:40 PST
 
Use the dot product. 

v1*v2 = |v1|*|v2|*cos(theta)

Where * is the dot product, |A| is the length (or magnitude) of A, and
theta is the angle that you seek.

In C++:

Vector v1, v2;
double v1_magnitude = sqrt(v1.x*v1.x + v1.y*v1.y + v1.z*v1.z);
double v2_magnitude = sqrt(v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);

v1 = v1 * (1.0/v1_magnitude) // this multiplies all the components
v2 = v2 * (1.0/v2_magnitude) // this multiplies all the components

double theta = acos(v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);

// and

double angle_in_degrees = theta * 180 / 3.1415926535897932384;


You'll need to #include "math.h" for the sqrt to work. And, you'll
need to check that both of the vectors are non-zero for the code to be
correct.

Dave

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy