Results 1 to 7 of 7

Thread: Angles between 2 lines

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    Try this

    Option Explicit
    
    'based on
    'ref http://www.devx.com/tips/Tip/33124
    
    Const cPI As Double = 3.14159265358979
    
    Function getAngleBetweenLinesVect(commonX As Double, commonY As Double, _
        X1 As Double, Y1 As Double, _
        Y2 As Double, X2 As Double) As Double
        
        Dim aRadians As Double
        Dim diffX1 As Double, diffX2 As Double, diffY1 As Double, diffY2 As Double
        
        'check if both the lines are the same, if they are exit the function
        If (commonY = Y1 And commonX = X1) Or _
            (commonY = Y2 And commonX = X2) Or _
            (Y1 = Y2 And X1 = X2) Then
            
            getAngleBetweenLinesVect = 0#
            Exit Function
        End If
        
        'set the variables
        diffX1 = commonX - X1
        diffY1 = commonY - Y1
        diffX2 = commonX - X2
        diffY2 = commonY - Y2
    
        'applying the angle between two lines vector rule, calculate the angle in radians
        aRadians = invCos(((diffX1 * diffX2) + (diffY1 * diffY2)) / _
            (Sqr(diffX1 ^ 2 + diffY1 ^ 2) * Sqr(diffX2 ^ 2 + diffY2 ^ 2)))
    
        'convert to degrees
        getAngleBetweenLinesVect = aRadians * 180# / cPI
    End Function
    
    'the inverse cos function. Returns an angle.
    Public Function invCos(X As Double) As Double
        If X > 1 Or X < -1 Then
        'if the ratio supplies is outside the acceptable range.
        ElseIf X = 1 Then
            invCos = 0
        ElseIf X = -1 Then
            invCos = cPI
        
        Else
            invCos = (Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1))
        End If
    End Function
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •