title: Calculate normal vector of a polygon - Newells Method
body: <p>I'm trying to calculate the surface normal of a 2D polygon. I am using Newell's method from the OpenGL wiki to calculate the surface normal. <a href="https://www.opengl.org/wiki/Calculating_a_Surface_Normal" rel="nofollow">https://www.opengl.org/wiki/Calculating_a_Surface_Normal</a> From my understanding the normal should be in the y direction but it always returns [0, 0, 0]. The y value gets changed to -1 on the second iteration and back to zero on the fourth iteration.</p>
  
  <pre><code>p = [[0, 0, 0]
      [1, 0, 0]
      [0, 0, 1]
      [1, 0, 1]]
  
  function calcNormal(p) {
      var normal = [0, 0, 0];
      for(var i = 0; i &lt; p.length; i++) {
          var j = (i + 1) % (p.length);       
          normal[0] += (p[i][1] - p[j][1]) * (p[i][2] + p[j][2]);
          normal[1] += (p[i][2] - p[j][2]) * (p[i][0] + p[j][0]);
          normal[2] += (p[i][0] - p[j][0]) * (p[i][1] + p[j][1]);
      }
      return normal;
  }
  </code></pre>