Thread: Angle between vectors

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Geometrian View Post
    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.
    But this is the same as the formula above, except v and w are normalized, hence the denominator is 1.
    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.

  2. #17
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yeah, I am using cartesian coordinate..except this one is reversed..
    the baffling thing is..except I swap the values around...
    I think I can continue working from now on..thanks very much Elysia..missed you on this forum

    Thanks Geometrian
    You ended that sentence with a preposition...Bastard!

  3. #18
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What is all this talk of a right triangle? If you have two coordinates you can form a right triangle quite easily but you do not need to for this problem.

    Code:
    // Returns a scalar that is the arccosine of the angle between the vectors
    // v1 and v2 must be unit vectors if the angle is needed - IE: normalized vectors
    // For simple sign comparison v1 and v2 do not need to be normalized
    float Dot(const Vector3 &v1,const Vector3 &v2)
    {
       return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
    }
    This is C++. Note that C# does not have const and Vector3 should be a struct or value type instead of a reference type.

    If you only need the angle between two vectors you simply normalize the vectors, dot them, and take the arccosine of the result to get the angle. If you only need to test the sign of the result (actually more useful than the angle) then the vectors do not need to be normalized as this does not affect the result.

    The dot product is probably the most important operation in computer graphics and is used all over the place because it's geometric meaning is extremely important and applicable to nearly every operation and test you need to perform in graphics and games.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That has already been mentioned, Bubba. In the first two replies, actually. And they don't require you to normalize your vectors first. I don't know if it's common/good practice to normalize vectors in computer graphics, though.
    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.

  5. #20
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Quote Originally Posted by Elysia View Post
    I don't know if it's common/good practice to normalize vectors in computer graphics, though.
    It depends. Generally speaking, raw datas' parameters should be normalized (i.e., your object data should have normalized geometry). When rendering, some things can mess this up--e.g. scaling, linear or nonlinear, (although the normal matrix (transpose of the inverse model*view matrix) counters this).

    However, in some cases, you will need to explicitly normalize your vectors. For example, in a fragment program, normals are linearly interpolated from the vertex program, changing their lengths. Often, the first thing one does in a fragment program is normalize such parameters (also commonly applies to tangents and binormals).

    Ian

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