ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
The dot product of two vectors is equal to the cosine of the angle between them divided by the vectors' magnitudes. For example:
The dot product of <1, 2> and <3, 4> is 1*3 + 2*4, i.e. 11. The magnitudes of the vectors are sqrt(1*1+2*2) and sqrt(3*3+4*4), i.e. sqrt(5) and sqrt(25).
So the cosine of the angle between the vectors is 11 / (sqrt(5)*sqrt(25)), i.e. 11 / (sqrt(5) * 5).
If we take the arccosine of that value, we get the angle:
acos(11/(sqrt(5)*5)).
So in general, the angle between two vectors u and v is
acos(dot(u, v) / sqrt(dot(u, u) * dot(v, v))),
where dot is the dot product function.
(It happens that the magnitude of a vector v can be written as sqrt(dot(v, v)).)
And if you haven't figured it out, the dot product of two vectors is the sum of the products of their constituent parts: `dot` = x*x' + y*y' + z*z'. This works for any number of dimensions.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
The Vector structure of C# also has a "dotproduct" method. (among many others)
But it may be a bit hard to find.
private Double getDotProductExample()
{
Vector vector1 = new Vector(20, 30);
Vector vector2 = new Vector(45, 70);
Double doubleResult;
// Return the dot product of the two specified vectors.
// The dot product is calculated using the following
// formula: (vector1.X * vector2.X) + (vector1.Y * vector2.Y).
// doubleResult is equal to 3000
doubleResult = Vector.Multiply(vector1, vector2);
return doubleResult;
}
You can also use the overloaded * operator here.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661