I have a set of key codes with values ββ(of course, 4), from 0 to 3, corresponding to the keys down, left, up, right, in that order. I need to convert these key codes in x and y directions, with a positive x indicating the location to the left of the origin, and a positive y indicating the location below the origin. As I see it, I have two ways to do this:
using arrays:
int [] dx = {0, -1, 0, 1};
int [] dy = {1, 0, -1, 0};
int x = dx[kc];
int y = dy[kc];
or using arithmetic:
int x = (kc%2)*(((kc/2)%2)*2 - 1);
int y = ((kc+1)%2)*(((kc/2)%2)*-2 + 1);
which would be more efficient?
source
share