News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Find the angle between two line segments

Started by nandagopal, Sep 01, 2008, 07:42 PM

Previous topic - Next topic

nandagopal


The dot product of two vectors A = <x1, y1> and B = <x2, y2> is written A · B and has the value:

    A · B = |A| * |B| * Cos(theta)

where theta is the angle between the two vectors. You can easily calculate the dot product using this equation:

    A · B = x1 * x2 + y1 * y2

The following code calculates the dot product of two vectors. The function takes as parameters the coordinates of three points A, B, and C, and finds the dot product of the vectors AB and BC.


' Return the dot product AB · BC.
' Note that AB · BC = |AB| * |BC| * Cos(theta).
Private Function DotProduct( _
    ByVal Ax As Single, ByVal Ay As Single, _
    ByVal Bx As Single, ByVal By As Single, _
    ByVal Cx As Single, ByVal Cy As Single _
  ) As Single
Dim BAx As Single
Dim BAy As Single
Dim BCx As Single
Dim BCy As Single

    ' Get the vectors' coordinates.
    BAx = Ax - Bx
    BAy = Ay - By
    BCx = Cx - Bx
    BCy = Cy - By

    ' Calculate the dot product.
    DotProduct = BAx * BCx + BAy * BCy
End Function