20 | 09 | 2017

Distance Between to Coordinates

The Function below calculates the distance bewteen Coordinates A and B.  

 

Function DistanceBtween2Points(A, B) As Variant

Dim Ax As Double, Ay As Double, Az As Double
Dim Bx As Double, By As Double, Bz As Double
Dim Unit As Double
Dim C(2) As Double

'Get Distance

Ax = A(0): Ay = A(1): Az = A(2)
Bx = B(0): By = B(1): Bz = B(2)

C(0) = Ay * Bz - Az * By
C(1) = Az * Bx - Ax * Bz
C(2) = Ax * By - Ay * Bx

'Convert to unit normal
Unit = Sqr(C(0) * C(0) + C(1) * C(1) + C(2) * C(2))
C(0) = C(0) / Unit: C(1) = C(1) / Unit: C(2) = C(2) / Unit


DistanceBtween2Points = C

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
2353585
Latest Articles