❔ How do I get the direction between two points?
Say that you are at position (10, 10) on the map, and your destination is (2, 2). I would like to calculate this and output this: "You need to go northwest." There will be all other directions, too. In my program, my Array2D class is being used to store the 2d array, and (0,0) is at the top-left corner.
My function is currently this, assuming I need vectors for it. What do I need to do to achieve this?
10 Replies
Atan2 and a switch statement
@5YJSA1DG9DFP14705 Since it's a 2d grid, someone recommended just getting the deltas and check for positive/negative. Is that also fine? It seems to be working.
JamesK.Polk#2544
REPL Result: Success
Console Output
Compile: 716.408ms | Execution: 50.415ms | React with ❌ to remove this embed.
Any advantage with that example over the less mathy one above?
sure.
JamesK.Polk#2544
REPL Result: Success
Console Output
Compile: 685.550ms | Execution: 102.033ms | React with ❌ to remove this embed.
the smallest deviation up doesn't apply
north
to it
everything is cordoned into 45 degree arcs.I'm confused how to apply the last code example. It only takes in x and y, but not both the position and destination. However, my less math one seems to be working fine.
If it works for what you need, that's all that matters!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.