Results 1 to 3 of 3

Thread: math equantion

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    math equantion

    i want write a program to calculate distance using formula below:
    d = acos(sin(Lat1) × sin(Lat2) + cos(Lat1) × cos(Lat2) × cos(Lon1 – Lon2))

    but, i find that vb6 not support "acos". how to solve this problem?

    also how to write formula below in vb.

    rad_dist = atan(-t5 / sqrt(-t5 * t4 + 1)) + 2 * atan(1)

    how to write sqrt in vb code.

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

  3. #3
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: math equantion

    The beauty with programming is what you need has probably been needed before..

    vb Code:
    1. ' Provided by Vincent DeCampo 12/28/03
    2.  
    3. Option Explicit
    4.  
    5. Public Const pi = 3.1415927
    6.  
    7. Global COS_LUT(359) As Single
    8. Global SIN_LUT(359) As Single
    9.  
    10. ' Provided by Vincent DeCampo 12/28/03
    11. '
    12. ' This function pre-calculates the SIN/COS of every angle from 0-359 degrees.
    13. ' Instead of using the SIN/COS function you can simply access the array of
    14. ' values to get the desired angle. (ie: COS_LUT(45) gives the Cosine of 45 degrees.
    15. '
    16. Public Sub Calc_SIN_COS_LUT()
    17. Dim Counter As Long
    18.  
    19. For Counter = 0 To 359 Step 1
    20. COS_LUT(Counter) = Cos(Deg2Rad(CSng(Counter)))
    21. SIN_LUT(Counter) = Sin(Deg2Rad(CSng(Counter))) * -1
    22. Next Counter
    23.  
    24. End Sub
    25.  
    26. ' Provided by Vincent DeCampo 12/28/03
    27. '
    28. ' This function will calculate the linear distance between any 2 sets of coordinates.
    29. ' Useful for when you want to activate effects or other sprites based on proximity.
    30. '
    31. Public Function Distance(x1 As Single, y1 As Single, x2 As Single, y2 As Single) As Single
    32.  
    33. Distance = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
    34.  
    35. End Function
    36.  
    37. ' Provided by Vincent DeCampo 12/28/03
    38. '
    39. ' This function will return the angle from origin 0,0 to X,Y
    40. ' Useful to figure out in which direction a set of coordinates
    41. ' is from another. By taking 2 sets of coordinates and normalizing them
    42. ' (ie: subtract X2-X1, Y2-Y1) will let you find the angle of any coordinate (X1,Y1)
    43. ' to any other coordinate. (X2,Y2)
    44. '
    45. Public Function OriginAngle(X As Single, Y As Single) As Single
    46. Dim Quadrant As Single
    47. Dim Angle As Single
    48. Dim Hyp As Single
    49. Dim Leg As Single
    50.  
    51. If (X = 0) And (Y = 0) Then Exit Function
    52.  
    53. Hyp = Distance(0, 0, X, Y)
    54. Leg = Distance(X, 0, X, Y)
    55.  
    56. If Not (Hyp = 0) Then
    57.  
    58. If (X >= 0) And (Y <= 0) Then
    59.   OriginAngle = ArcSin((1) * Leg / Hyp)
    60. ElseIf (X <= 0) And (Y <= 0) Then
    61.   OriginAngle = pi - ArcSin((1) * Leg / Hyp)
    62. ElseIf (X <= 0) And (Y >= 0) Then
    63.   OriginAngle = ArcSin((1) * Leg / Hyp) + pi
    64. ElseIf (X >= 0) And (Y >= 0) Then
    65.   OriginAngle = (2 * pi) - ArcSin((1) * Leg / Hyp)
    66. End If
    67.  
    68. End If
    69.  
    70. End Function
    71.  
    72. ' Provided by Vincent DeCampo 12/28/03
    73. '
    74. ' This function converts Degrees to Radians
    75. '
    76. Public Function Deg2Rad(Deg As Single) As Single
    77.  
    78. Deg2Rad = 6.28 * (Deg / 360)
    79.  
    80. End Function
    81.  
    82. ' Provided by Vincent DeCampo 12/28/03
    83. '
    84. ' This function converts Radians to Degrees
    85. '
    86. Public Function Rad2Deg(Rad As Single) As Single
    87.  
    88. Rad2Deg = 360 * (Rad / (pi * 2))
    89.  
    90. End Function
    91.  
    92. ' Provided by Vincent DeCampo 12/28/03
    93. '
    94. ' This function calculates the ArcSin
    95. '
    96. Public Function ArcSin(arg As Single) As Single
    97.  
    98. If arg > 1 Or arg < -1 Then 'outside domain!
    99. ArcSin = 0
    100. Exit Function
    101. End If
    102.  
    103. If arg = 1 Then 'div by 0 checks
    104.    ArcSin = pi / 2#
    105.    Exit Function
    106. End If
    107.  
    108. If arg = -1 Then
    109.    ArcSin = pi / -2#
    110. Else
    111.    ArcSin = Atn(arg / Sqr(1 - (arg ^ 2)))
    112. End If
    113.  
    114. End Function
    115.  
    116. ' Provided by Vincent DeCampo 12/28/03
    117. '
    118. ' This function will take a set of coordinates and rotate them around
    119. ' an origin 0,0 by so many degrees and return a NewX, NewY set of coordinates.
    120. ' You can use the Normalizing technique mentioned earlier to convert any set of
    121. ' coordinates to origin 0,0.
    122. '
    123. Public Sub RotateCoords(X As Single, Y As Single, degRotation As Single, NewX As Single, NewY As Single)
    124. Dim Radius As Single
    125. Dim Angle As Single
    126. Dim NewAngle As Single
    127. Dim RotAngle As Single
    128.  
    129. If (degRotation > 359) Or (degRotation < -359) Then Exit Sub
    130.  
    131. Radius = Distance(0, 0, X, Y)
    132. Angle = OriginAngle(X, Y)
    133. RotAngle = Deg2Rad(degRotation)
    134. NewAngle = Angle + RotAngle
    135.  
    136. NewX = Cos(NewAngle) * Radius
    137. NewY = Sin(NewAngle) * Radius
    138.  
    139. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width