Thread: Angle between vectors

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Angle between vectors

    Hi, I am trying to write a C++ program that gets angles between vector.

    I know how to get a direction of a vector..due to the degree of rotation.

    But what I don't fully understand is the angle between 2 vectors.

    For example if I have two vectors at point1(6,4) and point2(10,3)

    to make point1 rotate in order to face point2 I have to subtract
    new point(10-6, 3-4) atan(4/-1)..but thing is I don't understand how that is the angle between the 2 vectors..
    When I subtract 2 vectors it gives me a new vector. which tells me how much I need to move to get to the endpoint. so from the above explanation..to move from point to point2 I need a velocity of newpoint(4,-1)
    to get the angle between them..doesn't make much sense to me
    atan2(4/-1)..does that give me the angle between the new vector and the origin (0,0) or the origin of one of the points? Can someone please clarify...thanks

    If it was only one vector I understand it perfectly.but I just can't visualize the angle between 2 vectors.
    You ended that sentence with a preposition...Bastard!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The angle in-between is the arccosine of the dot product of the two vectors. Simple!

    EDIT: But if your vectors aren't normalized, you'll probably get some strange results!
    Last edited by GReaper; 02-20-2011 at 10:09 AM.
    Devoted my life to programming...

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Sipher View Post
    EDIT: But if your vectors aren't normalized, you'll probably get some strange results!
    It shouldn't be?

    Mathematical formula for those who prefer:

    v1 * v2 = |v1||v2|cos(fi)
    --> arccos((v1*v2)/(|v1||v2|)) = fi

    Where * is dot product and || is the absolute value (or whatever it's called).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    You can extend this to find the angle between two planes, by finding the angle between their normals. Also note that if you abs() the result of the dot product in the numerator, you can get the acute angle between two vectors.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I'm not using the arc cos but the arc tan or atan2(y/x)

    i have to vectors..to get the angle between them i use

    atan2(vector2.y-vector1.y, vector2.x-vector1.x) which gives me the amount of degrees to rotate..and then i use that to get the my new x and y direction. sin(angle)*radius..

    what I don't understand where does the angle on that atan2 returns lie on the cartesian coordinate. is it between the origin and one of the vectors? It is doing my head in..
    Thanks for fast reply.
    by the way Elysia..long time!
    Last edited by Eman; 02-20-2011 at 11:16 AM.
    You ended that sentence with a preposition...Bastard!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A quick computer drawn picture.
    Orange is vector 2. Red is vector 1.
    The smaller lines are the x and y components of the vectors.
    So the purple line would be the vec2.y - vec1.y, and the green would be vec1.x - vec2.x (you'll have to mirror it, sorry).
    So that leaves the black line as hypotenuse, and you can see the angle in there.

    I hope I got this right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    that's exactly my problem...you can form a right angled triangle...but I can never seem to do that...how do I do that..If I just draw a straight line to connect both heads of the 2 vectors...I don't get a right angled triangle..
    and if i form a right triangle for each vectors.it looks like they are individual angles..
    You ended that sentence with a preposition...Bastard!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Updated picture. This should now be correct.
    It's simple. Take vec2.y - vec1.y. That's the length of the orange line minus the length of the red line. Since the red line is negative, it gets longer.
    Then draw that line as the height of the triangle.

    Then take vec2.x - vec1.x. Since vec1.x is longer than vec2.x, we get a negative length, and since we defined positive x as right, we must draw it to the left.
    So draw it as the width of the triangle and connect it with the height. This is the green line.

    Then simply draw the hypotenuse to connect the base and the height.
    The angle fi will always be the angle between the hypotenuse and the height.
    Last edited by Elysia; 02-20-2011 at 11:39 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Ok my drawing sucks..the angle shaded in pink is that the angle I am looking for?

    And the pink line I use to connect it..is that how I connect two vectors.. it doesn't look like a right angled trangle
    You ended that sentence with a preposition...Bastard!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have to understand that vector arithmetics and scalar arithmetics are not the same thing. You can actually derive the cosine formula from vectors. Vectors do not always form right angled triangles.

    You should draw out the x and y components of your vectors.
    If you want to work purely with vectors in any coordinate system, then it's the formula I posted above that you want to use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    If you normalize the two vectors, (length = 1), then dot them, you'll get a right triangle. The dot product is the projection of the vectors onto each other.

    So, http://mathdl.maa.org/images/upload_.../Dray2/dot.gif. Suppose your two vectors are v and w. The dot product is the base of that right triangle in the image. Notice the right triangle it forms. Because v is normalized, the length of the hypotenuse is 1, so the base of the right triangle (dot product) is the cosine of the angle theta. So, take the arccosine of the dot product, and you'll get theta.

    Note that this works in any number of dimensions.

  12. #12
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I think your understand your explanation..
    so basically i subtract the 2 vectors..and resulting vector...i can draw it out as a right angle triangle....if i do that..will the sprite will have to move the whole degree before it settles..i will like to rotate the sprite on its vector
    You ended that sentence with a preposition...Bastard!

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot always form a right triangle with vectors! That only works if they are perpendicular. You can make a right triangle if you subtract their components. This is an important fact.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    their components meaning the Vector.x Vector.y and so on, yeah?
    That is what I mean..I'm not too big on terminology and stuff....
    You ended that sentence with a preposition...Bastard!

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, a vector contains two or more components. In the Cartesian coordinate system, that would be, say, x, y and z.
    There is a big difference between subtracting a vector's components and subtracting vectors.

    Actually, I shouldn't have said...
    "You can make a right triangle if you subtract their components"
    ...since this only holds true if the vector's components are perpendicular. Meh. But if you're using Cartesian, they always are.
    Well, they are in most coordinate systems, because it's convenient.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compute the angle to rotate for 2 vectors
    By Anddos in forum Game Programming
    Replies: 4
    Last Post: 12-16-2009, 11:34 PM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  4. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  5. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM