# Calculate direction of coordinates

I wanted conditions that could determine which direction (north, south, east, west, northwest, northeast, southeast, southwest) the player is following, what I got was just north, south, east and west, as shown in the code below:

var playerX = player.x, playerY = player.y;

var destX = 8, destY = -15;

if (playerX < destX) {
playerX += 0.5; // east
} else if (playerX > destX) {
playerX -= 0.5; // west
} else {
playerX = destX; // maintains direction
}
if (playerY < destY) {
playerY += 0.5; // north
} else if (playerY > destY) {
playerY -= 0.5; // south
} else {
playerY = destY; //maintains direction
}


image example:

• What are destX and destY, playerX and player, and player.x and player.y? What do they represent? Why do you hardcode 0.5, what is this value representing? – Vaillancourt Mar 23 '17 at 2:11
• what do you need exactly ! what is your game engine? can you share picture that show your problem? – Seyed Morteza Kamali Mar 23 '17 at 2:53
• I'm a bit confused. It looks like this code already computes movement correctly for northwest, northeast, etc. like you say you want. To take an example, if playerX < destX and playerY < destY then the player will move positively in both the x and y axes (ie. northeast). Have I misunderstood your code or the question you're asking? – DMGregory Mar 23 '17 at 2:56
• Currently I have 4 directions (north, south, east and west), and I need to work like 4 others (northwest, northeast, southeast, southwest). TOTALLY 8 DIRECTIONS, BUT I JUST HAVE 4! I updated the post with the improved code, and also a representative image. – Lucas Mar 23 '17 at 3:03
• I'm using three.js, but I do not think it matters much. Since this is a basic Cartesian problem, but I'm not in the mood. I'M SORRY, I'M USING A TRANSLATOR, I'M A BRAZILIAN. – Lucas Mar 23 '17 at 3:05

I solved this code:

var lastX, lastY;
var playerX = player.position.x,
playerY = player.position.y;

if(playerX < lastX) {
if (playerY < lastY) {
// top left
} else if (playerY == lastY) {
// left
} else { // playerY > lastY
// bottom left
}
} else
if (playerX == lastX) {
if (playerY < lastY) {
// top
} else if (playerY == lastY) {
// Not allowed
} else { // playerY > lastY
// bottom
}
} else { // playerX > lastX
if (playerY < lastY) {
// top right
} else if (playerY == lastY) {
// right
} else {// playerY > lastY
// bottom right
}
}

lastX = playerX;
lastY = playerY;
}


Imagine your player is a chess piece on a chessboard then look at the picture how every chess piece are moving and which field are valid fields for each piece, How ever your problem is more look like a Queen on a chess board because can move on every direction and you can adapt this algorithm to your player. you need array to loop through every row and column.

Here is the code for how a Queen move on a chessboard.

 public class Queen extends Chesspiece {

protected Queen(char color, char name) {
super(color, name);
}

@Override
public void markReachableFields() {
char rw = row;
byte col = column;

for (char i = 'a'; i <='h'; i++) {
rw = (char)i;
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
col = column;
rw = row;
for (byte j = 0; j < 8; j++) {
col = (byte)j;
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
rw = row;
col = column;
for (int i = 0; i < 8; i++) {
rw = (char)(rw - 1);
col = (byte)(col + 1);

if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
rw = row;
col = column;

for (int i = 1; i <= 8; i++) {
rw = (char)(rw + 1);
col = (byte)(col + 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
rw = row;
col = column;

for (int i = 1; i <= 8; i++) {
rw = (char)(rw + 1);
col = (byte)(col - 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}

rw = row;
col = column;
for (int i = 1; i <= 8; i++) {
rw = (char)(rw - 1);
col = (byte)(col - 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();

}
}
}
@Override
public void unmarkReachableFields() {
char rw = row;
byte col = column;

for (char i = 'a'; i <='h'; i++) {
rw = (char)i;
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
col = column;
rw = row;
for (byte j = 0; j < 8; j++) {
col = (byte)j;
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
rw = row;
col = column;
for (int i = 0; i < 8; i++) {
rw = (char)(rw - 1);
col = (byte)(col + 1);

if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
rw = row;
col = column;

for (int i = 1; i <= 8; i++) {
rw = (char)(rw + 1);
col = (byte)(col + 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
rw = row;
col = column;

for (int i = 1; i <= 8; i++) {
rw = (char)(rw + 1);
col = (byte)(col - 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}

rw = row;
col = column;
for (int i = 1; i <= 8; i++) {
rw = (char)(rw - 1);
col = (byte)(col - 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();

}
}
}
}