views:

333

answers:

4

I have a bunch of vectors normal to window surfaces in a 3D modelling software. Projected to the XY-Plane, I would like to know in which direction they are facing, translated to the 8 compass coordinates (North, North-East, East, South-East, South, South-West, West and North-West).

The vectors work like this:

  • the X axis represents East-West (with East being positive)
  • the y axis represents North-South (with North being positive)
  • thus
    • (0, 1) == North
    • (1, 0) == East
    • (0,-1) == South
    • (-1,0) == West

Given a vector (x, y) I am looking for the closest of the 8 compass coordinates. Any ideas on how to do this elegantly?

+6  A: 

I would probably just do a call to atan2() to figure out the heading angle ("yaw"), and then use either a sequence of if:s or some maths to "snap" it to a multiple of 90 degrees.

unwind
thanks, looks promising, will check it out after lunch!
Daren Thomas
+4  A: 

This works in Java, computing a value 0...7 for the eight directions:

import static java.lang.Math.*;    

int compass = (((int) round(atan2(y, x) / (2 * PI / 8))) + 8) % 8;

The result maps to the compass as follows:

0 => E
1 => NE
2 => N
3 => NW
4 => W
5 => SW
6 => S
7 => SE
starblue
+4  A: 

Hi, no need to do an atan function.

if you do: y/x you'll get the slope of the line. Judging by the number you get you can determine the angle/quadrant.

for positive x's (x>0)

  • (y/x) > 2.4 -=> 90 degrees (north)
  • 2.4 > (y/x) > 0.4 -=> 45 degrees (northwest)
  • 0.4 > (y/x) > -0.4 -=> 0 degrees (west)
  • -0.4 > (y/x) > -2.4 -=> -45 degrees (southwest)
  • -2.4 > (y/x) -=> 90 degrees (south)

and a similar list for the negative x's

and finally the exception cases:

  • (x==0 && y>0) -=> -90 degrees (south)
  • (x==0 && y<0) -=> 90 degrees (south)

addendum: I only report this method for when calculating an atan is a no go (for instance on an embedded system))

I had to dig a bit. Here's a highly optimized routine I use (used in mobile games).

input: x1,y1 = startpoint of vector x2,y2 = endpoint of vector output (0-7) = 0=north, 1=northwest, 2=west,...etc

 int CalcDir( int x1, int y1, int x2, int y2 )
 {
      int dx = x2 - x1, dy = y2 - y1;
      int adx = (dx<0)?-dx:dx, ady = (dy<0)?-dy:dy, r;
      r=(dy>0?8:0)+(dx>0?4:0)+(adx>ady?2:0);
      r=(int []){2,3,1,0,5,4,6,7}[r];
 }
Toad
+3  A: 

This one does not use atan2, and does at worst 4 comparisons and 2 products per call. Comparing x to y in the 4 inner blocks (I edited it in the first block only), it can be reduced to exactly 4 comparisons and 1 product per call.

int compass(double x,double y)
{
  double t = 0.392699082; // tan(M_PI/8.0);

  if (x>=0)
  {
    if (y>=0)
    {
      if (x>y) { if (y<t*x) return E_COMPASS; }
      else { if (x<t*y) return N_COMPASS; }
      return NE_COMPASS;
    }
    else
    {
      if (-y<t*x) return E_COMPASS;
      if (x<-t*y) return S_COMPASS;
      return SE_COMPASS;
    }
  }
  else
  {
    if (y>=0)
    {
      if (y<-t*x) return W_COMPASS;
      if (-x<t*y) return N_COMPASS;
      return NW_COMPASS;
    }
    else
    {
      if (-y<-t*x) return W_COMPASS;
      if (-x<-t*y) return S_COMPASS;
      return SW_COMPASS;
    }
  }
  return E_COMPASS;
}
Eric Bainville