-
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? :confused:
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. :sick:
TIA.
-
Re: Calculate Endpoint from X,Y,Dist,Heading
Try this:
Sqr(((X1 - X2) ^ 2) + ((Y1 - Y2) ^ 2))
-
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?
-
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.
-
Re: Calculate Endpoint from X,Y,Dist,Heading
Ok I get you now. Give me some time to figure this out....
-
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:
Option Explicit
Private Const PI As Single = 3.141592654
Private Sub Form_Activate()
'Set the Window State to Maximize to see the results.
AutoRedraw = True
ScaleMode = 3
DrawWidth = 10
Dim X1 As Single, Y1 As Single
Dim X2 As Single, Y2 As Single
Dim New_X1 As Single, New_Y1 As Single
Dim New_X2 As Single, New_Y2 As Single
Dim Center_X As Single, Center_Y As Single
Dim Distance As Single
Dim Angle As Single
Center_X = ScaleWidth / 2
Center_Y = ScaleHeight / 2
X1 = 120
Y1 = 120
Distance = 50
'Edit: This is wrong, need to figure this part out
[b]
X2 = Sqr(X1 ^ 2) + Distance
Y2 = Sqr(Y1 ^ 2) + Distance
[/b]
Angle = 250
New_X1 = Center_X + X1
New_Y1 = Center_Y + Y1
PSet (New_X1, New_Y1)
New_X2 = Center_X + (X2 * Cos(Angle * PI / 180)) - (Y2 * Sin(Angle * PI / 180))
New_Y2 = Center_Y + (X2 * Sin(Angle * PI / 180)) + (Y2 * Cos(Angle * PI / 180))
PSet (New_X2, New_Y2), RGB(0, 0, 255)
End Sub
-
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. :thumb:
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
-
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:
X2 = Sqr(X1 ^ 2) + Distance
Y2 = Sqr(Y1 ^ 2) + Distance
-
Re: Calculate Endpoint from X,Y,Dist,Heading
Got it :bigyello:
VB Code:
Option Explicit
Private Const PI As Single = 3.141592654
Private Sub Form_Activate()
'Set the Window State to Maximize to see the results.
AutoRedraw = True
ScaleMode = 3
DrawWidth = 10
Dim X1 As Single, Y1 As Single
Dim X2 As Single, Y2 As Single
Dim New_X1 As Single, New_Y1 As Single
Dim New_X2 As Single, New_Y2 As Single
Dim Center_X As Single, Center_Y As Single
Dim Distance As Single
Dim Angle As Single
Distance = 50
Angle = 250
Center_X = ScaleWidth / 2
Center_Y = ScaleHeight / 2
X1 = 120
Y1 = 120
X2 = Distance
Y2 = 0
New_X1 = Center_X + X1
New_Y1 = Center_Y + Y1
PSet (New_X1, New_Y1)
New_X2 = New_X1 + (X2 * Cos(Angle * PI / 180)) - (Y2 * Sin(Angle * PI / 180))
New_Y2 = New_Y1 + (X2 * Sin(Angle * PI / 180)) + (Y2 * Cos(Angle * PI / 180))
PSet (New_X2, New_Y2), RGB(0, 0, 255)
End Sub
-
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. :D
I love programming but i hate math. :confused:
-
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. :bigyello:
-
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.
-
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.
-
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.
-
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:
to this:
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
-
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?
-
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.
-
Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]
Basically the same with yer one change to orient up.
VB Code:
Private Sub Form_Activate()
'Set the Window State to Maximize to see the results.
AutoRedraw = True
ScaleMode = 3
DrawWidth = 10
Dim X1 As Single, Y1 As Single
Dim X2 As Single, Y2 As Single
Dim New_X1 As Integer, New_Y1 As Integer
Dim New_X2 As Integer, New_Y2 As Integer
Dim Center_X As Single, Center_Y As Single
Dim Distance As Single
Dim Angle As Single
Distance = 20
Angle = 180
Center_X = ScaleWidth / 2
Center_Y = ScaleHeight / 2
X1 = 20
Y1 = 20
X2 = 0
Y2 = -Distance
New_X1 = Center_X + X1
New_Y1 = Center_Y + Y1
PSet (New_X1, New_Y1)
New_X2 = New_X1 + (X2 * Cos(Angle * PI / 180)) - (Y2 * Sin(Angle * PI / 180))
New_Y2 = New_Y1 + (X2 * Sin(Angle * PI / 180)) + (Y2 * Cos(Angle * PI / 180))
PSet (New_X2, New_Y2), RGB(0, 250, 255)
Form1.Caption = New_X2 & " " & New_Y2
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.
-
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:
Option Explicit
Private Const PI As Single = 3.14159265358979 '4 * Atn(1)
Private Sub Form_Activate()
'Set the Window State to Maximize to see the results.
AutoRedraw = True
ScaleMode = 3
DrawWidth = 10
Dim X1 As Single, Y1 As Single
Dim X2 As Single, Y2 As Single
Dim New_X1 As Long, New_Y1 As Long
Dim New_X2 As Long, New_Y2 As Long
Dim Center_X As Single, Center_Y As Single
Dim Distance As Single
Dim Angle As Single
Distance = 50
Angle = 180
Center_X = ScaleWidth / 2
Center_Y = ScaleHeight / 2
X1 = 120
Y1 = 120
X2 = 0
Y2 = -Distance
New_X1 = Center_X + X1
New_Y1 = Center_Y + Y1
PSet (New_X1, New_Y1)
New_X2 = New_X1 + (X2 * Cos(Angle * PI / 180)) - (Y2 * Sin(Angle * PI / 180))
New_Y2 = New_Y1 + (X2 * Sin(Angle * PI / 180)) + (Y2 * Cos(Angle * PI / 180))
PSet (New_X2, New_Y2), RGB(0, 250, 255)
Form1.Caption = New_X1 & " " & New_Y1 & " " & New_X2 & " " & New_Y2
End Sub
It was exactly 50 paces from Y2 south. :bigyello:
-
Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]
I C. I totally forgot the Centering.
-
Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]
Yep, you sure did. :bigyello:
-
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:
Option Explicit
Private center_x, center_y
Private PI
Public Sub EndPoint(StartX As Single, StartY As Single, Distance As Single, Angle As Single, NewX As Single, NewY As Single)
'The equations you want are
'Y2 = Y1 + dist*sin(angle)
'X2 = X1 + dist*cos(angle)
Dim radAngle As Double
radAngle = Angle * PI / 180
NewX = StartX + Distance * Cos(radAngle)
NewY = StartY + Distance * Sin(radAngle)
End Sub
Public Sub PlotPoints(oldX As Single, oldY As Single, NewX As Single, NewY As Single)
Me.PSet (oldX + center_x, oldY + center_y), vbBlack
Me.PSet (NewX + center_x, NewY + center_y), vbRed
End Sub
Private Sub Command1_Click()
Dim NewX As Single, NewY As Single
Dim oldX As Single, oldY As Single
Dim Angle As Single, dist As Single
oldX = 120
oldY = 120
Angle = 180
dist = 500
EndPoint oldX, oldY, dist, Angle, NewX, NewY
PlotPoints oldX, oldY, NewX, NewY
Debug.Print oldX, oldY
Debug.Print NewX, NewY
End Sub
Private Sub Form_Activate()
Me.WindowState = vbMaximized
Me.AutoRedraw = True
Me.ScaleMode = 3
Me.DrawWidth = 10
PI = Atn(1#) * 4#
center_x = Me.ScaleWidth / 2
center_y = Me.ScaleHeight / 2
End Sub
-
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. :bigyello:
It's up to him how he uses it in the function. So no I wasn't overspecifying. :p
-
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.
;)
-
Re: Calculate Endpoint from X,Y,Dist,Heading [Problems Still]
You did it a little wrong in your code, moeur. :bigyello:
-
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!
-
Re: Calculate Endpoint from X,Y,Dist,Heading [Solved]
Quote:
You did it a little wrong in your code, moeur.
Please show me the error of my ways
-
Re: Calculate Endpoint from X,Y,Dist,Heading [Solved]
-
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. ;)
-
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. :p
VB Code:
Private center_x, center_y
Private PI