-
November 7th, 2011, 07:29 AM
#1
vb
Dim a As Double
Dim c As Double
Dim d As Double
Dim Dlt As Double
Dim DLon As Double
Private Const pi As Double = 3.14159265358979
Public Function atan2(y As Double, x As Double) As Double
If x > 0 Then
atan2 = Atn(y / x)
ElseIf x < 0 Then
atan2 = Sgn(y) * (pi - Atn(Abs(y / x)))
ElseIf y = 0 Then
atan2 = 0
Else
atan2 = Sgn(y) * pi / 2
End If
End Function
Private Sub Command1_Click()
lat1 = (Text1.Text * pi) / 180
lat2 = (Text3.Text * pi) / 180
Long1 = (Text2.Text * pi) / 180
Long2 = (Text4.Text * pi) / 180
dlat = Abs(L1t1 - lat2)
DLon = Abs(Long1 - Long2)
r = 6371
a = (Math.Sin(dlat / 2)) ^ 2 + Math.Cos(lat1) * Math.Cos(lat2) * (Sin(DLon / 2)) ^ 2
c = 2 * atan2(Sqr(a), Sqr(1 - a))
d = r * c
Text5.Text = d
End Sub
My program coding are as above,for Haversine formula to calculate great circle distances,p1 and p2 coordinates as below in decimal dergrees
p1
lat1-7.000664 N
lat2-80.411697 E
p2
long1-6.548509 N
long2-80.412038 E
Expected answer is 50 km,but the program giving 728.160
I can't recognize what is my fault.im using VB 6,
Can anyone help me please...
-
November 7th, 2011, 12:25 PM
#2
Re: vb
Hello
I am not sure "VB" us an apropriate title !
A Textbox contains text, not numeric
You then have to convert into numerics (using Val() function
-
November 7th, 2011, 12:36 PM
#3
Re: vb
And, what about the CODE TAGS we keep telling you about? Go back and EDIT your post(s)
Code:
' This is a code block
-
November 7th, 2011, 02:22 PM
#4
Re: vb
If I were to guess I would say the problem is likely here
Code:
a = (Math.Sin(dlat / 2)) ^ 2 + Math.Cos(lat1) * Math.Cos(lat2) * (Sin(DLon / 2)) ^ 2
I'm not sure what the formula is but the order of operations can throw a wrench in the works, depending on the exact formula you may need one or more sets of () in there to get the correct result.
Also if your text box actually conatins the letters N E then you could be running into an issue before you get to the calculation as 1N is not a valid number may need some additional parsing before the calculation. In some cases VB will pull a number from your alpha numeric string it is never a good idea to depend on VB to assume this is what you want to do. Code should always be explicit.
Last edited by DataMiser; November 7th, 2011 at 02:28 PM.
Always use [code][/code] tags when posting code.
-
November 7th, 2011, 05:20 PM
#5
-
November 8th, 2011, 03:31 AM
#6
Re: vb haversine formula
thank you all for your help. I've done the changes according to replies,I've used the val() function,N E not a part o text box and changed the function for atn2. but still it is giving the incorrect answer,i expect your further help to solve my problem.thank you.
-
November 8th, 2011, 02:33 PM
#7
Re: vb
Looks like VB.Net, anyways. I'd expect you NOT to expect an answer, with the code you have posted.
-
November 9th, 2011, 05:38 PM
#8
Re: vb
@cham2748
Code:
dlat = Abs(L1t1 - lat2)
Change "L1t1" to "lat1"
I recieve the value "50.2773561559658" which I think is what you are looking for right?
@dglienna
I doubt you noticed but cham2748's main language is most likely not English and having lived with someone from India common saying do not translate well and many an awkard moment was had with friends and family due to this.
-
November 10th, 2011, 12:45 AM
#9
Re: vb
Originally Posted by DemonPiggies
@cham2748
Code:
dlat = Abs(L1t1 - lat2)
Change "L1t1" to "lat1"
I recieve the value "50.2773561559658" which I think is what you are looking for right?
@dglienna
I doubt you noticed but cham2748's main language is most likely not English and having lived with someone from India common saying do not translate well and many an awkard moment was had with friends and family due to this.
Thank you this is the place where I gone wrong...
-
November 12th, 2011, 09:28 PM
#10
Re: vb
I'm getting negative bearing in atan2 function.How do I convert result to compass bearing to get the final answer..?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|