24 | 05 | 2017

Angle Between Coordinates

The Function below calculates the Angle bewteen Coordinates P1 and P2. 

Public Function AngFromX(P1, P2) As Double

'****************************************
'*** Code from VisibleVisual.com ********
'****************************************
 
Dim Dx As Double, dY As Double
Dim dAng As Double
 
 
dY = P2(1) - P1(1): Dx = P2(0) - P1(0)
If Rd(dY, 0) Then 'Line is horizontal
If Rd(Dx, 0) Then Exit Function
If Dx > 0 Then
dAng = 0
Else
dAng = pi '180
End If
ElseIf Rd(Dx, 0) Then 'Line is vertical
If dY > 0 Then
dAng = 0.5 * pi '90
Else
dAng = 1.5 * pi '270
End If
Else
dAng = Atn(dY / Dx)
If dAng < 0 Then '90->270
If Dx < 0 Then '90->270
dAng = pi + dAng '90->180
Else '270->360
dAng = 2 * pi + dAng
End If
Else
If Dx < 0 And dY < 0 Then '180->270
dAng = pi + dAng
End If
End If
End If
AngFromX = dAng
 
End Function

Test the Function using the following code

Sub TestDistanceFunction()
 
Dim coordinates(0 To 2) As Double
Dim coordinates2(0 To 2) As Double
 
'Set Coordinates
coordinates(0) = 1: coordinates(1) = 3: coordinates(2) = 4
coordinates2(0) = 10: coordinates2(1) = 10: coordinates2(2) = 10
 
'test Function
MsgBox DistanceBtween2Points(coordinates, coordinates2)
 
End Sub
 

Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2258940
Latest Articles