• Advertisement

Archived

This topic is now archived and is closed to further replies.

x,y coordinates to a direction

This topic is 5591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to find the direction (N, NE, E, SE, S, SW , W, NW) to a x, y destination coordinate from an x, y source coordinate. Im sure this is probably quite a simple formula unfortunately maths is not my strongest point. I need to create a function: int directionToCoord(POINT source, POINT destination) that will return an integer for the direction: 0 = North 1 = North East 2 = East 3 = South East etc. Does anybody have any ideas how I can achieve this, I was quessing that I probably have to firstly find the angle between the two points (from due North) and then split the 360 degrees up into the directions, but I have no idea how I can actually perform this. Any help would be greatly appreciated..

Share this post


Link to post
Share on other sites
Advertisement
no idea?

u know how to calculate sin right? (x^2+y^2)^0.5/y

angle=arcsin((x^2+y^2)^0.5/y)

if (x<0) angle=PI-angle;

I think that will work.

----------------------------
http://www.digitalsentience.pwp.blueyonder.co.uk

Share this post


Link to post
Share on other sites
dx = destx - startx
dy = desty - starty
angle = atan2(dy, dx);

Now that you have the angle, find the direction it is closest to (ie.: split the 360 degrees into 8 X 45 degrees), but beware that atan2 returns radians.

Cédric

Share this post


Link to post
Share on other sites
I followed Cedric''s example because I couldn''t see how fluffysteve''s would work because it didnt seem to take the source AND dest x,y points into account.

What I came up with was:


  
double angle = atan2(dest.y-source.y, dest.x-source.x) * 180 / PI;


And it works perfectly, it returns the correct angle!!

Thanks for your help.

Share this post


Link to post
Share on other sites

  • Advertisement