Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can i find the position (quadrant or axis) of a point on a graph without using any conditon (if, while, for...etc). The user will provide coordinates(x,y) of the point.
Language=C or CPP
Posted

1 solution

// requires <math.h>

int quadrant(double x, double y)
{
  static int q[]={3, 4, 2, 1};
  int sx = ((int)(x/fabs(x)) + 1)/2;
  int sy = ((int)(y/fabs(y)) + 1); 
  return q[sx+sy];
}


you may use, as well, atan2 instead of fabs:
int quadrant(double x, double y)
{
  static int q[]={3, 4, 2, 1};
  int i = (int)((atan2(y,x)/atan2(1.,0.))+2.0); 
  return q[i];
}




Please note, the function gives wrong result for x=0.0 or/and y=0.0 input values (such points are on the boundary of the quadrants).
:)
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900