CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: vb

  1. #1
    Join Date
    Nov 2011
    Posts
    5

    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...

  2. #2
    Join Date
    Oct 2006
    Posts
    327

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: vb

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Nov 2011
    Posts
    5

    Unhappy 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.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: vb

    Looks like VB.Net, anyways. I'd expect you NOT to expect an answer, with the code you have posted.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Apr 2006
    Location
    Rhode Island
    Posts
    32

    Talking 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.

  9. #9
    Join Date
    Nov 2011
    Posts
    5

    Re: vb

    Quote Originally Posted by DemonPiggies View Post
    @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...

  10. #10
    Join Date
    Nov 2011
    Posts
    5

    Smile 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
  •  





Click Here to Expand Forum to Full Width

Featured