Find resulting point by given point, angle and distance

JuliaR
09-05-2003, 10:42 AM
I need someone with a descent understanding of math to help me out with this little problem.

Given an origin coordinate, angle and distance, calculate the resulting point's x,y

So far I have the following:

Public Type Point
X As Integer
Y As Integer
End Type

Function Get_Point(ByRef ptOrigin As Point, ByVal angle As Integer, ByVal distance As Integer) As Point

'Do math stuff here

Get_Point.X = resultingX
Get_Point.Y = resultingY

End Function

Any help is much appreciated

SnakeChomp
09-05-2003, 10:45 AM
We are talking about points on a 2D plane I assume. You need to make use of the Sine / Cosine functions, and multiply the result you get from them by the length (distance) of the line. This will give you the destination point if the origin was 0, but since it wont be zero all the time, add the X and Y values of the origin to the X and Y you got from using the trig functions. If you don't know what Sine / Cosine are (they are trigonometry terms) ask and I will explain them in detail.

Thinker
09-05-2003, 10:49 AM
And you will need some floating point variables to work with, especially
one to hold the angle in Radians after you convert it from degrees.

JuliaR
09-05-2003, 10:50 AM
I would appreciate if you could explain in more detail. I have no experience or exposure to trig so this is not the easiest of topics for me.

Yes it is a point in 2D. (x,y)

SnakeChomp
09-05-2003, 10:59 AM
Trigonometry uses triangles to solve problems. On your 2D graph, you have a point, at (X,Y). What you are trying to find will also be a point (X2, Y2). If you connect Point 1 to Point 2, you get the hypotenuse of a triangle (the longest side). If you draw straight lines going either horizontally or vertically from Point 1 and Point 2 so that they meet at a 90 degree angle, you have created a right triangle. The basis of using trig to solve 2D plane problems is the fact that any two points form a triangle. In your case, you have the angle of the hypotenuse relative to the base of the triangle (this is assuming that your distance line points up and to the right of your first point). You can now use Sine and Cosine to find the length of the two other sides in the triangle.

Please consult the attached image for more information.

The triangle has three points making it up, A B C. A is the origin point you are given. Point B is what you need to find. The hyptenuse is the longest side of the trangle, which is your distance measurement. For now the origin point doesnt matter, so lets ignore it.

What you need to find is the X and Y of the second point. If you notice, when the origin is 0,0 the base line of the triangle ends exactly on point B's X value. In other words, X = Length of base. The right line in the triangle follows the same principle. Y = Height of Triangle. In order to find the length and height, you use sine and cosine.

Sine is defined as: Sine(angle) = Length of Opposite side / Hypotenuse. When working with the angle at point A, (angle a, in lower case) the side opposite of this angle is the side formed by points B and C. The opposite side of angle a is the side you want to use to find the Y coordinate. Since you know the angle, as well as the hypotenuse, rearrange the equation to solve for the length of the opposite side. The equation becomes Sine(a) / Hypotenuse = Length Opposite Side = Y coordinate of B. Cosine is similar
Cosine is defined as: Cosine(angle) = Length of Adjacent side / Hypotenuse. The adjacent side is the side of the trangle that makes up the angle and that is not the hypotenuse. In this case, the adjacent side is the base of the triangle. Follow the same equation rearranging method to find the length of the base and X. Cosine(a) / Hypotenuse = Length of Base = X coordinate.

Note however, that in order to do trigonometric functions in VB, you need to deal with Radians and Degrees. Radians = pi / 180. (if you don't know, pi = 3.14159265 (that is a short value, the length of the value of pi is infinite). In order to properly work with trig in VB, you need to calculate Sine and Cosine like this: Sine(angle * radians) and Cosine(angle * radians). If you don't multiply the angle by radians your answer will be incorrect.

That should be everything you need to know as I basically told you exactly how to do it. Remember to add the Origin Coordinates to the coordinates you get for Point B when the origin is not 0,0.

(woot me for knowing math)

Thanatos
09-05-2003, 11:09 AM
Try this:
Given the Origin(X1,Y1), the Angle, and the distance:
X2=X1+[cos(angle)*Distance]
Y2=Y1+[sin(angle)*Distance]

JuliaR
09-05-2003, 11:30 AM
Much appreciated guys. Very informative posts

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum