title: How would you change this function to check XY or YZ instead of just XZ
body: <p>I have this function that checks in a plane XZ if a point given is inside of the shape given.</p>
  <p>Edit: the shape is given by an array of 3D points, so essentially an array of arrays</p>
  <pre class="lang-js prettyprint-override"><code>function pointInShape( pos , shape )
    {
    // ray-casting algorithm based on
    // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
    var is_3d  = pos.length == 3;
    var x      = pos[0];
    var y      = pos[ is_3d ? 2 : 1];
    var inside = false;
    var l      = shape.length;
    var offset = is_3d ? 3 : 2;
  
    for (var i = 0, j = l - offset; i &lt; l; j = i, i += offset ) 
      {
      var xi = shape[i], yi = shape[i + (is_3d ? 2 : 1)];
      var xj = shape[j], yj = shape[j + (is_3d ? 2 : 1)];
      
      //works by testing an horizontal semi-infinite line starting at P and going to the right
      var intersect = ((yi &gt; y) != (yj &gt; y)) //first test if it lays in the y range of the segment
          &amp;&amp; (x &lt; (xj - xi) * (y - yi) / (yj - yi) + xi); //then test in which side of the plane it lies
      if (intersect)
          inside = !inside;
      }
    return inside;
    };
  </code></pre>
  <p>It doesn't take into account the Y, so for example:<br />
  Given a <strong>shape</strong> <code>s = [[0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0]]</code><br />
  Both <code>pointInShape([0.5, 0, 0.5], s.flat())</code><br />
  and <code>pointInShape([0.5, 1, 0.5], s.flat())</code>    returns <code>true</code>.
  (notice the flat)</p>
  <p>But with a shape s2 = [[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]], <code>pointInShape([0.5, 0.5, 0], s2.flat())</code> returns <code>false</code> even if the point is inside the area.</p>
  <p>How can I change the function (or create another one) so it also checks the other 2 planes?</p>