View Full Version : Am I calculating the dot-product correctly?
sealfin
2005.07.13, 08:46 AM
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 dot-product, correctly?
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 dot-product ≥ 1.0, the two vectors are parallel?
ThemsAllTook
2005.07.13, 10:33 AM
It looks right to me.
What language is that? :p
- Alex Diener
unknown
2005.07.13, 02:56 PM
a.b = a.x*b.x + a.y*b.y + a.z*b.z
(a.b)/(|a||b|)=cos(theta)
theta is the smallest angle between the two vectors from the origin
sealfin
2005.07.13, 03:46 PM
Thanks ThemsAllTook, unknown! So the only question remaining is whether I'm correct in my assumption that if the result of the dot-product ≥ 1.0, the two vectors are parallel?
ThemsAllTook
2005.07.13, 04:00 PM
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
sealfin
2005.07.13, 04:19 PM
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 ;)
Cochrane
2005.07.13, 06:10 PM
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.
unknown
2005.07.13, 07:22 PM
very elegent
sealfin: lol neither can I, 3D is the way
Skorche
2005.07.13, 08:02 PM
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.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.