Question

I know top left corner and also I know the width and height of the square. Note (calculation) can be rounded because the distance just 0.25 mile. Also I know the point which is inside that square. How to calculate for which square the given point is belong? I've attached the picture which will show what I mean. enter image description here

Était-ce utile?

La solution

If you know the point p = (x,y), then just use some nested if statements...

if(x >= 0 ) {
  if( y >= 0 ) 
    return QuadrantB;
  else
    return QuadrantD;
}
else {
  if( y >= 0 )
    return QuadrantA;
  else
    return QuadrantC;
}

You might want to change whether or not the conditionals inside the if statement are inclusive or exclusive.

Note: This is assuming the center of all four quadrants is defined as (0,0). If the top left is defined as (0,0), then just subtract 0.25/2 = 0.125 from both x and y to get the point in the coordinate frame defined by (0,0) being the center of all four quadrants.

This sort of analysis is used to compute the quadrant in the commonly used atan2 function, which returns the angle of the vector that starts at the origin and ends at point (x,y): http://en.wikipedia.org/wiki/Atan2.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top