Get quadrant value after reflecting points across x axis
I have a number of points on a 2D graph, and I am concerned with they quadrant they lie in than the actual coordinates itself. I have a function ReflectX() which performs "Reflecting along X-axis" on a subset and returns the updated quadrant in which the points now lie. So, points originally in quadrant 1 will now lie in 4 and vice-versa; similarly for quadrant 2 and 3. Instead of using 4 if-else statements, I want to use just 2, for optimising. Can that be done? Any suggestions for other kind of optimisation is also welcome. What I have done so far (which isnt much):
int ReflectX(int q[], int x, int y){//x & y provide the range boundaries
int i;
for(i=x-1; i<y; i++){
if(q[i]==1 || q[i]==4) q[i]=(q[i]+3)%6;
else //change 2 into 3 and 3 into 2
}
}
EDIT: I had not posted about ReflectY() because I could do it with one if-else condition. However, based on Adam Rosenfield 's answer I decided to optimise this function with a one-liner formula and got:
q[i] = (q[i] - pow(-1, q[i开发者_如何学Go]%2));
You can get a much simpler answer by just looking at the table of all possible inputs and outputs:
Initial quadrant Reflected quadrant 1 4 2 3 3 2 4 1
So, just do this:
q[i] = 5 - q[i];
精彩评论