Link to home
Start Free TrialLog in
Avatar of Saqib Husain
Saqib HusainFlag for Pakistan

asked on

Trigonometry – Finding next point in a path based on distance and angle

I have two coordinates p1(x1,y1) and p2(x2,y2). I need a third point p3(x3,y3) for wich the following are given
   -   distance from p2 to p3
   -   included angle p1-p2-p3, negative for counterclockwise

I need this to insert into a VBA code. I have tried but am confusing the directions (negatives and positives). VBA code would be super but only an algorithm which can directly be converted to code would also suffice.

This is not homework.
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

this should get you started
a = height
b = distance
c = length of hypotenuse

c = sqrt( (a^2) + (b^2))
height = abs((b1 - a1))
distance = abs((b2-a2))

a2 = hypotenuse * sin(angle)
distance = sqrt(c^2 - a^2)
if angle is greater than 180
total a = a + a2
b = b - b2
else
total a = a1+a2
totel b = b1 + b2
I oversimplified in my initial comment, and so deleted it.

I tested the following code with a small number of test cases, but definitely not with all the quadrants.

Const Pi As Double = 3.14159265358979

Function ArcTan(x As Double, y As Double) As Variant
'Returns the angle of a line segment starting at the origin in radians over the range 0 to 2 pi given the x and y co-ordinates of a second point
'Note that VBA Atn function works between -pi/2 and +pi/2 radians
Dim theta As Variant
If x = 0 Then
    If y = 0 Then
        theta = "DIV/0!"
    Else
        theta = IIf(y > 0, Pi / 2, 1.5 * Pi)
    End If
ElseIf x < 0 Then
    theta = -Atn(y / x) + Pi
Else
    theta = Atn(y / x) + IIf(y > 0, 0, 2 * Pi)
End If
ArcTan = theta
End Function

Function NewCoordinates(x1 As Double, y1 As Double, x2 As Double, y2 As Double, Distance As Double, thetaIncremental As Double) As Variant
'Returns new x and y co-ordinates a Distance away from initial x, y co-ordinates at a bearing thetaIncremental
'thetaIncremental is positive for clockwise angles and negative for counterclockwise
Dim thetaNet As Double, thetaBase As Double, x As Double, xNet As Double, y As Double, yNet As Double
Dim v As Variant
x = x1 - x2
y = y1 - y2
v = ArcTan(x, y)
If Distance = 0 Then
    NewCoordinates = Array(x2, y2)
Else
    If IsNumeric(v) Or Distance < 0 Then
        thetaBase = v
        thetaNet = thetaBase - thetaIncremental
        If thetaNet < 0 Then thetaNet = thetaNet + 2 * Pi
        If thetaNet > 2 * Pi Then thetaNet = thetaNet - 2 * Pi
        xNet = x2 + Distance * Cos(thetaNet)
        yNet = y2 + Distance * Sin(thetaNet)
        NewCoordinates = Array(xNet, yNet)
    Else
        NewCoordinates = Array("N/A", "N/A")
    End If
End If
End Function

Open in new window

TrigQ29201649.xlsm
Avatar of Saqib Husain

ASKER

Brad,
The UDF is not working as I expected.
See NewCoordinates.xlsm

The first two sets of coordinates are concurrent on the chart. The third set is not in agreement with the first two.
ASKER CERTIFIED SOLUTION
Avatar of byundt
byundt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Spot on! Thanks