❔ 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?
public EDirection GetDirection(int xPos, int yPos, int xDest, int yDest)
{
Vector2D pos = new Vector2D(xPos, yPos);
Vector2D dest = new Vector2D(xDest, yDest);


}
public EDirection GetDirection(int xPos, int yPos, int xDest, int yDest)
{
Vector2D pos = new Vector2D(xPos, yPos);
Vector2D dest = new Vector2D(xDest, yDest);


}
10 Replies
Google
Google2y ago
Atan2 and a switch statement
triplemocha
triplemochaOP2y ago
@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.
public EDirection GetDirection(int xPos, int yPos, int xDest, int yDest)
{
int x = xDest - xPos;
int y = yDest - yPos;

if ((x == 0) && (y < 0))
return EDirection.N;
else if ((x > 0) && (y < 0))
return EDirection.NE;
else if ((x > 0) && (y == 0))
return EDirection.E;
else if ((x > 0) && (y > 0))
return EDirection.SE;
else if ((x == 0) && (y > 0))
return EDirection.S;
else if ((x < 0) && (y > 0))
return EDirection.SW;
else if ((x < 0) && (y == 0))
return EDirection.W;
else if ((x < 0) && (y < 0))
return EDirection.NW;
else if ((x == 0) && (y == 0))
return EDirection.Center;
else
return EDirection.Unknown;
}
public EDirection GetDirection(int xPos, int yPos, int xDest, int yDest)
{
int x = xDest - xPos;
int y = yDest - yPos;

if ((x == 0) && (y < 0))
return EDirection.N;
else if ((x > 0) && (y < 0))
return EDirection.NE;
else if ((x > 0) && (y == 0))
return EDirection.E;
else if ((x > 0) && (y > 0))
return EDirection.SE;
else if ((x == 0) && (y > 0))
return EDirection.S;
else if ((x < 0) && (y > 0))
return EDirection.SW;
else if ((x < 0) && (y == 0))
return EDirection.W;
else if ((x < 0) && (y < 0))
return EDirection.NW;
else if ((x == 0) && (y == 0))
return EDirection.Center;
else
return EDirection.Unknown;
}
MODiX
MODiX2y ago
JamesK.Polk#2544
REPL Result: Success
var result = GetCardinalDirection(2, 4);
Console.WriteLine(result);
string GetCardinalDirection(int x, int y)
{
if (x == 0 && y == 0) { return "Origin"; }
var angle = Math.Atan2(y, x);
var angleInDegrees = angle * (180 / Math.PI);
if (angleInDegrees < 0) { angleInDegrees += 360; }

var directions = new string[] { "North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest" };
var index = (int)Math.Round((angleInDegrees % 360) / 45) % 8;
return directions[index];
}
var result = GetCardinalDirection(2, 4);
Console.WriteLine(result);
string GetCardinalDirection(int x, int y)
{
if (x == 0 && y == 0) { return "Origin"; }
var angle = Math.Atan2(y, x);
var angleInDegrees = angle * (180 / Math.PI);
if (angleInDegrees < 0) { angleInDegrees += 360; }

var directions = new string[] { "North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest" };
var index = (int)Math.Round((angleInDegrees % 360) / 45) % 8;
return directions[index];
}
Console Output
Northeast
Northeast
Compile: 716.408ms | Execution: 50.415ms | React with ❌ to remove this embed.
triplemocha
triplemochaOP2y ago
Any advantage with that example over the less mathy one above?
Google
Google2y ago
sure.
MODiX
MODiX2y ago
JamesK.Polk#2544
REPL Result: Success
var result = GetCardinalDirection(1, 4);
Console.WriteLine(result);
string GetCardinalDirection(int x, int y)
{
if (x == 0 && y == 0) { return "Origin"; }
var angle = Math.Atan2(y, x);
var angleInDegrees = angle * (180 / Math.PI);
if (angleInDegrees < 0) { angleInDegrees += 360; }

var directions = new string[] { "North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest" };
var index = (int)Math.Round((angleInDegrees % 360) / 45) % 8;
return directions[index];
}
var result = GetCardinalDirection(1, 4);
Console.WriteLine(result);
string GetCardinalDirection(int x, int y)
{
if (x == 0 && y == 0) { return "Origin"; }
var angle = Math.Atan2(y, x);
var angleInDegrees = angle * (180 / Math.PI);
if (angleInDegrees < 0) { angleInDegrees += 360; }

var directions = new string[] { "North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest" };
var index = (int)Math.Round((angleInDegrees % 360) / 45) % 8;
return directions[index];
}
Console Output
East
East
Compile: 685.550ms | Execution: 102.033ms | React with ❌ to remove this embed.
Google
Google2y ago
the smallest deviation up doesn't apply north to it everything is cordoned into 45 degree arcs.
triplemocha
triplemochaOP2y ago
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.
Google
Google2y ago
If it works for what you need, that's all that matters!
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server