javascript - Calculate vector sign(positive/negative) -
i'm trying calculate normal vector 3 points, getting 2 vectors them , calculate normal. need know if normal positive or negative. pls :)
the normal vector, can has direction, , value (or magnitude or length), no sign.
the direction of normal depends on order, pass values.
i assume asking culling, wether vector front or @ face? can calculate dot-product of vector , normal.
result > 0 point in same direction,
result < 0 point in opposite directions
function pt(x,y,z){ return { x: +x||0, y: +y||0, z: +z||0 } } function dot(a,b){ return b.x*a.x + b.y*a.y + b.z-a.z } function delta(a,b){ return pt(b.x-a.x, b.y-a.y, b.z-a.z) } function cross(a,b){ return pt( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x ) } var face = [ pt(0,0,0), pt(1,0,0), pt(0,1,0) ]; var vector; var normal = cross( delta(face[0], face[1]), delta(face[1], face[2]) ); console.log('normal', normal); console.log(vector = pt(0, 0, 10), dot(normal,vector)); console.log(vector = pt(0, 0, -1), dot(normal,vector)); //pretty flat angle still enough determine direction console.log(vector = pt(1, 1, .00005), dot(normal,vector));
Comments
Post a Comment