Results 1 to 30 of 30

Thread: Calculate Endpoint from X,Y,Dist,Heading [Solved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Calculate Endpoint from X,Y,Dist,Heading [Solved]

    Hey folks,

    I didn't find any other threads regarding this question, sorry if I missed one.

    I'm having a major brain fart here. I can calculate distance and heading between points but I can't seem to come up with a function for this:

    _________ +X
    |
    |
    |
    |
    +Y

    I have a square, flat grid up to 2000 by 2000 pixels.

    Given the start X & Y, distance, and heading in degrees, how do I determine the end point X & Y location?

    I'm trying to make a simple function such as this:

    Public Function EndPoint(Map1.Width as integer, Map1.Height as integer, startX as integer, startY as integer ,Dist as integer, Hdg as integer) as string

    ??????

    EndPoint = EndX & "|" & EndY
    End function


    I must be up too many hours or out of school to long.

    TIA.
    Last edited by stmdk; Apr 23rd, 2005 at 09:13 PM.

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading

    Try this:

    Sqr(((X1 - X2) ^ 2) + ((Y1 - Y2) ^ 2))

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading

    That doesn't take distance and heading into account. The start point is moving around the grid.

    To make things more clear let me explain the purpose.

    The start point is the X & Y location of a ship on the water. It's constantly moving and changing heading. I 'm trying to calculate where it will end up provided the distance and heading.

    For instance I'm at X:120 Y:150 Heading 250*. Where will I be if I travel 50 pixels?

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Calculate Endpoint from X,Y,Dist,Heading

    But Jacob, he knows the distance already. What he doesn't know is the X2 and Y2 values.

  5. #5
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading

    Ok I get you now. Give me some time to figure this out....

  6. #6
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading

    Ok you have a ship at x:120 and y:120. You want to know what position it'll be if it went 50 pixels traveling at 250 degrees.

    I just figured it out...sort of. Just need help with 2 lines of code. Try this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const PI As Single = 3.141592654
    4.  
    5. Private Sub Form_Activate()
    6.    
    7.     'Set the Window State to Maximize to see the results.
    8.    
    9.     AutoRedraw = True
    10.     ScaleMode = 3
    11.     DrawWidth = 10
    12.  
    13.     Dim X1 As Single, Y1 As Single
    14.     Dim X2 As Single, Y2 As Single
    15.    
    16.     Dim New_X1 As Single, New_Y1 As Single
    17.     Dim New_X2 As Single, New_Y2 As Single
    18.    
    19.     Dim Center_X As Single, Center_Y As Single
    20.     Dim Distance As Single
    21.     Dim Angle As Single
    22.    
    23.     Center_X = ScaleWidth / 2
    24.     Center_Y = ScaleHeight / 2
    25.    
    26.     X1 = 120
    27.     Y1 = 120
    28.    
    29.     Distance = 50
    30.  
    31.     'Edit: This is wrong, need to figure this part out
    32.     [b]
    33.     X2 = Sqr(X1 ^ 2) + Distance
    34.     Y2 = Sqr(Y1 ^ 2) + Distance
    35.     [/b]
    36.     Angle = 250
    37.    
    38.     New_X1 = Center_X + X1    
    39.     New_Y1 = Center_Y + Y1
    40.    
    41.     PSet (New_X1, New_Y1)
    42.    
    43.     New_X2 = Center_X + (X2 * Cos(Angle * PI / 180)) - (Y2 * Sin(Angle * PI / 180))
    44.     New_Y2 = Center_Y + (X2 * Sin(Angle * PI / 180)) + (Y2 * Cos(Angle * PI / 180))
    45.    
    46.     PSet (New_X2, New_Y2), RGB(0, 0, 255)
    47.        
    48. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading

    Thank you very much!

    I actually searched around an managed to alter some other code to fit the purpose. This is what I came up with but I question its accuracy. You method is more precise.

    Public Function endPoint(startX As Integer, startY As Integer, Dist As _Integer, Hdg As Integer) As String

    'Determines end point given distance, heading, and start.

    Dim endPointX As Integer
    Dim endPointY As Integer
    Dim B As Integer
    B = 1.8

    Select Case Hdg
    Case 0 To 89, 360
    endPointX = Dist - (90 - Hdg Mod 90) ^ B * Dist / 90 ^ B
    endPointY = (Hdg Mod 90) ^ B * Dist / 90 ^ B
    Case 90 To 179
    endPointX = Dist - (Hdg Mod 90) ^ B * Dist / 90 ^ B
    endPointY = Dist - (90 - Hdg Mod 90) ^ B * Dist / 90 ^ B + Dist
    Case 180 To 269
    endPointX = (90 - Hdg Mod 90) ^ B * Dist / 90 ^ B - Dist
    endPointY = Dist - (Hdg Mod 90) ^ B * Dist / 90 ^ B + Dist
    Case 270 To 359
    endPointX = (Hdg Mod 90) ^ B * Dist / 90 ^ B - Dist
    endPointY = (90 - Hdg Mod 90) ^ B * Dist / 90 ^ B
    End Select

    endPoint = endPointX & "-" & endPointY
    End Function

  8. #8
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [SOLVED]

    In my code though I need to figure out X2 and Y2 cause this part is wrong:

    VB Code:
    1. X2 = Sqr(X1 ^ 2) + Distance
    2.     Y2 = Sqr(Y1 ^ 2) + Distance

  9. #9
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading

    Got it

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const PI As Single = 3.141592654
    4.  
    5. Private Sub Form_Activate()
    6.    
    7.     'Set the Window State to Maximize to see the results.
    8.  
    9.     AutoRedraw = True
    10.     ScaleMode = 3
    11.     DrawWidth = 10
    12.  
    13.     Dim X1 As Single, Y1 As Single
    14.     Dim X2 As Single, Y2 As Single
    15.    
    16.     Dim New_X1 As Single, New_Y1 As Single
    17.     Dim New_X2 As Single, New_Y2 As Single
    18.    
    19.     Dim Center_X As Single, Center_Y As Single
    20.     Dim Distance As Single
    21.     Dim Angle As Single
    22.    
    23.     Distance = 50
    24.    
    25.     Angle = 250
    26.    
    27.     Center_X = ScaleWidth / 2
    28.     Center_Y = ScaleHeight / 2
    29.    
    30.     X1 = 120
    31.     Y1 = 120
    32.    
    33.     X2 = Distance
    34.     Y2 = 0
    35.    
    36.     New_X1 = Center_X + X1
    37.     New_Y1 = Center_Y + Y1
    38.    
    39.     PSet (New_X1, New_Y1)
    40.    
    41.     New_X2 = New_X1 + (X2 * Cos(Angle * PI / 180)) - (Y2 * Sin(Angle * PI / 180))
    42.     New_Y2 = New_Y1 + (X2 * Sin(Angle * PI / 180)) + (Y2 * Cos(Angle * PI / 180))
    43.        
    44.     PSet (New_X2, New_Y2), RGB(0, 0, 255)
    45.  
    46. End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading

    Thanks bud.


    Math and programming go hand in hand. Still I feel no joy in solving math problems. Although I think solving a programming problem is better than nookie sometimes.

    I love programming but i hate math.

  11. #11
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Solved by Jacob]

    Me, I'm a math junkie, especially when it comes to working with 3D games that I'm making using DirectX.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading [Solved by Jacob]

    I'd love to learn to use DirectX but I hear it's hell in VB.

    Some of the coding I'm doing now is for an MMO I'm building. The server side is almost complete. I'm currently working on the client side. I think I'm gonna use DarkBasic for the graphic intesive stuff. From what I've read, DarkBasic is super easy to use.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading [Solved by Jacob]

    ooops actually there is still an error. Yer off of 90 degrees clockwise. Easy fix.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading [Solved By Jacob]

    I put in a fix, Angle=Angle-90 that puts it to what looks like the right place however...something strange.

    I set angle at 180. At a distance of 50 the NEW_Y2 should be 230, instead I get 233?? The NEW_X2 should remain the same since we are heading due south but I get 277?

    add this to the bottom and see what I mean.

    MsgBox New_X2 & " " & New_Y2

    It reports back 277 233 for me.

  15. #15
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    You didn't have to do that with the angle. If you wanted up to be the starting position, then change these two lines:

    VB Code:
    1. X2 = Distance
    2.     Y2 = 0

    to this:

    VB Code:
    1. X2 = 0
    2.     Y2 = -Distance

    And DirectX in VB is easier than you think. It's not hell at all. If you want some killer tutorials on DirectX, then go in here:

    http://externalweb.exhedra.com/Direc...T_DX8Start.asp

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    Any ideas about why it's reporting back the wrong numbers when I look at the values of NEW_X2 and NEW_Y2?

  17. #17
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    Yeah, but before I do, I wanna see the new code that you put in your function and I'll find out for ya.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    Basically the same with yer one change to orient up.


    VB Code:
    1. Private Sub Form_Activate()
    2.    
    3.     'Set the Window State to Maximize to see the results.
    4.  
    5.     AutoRedraw = True
    6.     ScaleMode = 3
    7.     DrawWidth = 10
    8.  
    9.     Dim X1 As Single, Y1 As Single
    10.     Dim X2 As Single, Y2 As Single
    11.    
    12.     Dim New_X1 As Integer, New_Y1 As Integer
    13.     Dim New_X2 As Integer, New_Y2 As Integer
    14.    
    15.     Dim Center_X As Single, Center_Y As Single
    16.     Dim Distance As Single
    17.     Dim Angle As Single
    18.    
    19.     Distance = 20
    20.    
    21.     Angle = 180
    22.    
    23.    
    24.     Center_X = ScaleWidth / 2
    25.     Center_Y = ScaleHeight / 2
    26.    
    27.     X1 = 20
    28.     Y1 = 20
    29.    
    30.     X2 = 0
    31.     Y2 = -Distance
    32.    
    33.     New_X1 = Center_X + X1
    34.     New_Y1 = Center_Y + Y1
    35.    
    36.     PSet (New_X1, New_Y1)
    37.    
    38.     New_X2 = New_X1 + (X2 * Cos(Angle * PI / 180)) - (Y2 * Sin(Angle * PI / 180))
    39.     New_Y2 = New_Y1 + (X2 * Sin(Angle * PI / 180)) + (Y2 * Cos(Angle * PI / 180))
    40.        
    41.     PSet (New_X2, New_Y2), RGB(0, 250, 255)
    42. Form1.Caption = New_X2 & "  " & New_Y2
    43. End Sub

    If Y= 20 and I'm heading 20 due south at 180 the NEW_Y2 should by 40. Yet look at the form caption, it says 143.

  19. #19
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    That's because Center_X and Center_Y was included. If you want the values to not include that, simply subtract Center_X from New_X2 and subtract Center_Y from New_Y2

    And this worked by the way:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const PI As Single = 3.14159265358979 '4 * Atn(1)
    4.  
    5. Private Sub Form_Activate()
    6.    
    7.     'Set the Window State to Maximize to see the results.
    8.  
    9.     AutoRedraw = True
    10.     ScaleMode = 3
    11.     DrawWidth = 10
    12.  
    13.     Dim X1 As Single, Y1 As Single
    14.     Dim X2 As Single, Y2 As Single
    15.    
    16.     Dim New_X1 As Long, New_Y1 As Long
    17.     Dim New_X2 As Long, New_Y2 As Long
    18.    
    19.     Dim Center_X As Single, Center_Y As Single
    20.     Dim Distance As Single
    21.     Dim Angle As Single
    22.    
    23.     Distance = 50
    24.    
    25.     Angle = 180
    26.    
    27.     Center_X = ScaleWidth / 2
    28.     Center_Y = ScaleHeight / 2
    29.    
    30.     X1 = 120
    31.     Y1 = 120
    32.    
    33.     X2 = 0
    34.     Y2 = -Distance
    35.    
    36.     New_X1 = Center_X + X1
    37.     New_Y1 = Center_Y + Y1
    38.    
    39.     PSet (New_X1, New_Y1)
    40.    
    41.     New_X2 = New_X1 + (X2 * Cos(Angle * PI / 180)) - (Y2 * Sin(Angle * PI / 180))
    42.     New_Y2 = New_Y1 + (X2 * Sin(Angle * PI / 180)) + (Y2 * Cos(Angle * PI / 180))
    43.        
    44.     PSet (New_X2, New_Y2), RGB(0, 250, 255)
    45.    
    46.     Form1.Caption = New_X1 & "  " & New_Y1 & "  " & New_X2 & "  " & New_Y2
    47.  
    48. End Sub

    It was exactly 50 paces from Y2 south.

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    I C. I totally forgot the Centering.

  21. #21
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    Yep, you sure did.

  22. #22
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    You guys are over specifying the problem. You either give a new distance and angle or a new x and y not both
    I thought you wanted to specify only distance and angle
    VB Code:
    1. Option Explicit
    2.  
    3. Private center_x, center_y
    4. Private PI
    5.  
    6. Public Sub EndPoint(StartX As Single, StartY As Single, Distance As Single, Angle As Single, NewX As Single, NewY As Single)
    7. 'The equations you want are
    8. 'Y2 = Y1 + dist*sin(angle)
    9. 'X2 = X1 + dist*cos(angle)
    10. Dim radAngle As Double
    11.  
    12.  radAngle = Angle * PI / 180
    13.  NewX = StartX + Distance * Cos(radAngle)
    14.  NewY = StartY + Distance * Sin(radAngle)
    15.  
    16. End Sub
    17.  
    18. Public Sub PlotPoints(oldX As Single, oldY As Single, NewX As Single, NewY As Single)
    19.     Me.PSet (oldX + center_x, oldY + center_y), vbBlack
    20.     Me.PSet (NewX + center_x, NewY + center_y), vbRed
    21. End Sub
    22.  
    23.  
    24. Private Sub Command1_Click()
    25. Dim NewX As Single, NewY As Single
    26. Dim oldX As Single, oldY As Single
    27. Dim Angle As Single, dist As Single
    28.  
    29.     oldX = 120
    30.     oldY = 120
    31.     Angle = 180
    32.     dist = 500
    33.     EndPoint oldX, oldY, dist, Angle, NewX, NewY
    34.     PlotPoints oldX, oldY, NewX, NewY
    35.  
    36.     Debug.Print oldX, oldY
    37.     Debug.Print NewX, NewY
    38.  
    39. End Sub
    40.  
    41.  
    42. Private Sub Form_Activate()
    43.     Me.WindowState = vbMaximized
    44.     Me.AutoRedraw = True
    45.     Me.ScaleMode = 3
    46.     Me.DrawWidth = 10
    47.     PI = Atn(1#) * 4#
    48.     center_x = Me.ScaleWidth / 2
    49.     center_y = Me.ScaleHeight / 2
    50. End Sub

  23. #23
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    Oh that small program I had going in the Form_Activate event was just to solve a little math problem I had earlier, and I managed to solve it.

    It's up to him how he uses it in the function. So no I wasn't overspecifying.

  24. #24
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    Oh I see... said the blind man as he picked up his hammer and saw.

  25. #25
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    You did it a little wrong in your code, moeur.

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    19

    Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]

    My intention was to get a function that would simply return the X and Y coords of an end point given a start, distance, and heading.

    The purpose of this is for simulation whereby a moving submarine on a map could see its destination at its current course.

    It also allowed me to build a side contour map using elevations 20 points forward and 20 points at back azimuth.

    It's already in the simulation and runs great. Thanks guys!

  27. #27
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculate Endpoint from X,Y,Dist,Heading [Solved]

    You did it a little wrong in your code, moeur.
    Please show me the error of my ways

  28. #28
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Solved]

    Anytime man.

  29. #29
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Solved]

    Quote Originally Posted by moeur
    Please show me the error of my ways
    You used a command button. It's in the way.

  30. #30
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Calculate Endpoint from X,Y,Dist,Heading [Solved]

    Another problem, you didn't declare these variables with a data type. Big no no there.

    VB Code:
    1. Private center_x, center_y
    2. Private PI

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