Share via


Vector2.Dot Method (Vector2%, Vector2%, Single%)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Calculates the dot product of two vectors and writes the result to a user-specified variable. If the two vectors are unit vectors, the dot product returns a floating point value between -1 and 1 that can be used to determine some properties of the angle between two vectors. For example, it can show whether the vectors are orthogonal, parallel, or have an acute or obtuse angle between them.

Namespace:  Microsoft.Xna.Framework
Assembly:  Microsoft.Xna.Framework.Math (in Microsoft.Xna.Framework.Math.dll)

Syntax

'Declaration
Public Shared Sub Dot ( _
    ByRef value1 As Vector2, _
    ByRef value2 As Vector2, _
    <OutAttribute> ByRef result As Single _
)
public static void Dot(
    ref Vector2 value1,
    ref Vector2 value2,
    out float result
)

Parameters

  • result
    Type: System.Single%
    [OutAttribute] The dot product of the two vectors.

Remarks

The dot product is also known as the inner product.

For any two vectors, the dot product is defined as:

(vector1.X * vector2.X) + (vector1.Y * vector2.Y)

The result of this calculation, plus or minus some margin to account for floating point error, is equal to:

Length( vector1 ) * Length( vector2 ) * System.Math.Cos( theta )

Here, theta is the angle between the two vectors.

If vector1 and vector2 are unit vectors, the length of each vector will be equal to 1. So, when vector1 and vector2 are unit vectors, the dot product is simply equal to the cosine of the angle between the two vectors. The following calculation:

Vector2.Dot( vector1.Normalize(), vector2.Normalize() )

Is equivalent to:

System.Math.Cos( theta )

Therefore, if vector1 and vector2 are unit vectors (i.e., we've called Normalize on each vector to make it a unit vector), without knowing the value of theta or using a potentially processor-intensive trigonometric function, the dot product can tell us the following things:

  1. If Vector2.Dot( vector1.Normalize(), vector2.Normalize() ) > 0 , the angle between the two vectors is less than 90 degrees.

  2. If Vector2.Dot( vector1.Normalize(), vector2.Normalize() ) < 0 , the angle between the two vectors is more than 90 degrees.

  3. If Vector2.Dot( vector1.Normalize(), vector2.Normalize() ) == 0, the angle between the two vectors is 90 degrees; that is, the vectors are orthogonal.

  4. If Vector2.Dot( vector1.Normalize(), vector2.Normalize() ) == 1, the angle between the two vectors is 0 degrees; that is, the vectors point in the same direction and are parallel.

  5. If Vector2.Dot( vector1.Normalize(), vector2.Normalize() ) == -1, the angle between the two vectors is 180 degrees; that is, the vectors point in opposite directions and are parallel.

Caution noteCaution:

Because of floating point error, two orthogonal vectors may not return a dot product that is exactly zero. It might be zero plus some amount of floating point error. In your code, you will want to determine what amount of error is acceptable in your calculation, and take that into account when you do your comparisons.

Version Information

Silverlight

Supported in: 5

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.