Am I calculating the dotproduct correctly?
Yay, the latest installment in the saga of "Mark's poor maths strikes back"; I'm just wondering if I'm calculating the angle between two vectors, using the dotproduct, correctly?
This looks correct, as my empirically determined angle was ~38Â°, but I'm just wondering if somebody who actually knows this math could confirm? If so, am I right in my assumption that if the result of the dotproduct â‰¥ 1.0, the two vectors are parallel?
Code:
a = [ 1, 3 ]
b = [ 1, 3 ]
aNorm = a / a = [ 0.316, 0.949 ]
bNorm = b / b = [ 0.316, 0.949 ]
aNorm Â· bNorm = 0.800745
angle = acos 0.800745 = 36.798Â°
This looks correct, as my empirically determined angle was ~38Â°, but I'm just wondering if somebody who actually knows this math could confirm? If so, am I right in my assumption that if the result of the dotproduct â‰¥ 1.0, the two vectors are parallel?
Mark Bishop

Student and freelance OS X & iOS developer
It looks right to me.
What language is that?
 Alex Diener
What language is that?
 Alex Diener
a.b = a.x*b.x + a.y*b.y + a.z*b.z
(a.b)/(ab)=cos(theta)
theta is the smallest angle between the two vectors from the origin
(a.b)/(ab)=cos(theta)
theta is the smallest angle between the two vectors from the origin
Thanks ThemsAllTook, unknown! So the only question remaining is whether I'm correct in my assumption that if the result of the dotproduct â‰¥ 1.0, the two vectors are parallel?
Mark Bishop

Student and freelance OS X & iOS developer
Yes, that's correct, although you'll probably want to fudge it a bit because of rounding. > 0.999 or so = parallel. Depends on what you're doing, of course...
 Alex Diener
 Alex Diener
Thanks again! Thanks, I know I'll probably need to massage the results a little, but at the moment all my checks for parallel vectors are resulting in â‰¥1.00x or so, so I'm not needing much in the way of inaccuracy massaging...
Unfortunately this looks like one of those situations where what I've looked up is no relation to what I was looking for; oh well, I needed to improve my mathematical abilities anyway I can't code Tetris and Puyo Puyo variants my whole life
Unfortunately this looks like one of those situations where what I've looked up is no relation to what I was looking for; oh well, I needed to improve my mathematical abilities anyway I can't code Tetris and Puyo Puyo variants my whole life
Mark Bishop

Student and freelance OS X & iOS developer
Another way to check whether the vectors are parallel, which doesn't require the calculation of an angle, would be to check for linear independence. To do this, simply divide the x component of the one vector by the x component of the other vector. Then, multiply the result with the y component of the second vector and check whether it is equal to the y component of the first vector. Do the same with z. So what I mean is:
r = a.x / b.x
if (a.y != b.y * r) => not parallel
if (a.z != b.z * r) => not parallel
This should work faster in case you don't need the angle, because you don't have to compute the length of the vectors.
r = a.x / b.x
if (a.y != b.y * r) => not parallel
if (a.z != b.z * r) => not parallel
This should work faster in case you don't need the angle, because you don't have to compute the length of the vectors.
very elegent
sealfin: lol neither can I, 3D is the way
sealfin: lol neither can I, 3D is the way
Just to be clear, that's not exactly the dot product. For the dot product, you just need to multiply the similar components together and add. (x*x + y*y)
You only need to normalize the vectors if you want the cosine of the angle. Otherwise you won't get nice numbers in the 1 to 1 range.
However, you might not care. If you only need to know if two vectors are traveling together or apart for instance. Then you need only to check the sign.
You only need to normalize the vectors if you want the cosine of the angle. Otherwise you won't get nice numbers in the 1 to 1 range.
However, you might not care. If you only need to know if two vectors are traveling together or apart for instance. Then you need only to check the sign.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
calculating X and Y coordinates w/ an angle and distance  ferum  13  18,631 
Jun 25, 2008 10:53 PM Last Post: rosenth 

Calculating relative transformations  TomorrowPlusX  3  3,447 
Jul 11, 2007 06:15 PM Last Post: Skorche 

Dot and Cross product  unknown  4  5,175 
May 15, 2007 11:39 AM Last Post: George Marlin 

Calculating direction of car from normal vector and angle  spinner  1  4,069 
Oct 21, 2006 10:43 AM Last Post: imikedaman 

Trouble Calculating Height Field Normals  Nick  15  7,716 
Jul 21, 2005 07:27 AM Last Post: Nick 