Calculate Endpoint from Bearing

Does the following seem the right calculation for finding the endpoint given a start point and bearing? It isn’t yielding the expected results

Const PI=3.14159265358979323846264338327950
Distance = double.FromText(label2.text)
Dir = double.FromText(label1.text)*PI/180
ey = sy - cos(Dir) * Distance
ex = sx + sin(Dir) * Distance

Usually the angle is defined with respect to the horizontal direction and then you must use cosine for ex and sine for ey, but that depends on how you define the angle.

EDIT: and in general I would not expect to see any minus sign in those equations (the cosine/sine should take care of that), but again it depends on how your system is defined.

Julen

As @Julen_I mentioned, I would expect not to see the negative signs assuming a first quadrant coordinate system. Something like:

ex = sx + cos(dir) * dist
ey = sy + sin(dir) * dist

You already seemed to take care of the degrees-to-radians. Also use the debugger to make sure the interim conversions are all good.

What if the angle is 0-360 bearing style not just 0-90?

It will work as expected, the cosine of angles above 90 º and up to 270 º is negative, the sine is negative from 180 º to 360 º, so as I said before those functions will take care of the negative sign when neeeded. I am not sure I got your question, though.

I have seen you have another thread about creating a graph. Is this thread related? And, are you using your y values in the default direction (the values increase when moving downwards) or have you modified that axis?

Is your angle 0 º when pointing to the right and 90 º when pointing upwards?

Julen

When referred to as a “Bearing” the convention is to measure the angle clockwise from North.

1 Like

Thank you Robert, I didn’t know that.

In that case the use of sine and cosine in your first post was correct, Martin (assuming you are using the default x axis orientation). Sorry for the confusion.

What’s the problem with that code?

Julen

Assuming that y is in the north direction and x in the east direction, with the already suggested correction of changing the (-) to a (+), your equations should work just fine. The one time where you might want a (-) is if you are using screen coordinates where y is positive downwards (i.e. towards the south).

If you are doing this calculation a lot, you might want to define a constant for the PI/180 radians/degree. As you undoubtedly know, 90° is PI/2 radians, 180° = PI radians, 270 = 3*PI/2 radians, and 360 = 2*PI radians.

The problem is slightly trickier if you know the direction cosines and want to calculate the bearings because the trig values repeat between 0 and 360.

1 Like

I meant the default y orientation…

Julen

1 Like

Here is a little test app to make it easier to talk about.
Using @Jason_King 's pixel test method from 2014, I can

  • Drag the pointer around the screen and know what color it is on: Tick
  • Enter a bearing and distance and plot to the new destination and know if you are on the right color: tick (most of the time?)
  • I came up with a different way of plotting the bearing to initially as the first method didn’t work in all quadrants??
  • Crashes when I rotate to landscape so not sure why this isn’t working

Pixelcolor Test

The demo only seems to work on the initially setup iPad when you change to iPhone or landscape the scaling and pixel checking doesn’t work?

Is img.Pixel(sx, sy) the same as sx,sy on the canvas where the image has been scaled?

Hi @Jason_King should the pixel color work inside a canvas? I have tried for a couple of days to try different methods but using your initial example drawing the bitmap to the canvas and then clicking on the canvas it returns a color but not the right one see below. I assume if it was simple Xojo would have already added it so just need to know if I should pass on it as a solution.
Project

Back to the calculation, I can’t seem to get it
If I enter Bearing 180’ (south) and distance 30 I should end up at (50,70) for example
ey = sy - (cos(Dir) * Distance)
ex = sx + (sin(Dir) * Distance)
What should the formula be?

Try this:

Angle = (90-Bearing)*Pi/180
ex = sx + Distance * cos(Angle)
ey = sy + Distance * sin(Angle)
1 Like

Close, seems to be out by 90

Your formula is the correct one. What is the end point you get if you enter 180º and (50,40)?

Julen

This should be:
Angle = (Bearing-90)*Pi/180
ex = sx + Distance * cos(Angle)
ey = sy + Distance * sin(Angle)

1 Like

ey = 40 - 30cos(180º) = 40 - 30(-1) = 40 + 30 = 70
ex = 50 + 30* sin (180º) = 50 + 30*0 = 50
(50, 70)

Thanks @Julen_I that’s done it. Now just need a pixel color solution :slight_smile: