Search code examples
glslshaderwebgl

What does this shader code return for two vectors a, b, given an angle of 45 degrees between them?


What is the value returned by:

dot(normalize(a), normalize(b))

given that the angle between the vectors a and b is 45°.

  • 0
  • 1
  • sqrt(2)
  • 1 / sqrt(2)

Solution

  • In general The dot product of 2 vectors is equal the cosine of the angle between the 2 vectors multiplied by the magnitude (length) of both vectors.

    dot( A, B ) == | A | * | B | * cos( angle_A_B ) 
    

    This follows, that the dot product of 2 unit vectors is equal the cosine of the angle between the 2 vectors, because the length of a unit vector is 1.

    uA = normalize( A )
    uB = normalize( B )
    cos( angle_A_B ) == dot( uA, uB )
    

    A dot B


    This means that, if the angle between a vector a and b is 45 degrees, then:

    dot(normalize(a), normalize(b))  =  cos(45°)  =  1 / sqrt(2)
    

    Note, the length of the diagonal in a square with a side length of 1, is sqrt(2). If the lenght of the diagonal is 1, then the length of one side is 1 / sqrt(2).