title: Fastest way to find the angle between two points
body: <p>To increase the speed at which I find the sine/cosine of an angle, I have built a reference table instead of calculating them on the fly. I have the same idea with finding the angle from one point to another.</p>
  
  <p>I have created a table of 3600 normalized vectors (3600 / 10 = accuracy of one tenth of a degree). Whenever I need to know the angle from one point to the next, I look through the table to find the best match. However, I am concerned that this might be slower than using math.atan2().</p>
  
  <p>Here is the code I am using:</p>
  
  <p>Create the vector table:</p>
  
  <pre><code>// vector to angle table
  var vectorToAngleTable = new Array();
  for (i = 0; i &lt; 3600; i += 1) {
      vectorToAngleTable[i] = new Vector2();
      vectorToAngleTable[i] = RotatePoint(forwardVector, i / 10);
  }
  </code></pre>
  
  <p>Find the angle from two points:</p>
  
  <pre><code>function NormalizeVector(vector) {
      var toReturn = vector;
      var dist = Math.sqrt(vector.x * vector.x + vector.y * vector.y);
      toReturn.x /= dist.x;
      toReturn.y /= dist.y;
      return toReturn;
  }
  
  function PointDirection(position, target) {
      var vector = target;
      var toReturn = 0;
      var smallest = 1.0;
      vector.x -= position.x;
      vector.y -= position.y;
      vector = NormalizeVector(vector);
      for (i = 0; i &lt; 3600; i += 1) {
          if (PointDistance(vectorToAngleTable[i], vector) &lt; smallest) {
              smalllest = PointDistance(vectorToAngleTable[i], vector);
              toReturn = i;
          }
      }
      return toReturn;
  }
  
  function PointDistance(point1, point2) {
      return Math.sqrt(((point2.x - point1.x) * (point2.x - point1.x)) + ((point2.y - point1.y) * (point2.y - point1.y)));
  }
  </code></pre>
  
  <p>As you can see, my concern is all the lines of code it is going through, and how many entries there are on the table that it looks through. I would love to know the fastest way to find the angle, no matter what the method is.</p>