Results 1 to 6 of 6

Thread: [RESOLVED] Get the distance of two points on a coordinate grid.

  1. #1

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Resolved [RESOLVED] Get the distance of two points on a coordinate grid.

    I'm sure some of you know the equation to get the distance of 2 points on a coordinate grid, and I'm making a program to do just that, the only problem is that when I use the square root function it doesn't return an exact number, what's wrong with my code?
    vb Code:
    1. Dim P1 As Integer = CInt(X2.Text) - CInt(X1.Text)
    2.         Dim P2 As Integer = CInt(Y2.Text) - CInt(Y1.Text)
    3.         Dim XX1 As Integer = P1 * P1
    4.         Dim YY1 As Integer = P2 * P2
    5.         Dim W1 As Integer = XX1 + YY1
    6.         Dim Sq As Integer = System.Math.Sqrt(W1)
    7.         Sol.Text = Sq

    I don't get why it automatically rounds the number up. Can anyone tell me how to code it to get an exact square root?

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Get the distance of two points on a coordinate grid.

    Do you really want it to return an integer? By its very definition this will round the answer.

    I would have thought you ought to be using a double as this is what the Sqrt function returns.

  3. #3

  4. #4

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Re: Get the distance of two points on a coordinate grid.

    Quote Originally Posted by keystone_paul View Post
    Do you really want it to return an integer? By its very definition this will round the answer.

    I would have thought you ought to be using a double as this is what the Sqrt function returns.
    Yeah, I figured that out after a while, but thanks anyway.

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Get the distance of two points on a coordinate grid.

    Quote Originally Posted by DerekM View Post
    Yeah, I figured that out after a while, but thanks anyway.
    Glad you figured it out, but it would be helpful if you'd marked your thread "resolved" using the thread tools menu, so people realise you don't need any help and don't waste their breath!

  6. #6

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Re: Get the distance of two points on a coordinate grid.

    Quote Originally Posted by keystone_paul View Post
    Glad you figured it out, but it would be helpful if you'd marked your thread "resolved" using the thread tools menu, so people realise you don't need any help and don't waste their breath!
    Yeah, it's been awhile since I've been on the forums, I tend to forget simple thing like that.

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