javascript - Calculate normal vector of a polygon - Newells Method -


i'm trying calculate surface normal of 2d polygon. using newell's method opengl wiki calculate surface normal. https://www.opengl.org/wiki/calculating_a_surface_normal understanding normal should in y direction returns [0, 0, 0]. y value gets changed -1 on second iteration , 0 on fourth iteration.

p = [[0, 0, 0]     [1, 0, 0]     [0, 0, 1]     [1, 0, 1]]  function calcnormal(p) {     var normal = [0, 0, 0];     for(var = 0; < 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; } 

you're using degenerate polygon testing. if draw in xz-plane, vertices numbered 0 3, looks this:

2 ---- 3  \    /   \  /    \/    /\   /  \  /    \ 0 ---- 1 

this polygon not have defined normal, since changes orientation in middle, , folds on itself.

if swap last 2 vertices:

p = [[0, 0, 0]      [1, 0, 0]      [1, 0, 1]      [0, 0, 1]] 

it this, , should more meaningful results:

3 ---- 2 |      | |      | |      | 0 ---- 1 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -