开发者

I have a brain freeze, help me make this method efficient :>

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.

int eastWest = Double.compare(currentPoint.getX(), nextPoint.getX()) 
if (eastWest == 0) {
    int northSouth = Double.compare(currentPoint.getX() , nextPoint.getX()) 
    /* DRIVE NORTH (-1), SOUTH (+1) */ 
    if (northSouth == 0)
       rotation = -1;
    else
       rotation = (5 + northSouth - heading) % 4;
} else {
    /* DRIVE WEST (-1), EAST (+1) */  
    rotation = heading ^ (2 + eastWest);
}

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.


The best way to do it is using mathematical Vectors to indicate your current direction.


It's hardly readable, but if you want to write less code, you can get the result with simple mathematics:

public static int getRotation(Point currentPoint, Point nextPoint, int heading) {
    int dx = (int) Math.signum(nextPoint.getX() - currentPoint.getX());
    int dy = (int) Math.signum(nextPoint.getY() - currentPoint.getY());
    return dx != 0 ? (4-heading+dx)%4 : dy != 0 ? (7-heading+dy)%4 : -1;
}


All rotation values are a rotated version of "0321" (north: 0321, east: 3210, south: 2103, west: 1032). A simple solution would be to create an array containing "03210321" and access it with a different index for each case. Pseudocode:

initialise array arr with 0,3,2,1,0,3,2,1
index = 0 for north, 1 for east, 2 for south, 3 for west
rotation = arr[index+heading];

EDIT: I think we can use this mathematical expression instead of the array:

index = 0 for north, 1 for east, 2 for south, 3 for west
rotation = (8 - (index+heading)) % 4;


Of course you can optimize above method but in the future, in maintenance this peace of code will be very hard to understand.

If you haven't any better idea try create 4 Maps which will mapping heading to rotation value. Think about create some enum to show what is mystery 1 2 3 4.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜