Guest User

Untitled

a guest
Jan 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. x:+1, y:+1 => NE
  2. x:0, y:+3 => N
  3. x:+10, y:-2 => E // closest compass direction
  4.  
  5. // enumerated counterclockwise, starting from east = 0:
  6. enum compassDir {
  7. E = 0, NE = 1,
  8. N = 2, NW = 3,
  9. W = 4, SW = 5,
  10. S = 6, SE = 7
  11. };
  12.  
  13. // for string conversion, if you can't just do e.g. dir.toString():
  14. const string[8] headings = { "E", "NE", "N", "NW", "W", "SW", "S", "SE" };
  15.  
  16. // actual conversion code:
  17. float angle = atan2( vector.y, vector.x );
  18. int octant = round( 8 * angle / (2*PI) + 8 ) % 8;
  19.  
  20. compassDir dir = (compassDir) octant; // typecast to enum: 0 -> E etc.
  21. string dirStr = headings[octant];
  22.  
  23. int octant = int( 8 * angle / (2*PI) + 8.5 ) % 8; // int() rounds down
  24.  
  25. //start direction from the lowest value, in this case it's west with -π
  26. enum direction {
  27. west,
  28. south,
  29. east,
  30. north
  31. }
  32.  
  33. increment = (2PI)/direction.count
  34. angle = atan2(y,x);
  35. testangle = -PI + increment/2
  36. index = 0
  37.  
  38. while angle > testangle
  39. index++
  40. if(index > direction.count - 1)
  41. return direction[0] //roll over
  42. testangle += increment
  43.  
  44.  
  45. return direction[index]
  46.  
  47. map<float2,Direction> candidates;
  48. candidates[float2(1,0)] = E; candidates[float2(0,1)] = N; // etc.
  49.  
  50. for each (float2 dir in candidates)
  51. {
  52. float goodness = dot(dir, v);
  53. if (goodness > bestResult)
  54. {
  55. bestResult = goodness;
  56. bestDir = candidates[dir];
  57. }
  58. }
  59.  
  60. NW (-1 + i) N (i) NE (1 + i)
  61. W (-1) Origin E (1)
  62. SW (-1 - i) S (-i) SE (1 - i)
  63.  
  64. // Some pseudocode
  65.  
  66. enum xDir { West = -1, Center = 0, East = 1 }
  67. enum yDir { South = -1, Center = 0, North = 1 }
  68.  
  69. xDir GetXdirection(Vector2 heading)
  70. {
  71. return round(heading.x / Max(heading.x, heading.y));
  72. }
  73.  
  74. yDir GetYdirection(Vector2 heading)
  75. {
  76. return round(heading.y / Max(heading.x, heading.y));
  77. }
  78.  
  79. -(1+sign(abs(sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y))))
  80.  
  81. -pi()/(8+10^-15)))/2*sign((x^2-y^2)*(x*y))),8)
  82.  
  83. public class So49290 {
  84. int piece(int x,int y) {
  85. double angle=Math.atan2(y,x);
  86. if(angle<0) angle+=2*Math.PI;
  87. int piece=(int)Math.round(n*angle/(2*Math.PI));
  88. if(piece==n)
  89. piece=0;
  90. return piece;
  91. }
  92. void run(int x,int y) {
  93. System.out.println("("+x+","+y+") is "+s[piece(x,y)]);
  94. }
  95. public static void main(String[] args) {
  96. So49290 so=new So49290();
  97. so.run(1,0);
  98. so.run(1,1);
  99. so.run(0,1);
  100. so.run(-1,1);
  101. so.run(-1,0);
  102. so.run(-1,-1);
  103. so.run(0,-1);
  104. so.run(1,-1);
  105. }
  106. int n=8;
  107. static final String[] s=new String[] {"e","ne","n","nw","w","sw","s","se"};
  108. }
  109.  
  110. h_axis = ""
  111. v_axis = ""
  112.  
  113. if (x > 0) h_axis = "E"
  114. if (x < 0) h_axis = "W"
  115. if (y > 0) v_axis = "S"
  116. if (y < 0) v_axis = "N"
  117.  
  118. return v_axis.append_string(h_axis)
  119.  
  120. // main direction constants
  121. DIR_E = 0x1
  122. DIR_W = 0x2
  123. DIR_S = 0x4
  124. DIR_N = 0x8
  125. // mixed direction constants
  126. DIR_NW = DIR_N | DIR_W
  127. DIR_SW = DIR_S | DIR_W
  128. DIR_NE = DIR_N | DIR_E
  129. DIR_SE = DIR_S | DIR_E
  130.  
  131. // calculating the direction
  132. dir = 0x0
  133.  
  134. if (x > 0) dir |= DIR_E
  135. if (x < 0) dir |= DIR_W
  136. if (y > 0) dir |= DIR_S
  137. if (y < 0) dir |= DIR_N
  138.  
  139. return dir
Add Comment
Please, Sign In to add comment