Am I calculating the dot-product correctly?

Moderator
Posts: 713
Joined: 2002.04
Post: #1
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?

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 dot-product â‰¥ 1.0, the two vectors are parallel?

Mark Bishop
--
Student and freelance OS X & iOS developer
Moderator
Posts: 1,563
Joined: 2003.10
Post: #2
It looks right to me.

What language is that?

- Alex Diener
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
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
Moderator
Posts: 713
Joined: 2002.04
Post: #4
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?

Mark Bishop
--
Student and freelance OS X & iOS developer
Moderator
Posts: 1,563
Joined: 2003.10
Post: #5
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
Moderator
Posts: 713
Joined: 2002.04
Post: #6
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

Mark Bishop
--
Student and freelance OS X & iOS developer
Member
Posts: 114
Joined: 2005.03
Post: #7
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.
Sage
Posts: 1,403
Joined: 2005.07
Post: #8
very elegent

sealfin: lol neither can I, 3D is the way
Sage
Posts: 1,487
Joined: 2002.09
Post: #9
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.