I have the following code, and I can see that there is a connection between the values. I can however not get my head together and figure out how to remove the switch statements with some add subtract modelo haxx. Would really appriciate some help to get this method smoother.
The heading variable is either 0, 1, 2 or 3.
public static int getRotation(Point currentPoint, Point nextPoint, int heading) {
int rotation = -1;
if(currentPoint.getX() < nextPoint.getX()) /* DRIVE WEST */ {
switch(heading) {
case 0: rotation = 1; break;
case 1: rotation = 0; break;
case 2: rotation = 3; break;
case 3: rotation = 2; break;
}
} else if(currentPoint.getX() > nextPoint.getX()) /* DRIVE EAST */ {
switch(heading) {
case 0: rotation = 3; break;
case 1: rotation = 2; break;
case 2: rotation = 1; break;
case 3: rotation = 0; break;
}
} else if(currentPoint.getY() < nextPoint.getY()) /* DRIVE NORTH */ {
switch(heading) {
case 0: rotation = 0; break;
case 1: rotation = 3; break;
case 2: rotation = 2; break;
case 3: rotation = 1; break;
}
} else if(currentPoint.getY() > nextPoint.getY()) /* DRIVE SOUTH */ {
switch(heading) {
case 0: rotation = 2; break;
case 1: rotation = 1; break;
case 2: rotation = 0; break;
case 3: rotation = 3; break;
}
}
return rotation;
}
EDIT: I did forget to mention that nextPoint can only be +-x or y compared to currentPoint. if currentPoint is (0,0) newPoint must be either (-1,0), (1,0), (0,-1) or (0,1).
You could try.
Note: I would expect to see some symetry to these functions are each direction appears to have a different combination and doesn’t sound right.