Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x:+1, y:+1 => NE
- x:0, y:+3 => N
- x:+10, y:-2 => E // closest compass direction
- // enumerated counterclockwise, starting from east = 0:
- enum compassDir {
- E = 0, NE = 1,
- N = 2, NW = 3,
- W = 4, SW = 5,
- S = 6, SE = 7
- };
- // for string conversion, if you can't just do e.g. dir.toString():
- const string[8] headings = { "E", "NE", "N", "NW", "W", "SW", "S", "SE" };
- // actual conversion code:
- float angle = atan2( vector.y, vector.x );
- int octant = round( 8 * angle / (2*PI) + 8 ) % 8;
- compassDir dir = (compassDir) octant; // typecast to enum: 0 -> E etc.
- string dirStr = headings[octant];
- int octant = int( 8 * angle / (2*PI) + 8.5 ) % 8; // int() rounds down
- //start direction from the lowest value, in this case it's west with -π
- enum direction {
- west,
- south,
- east,
- north
- }
- increment = (2PI)/direction.count
- angle = atan2(y,x);
- testangle = -PI + increment/2
- index = 0
- while angle > testangle
- index++
- if(index > direction.count - 1)
- return direction[0] //roll over
- testangle += increment
- return direction[index]
- map<float2,Direction> candidates;
- candidates[float2(1,0)] = E; candidates[float2(0,1)] = N; // etc.
- for each (float2 dir in candidates)
- {
- float goodness = dot(dir, v);
- if (goodness > bestResult)
- {
- bestResult = goodness;
- bestDir = candidates[dir];
- }
- }
- NW (-1 + i) N (i) NE (1 + i)
- W (-1) Origin E (1)
- SW (-1 - i) S (-i) SE (1 - i)
- // Some pseudocode
- enum xDir { West = -1, Center = 0, East = 1 }
- enum yDir { South = -1, Center = 0, North = 1 }
- xDir GetXdirection(Vector2 heading)
- {
- return round(heading.x / Max(heading.x, heading.y));
- }
- yDir GetYdirection(Vector2 heading)
- {
- return round(heading.y / Max(heading.x, heading.y));
- }
- -(1+sign(abs(sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y))))
- -pi()/(8+10^-15)))/2*sign((x^2-y^2)*(x*y))),8)
- public class So49290 {
- int piece(int x,int y) {
- double angle=Math.atan2(y,x);
- if(angle<0) angle+=2*Math.PI;
- int piece=(int)Math.round(n*angle/(2*Math.PI));
- if(piece==n)
- piece=0;
- return piece;
- }
- void run(int x,int y) {
- System.out.println("("+x+","+y+") is "+s[piece(x,y)]);
- }
- public static void main(String[] args) {
- So49290 so=new So49290();
- so.run(1,0);
- so.run(1,1);
- so.run(0,1);
- so.run(-1,1);
- so.run(-1,0);
- so.run(-1,-1);
- so.run(0,-1);
- so.run(1,-1);
- }
- int n=8;
- static final String[] s=new String[] {"e","ne","n","nw","w","sw","s","se"};
- }
- h_axis = ""
- v_axis = ""
- if (x > 0) h_axis = "E"
- if (x < 0) h_axis = "W"
- if (y > 0) v_axis = "S"
- if (y < 0) v_axis = "N"
- return v_axis.append_string(h_axis)
- // main direction constants
- DIR_E = 0x1
- DIR_W = 0x2
- DIR_S = 0x4
- DIR_N = 0x8
- // mixed direction constants
- DIR_NW = DIR_N | DIR_W
- DIR_SW = DIR_S | DIR_W
- DIR_NE = DIR_N | DIR_E
- DIR_SE = DIR_S | DIR_E
- // calculating the direction
- dir = 0x0
- if (x > 0) dir |= DIR_E
- if (x < 0) dir |= DIR_W
- if (y > 0) dir |= DIR_S
- if (y < 0) dir |= DIR_N
- return dir
Add Comment
Please, Sign In to add comment