Am I calculating the dot-product correctly?

Moderator
Posts: 700
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
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
It looks right to me.

What language is that? Rasp

- Alex Diener
Quote this message in a reply
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
Quote this message in a reply
Moderator
Posts: 700
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
Quote this message in a reply
Moderator
Posts: 1,560
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
Quote this message in a reply
Moderator
Posts: 700
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 Wink I can't code Tetris and Puyo Puyo variants my whole life Wink

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
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.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #8
very elegent

sealfin: lol neither can I, 3D is the way
Quote this message in a reply
Sage
Posts: 1,482
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  calculating X and Y coordinates w/ an angle and distance ferum 13 15,270 Jun 25, 2008 10:53 PM
Last Post: rosenth
  Calculating relative transformations TomorrowPlusX 3 2,870 Jul 11, 2007 06:15 PM
Last Post: Skorche
  Dot and Cross product unknown 4 3,746 May 15, 2007 11:39 AM
Last Post: George Marlin
  Calculating direction of car from normal vector and angle spinner 1 3,320 Oct 21, 2006 10:43 AM
Last Post: imikedaman
  Trouble Calculating Height Field Normals Nick 15 6,253 Jul 21, 2005 07:27 AM
Last Post: Nick