poly2tri
Version:
A 2D constrained Delaunay triangulation library
110 lines (98 loc) • 2.56 kB
JavaScript
/*
* Poly2Tri Copyright (c) 2009-2014, Poly2Tri Contributors
* http://code.google.com/p/poly2tri/
*
* poly2tri.js (JavaScript port) (c) 2009-2014, Poly2Tri Contributors
* https://github.com/r3mi/poly2tri.js
*
* All rights reserved.
*
* Distributed under the 3-clause BSD License, see LICENSE.txt
*/
;
/**
* Precision to detect repeated or collinear points
* @private
* @const {number}
* @default
*/
var EPSILON = 1e-12;
exports.EPSILON = EPSILON;
/**
* @private
* @enum {number}
* @readonly
*/
var Orientation = {
"CW": 1,
"CCW": -1,
"COLLINEAR": 0
};
exports.Orientation = Orientation;
/**
* Formula to calculate signed area<br>
* Positive if CCW<br>
* Negative if CW<br>
* 0 if collinear<br>
* <pre>
* A[P1,P2,P3] = (x1*y2 - y1*x2) + (x2*y3 - y2*x3) + (x3*y1 - y3*x1)
* = (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)
* </pre>
*
* @private
* @param {!XY} pa point object with {x,y}
* @param {!XY} pb point object with {x,y}
* @param {!XY} pc point object with {x,y}
* @return {Orientation}
*/
function orient2d(pa, pb, pc) {
var detleft = (pa.x - pc.x) * (pb.y - pc.y);
var detright = (pa.y - pc.y) * (pb.x - pc.x);
var val = detleft - detright;
if (val > -(EPSILON) && val < (EPSILON)) {
return Orientation.COLLINEAR;
} else if (val > 0) {
return Orientation.CCW;
} else {
return Orientation.CW;
}
}
exports.orient2d = orient2d;
/**
*
* @private
* @param {!XY} pa point object with {x,y}
* @param {!XY} pb point object with {x,y}
* @param {!XY} pc point object with {x,y}
* @param {!XY} pd point object with {x,y}
* @return {boolean}
*/
function inScanArea(pa, pb, pc, pd) {
var oadb = (pa.x - pb.x) * (pd.y - pb.y) - (pd.x - pb.x) * (pa.y - pb.y);
if (oadb >= -EPSILON) {
return false;
}
var oadc = (pa.x - pc.x) * (pd.y - pc.y) - (pd.x - pc.x) * (pa.y - pc.y);
if (oadc <= EPSILON) {
return false;
}
return true;
}
exports.inScanArea = inScanArea;
/**
* Check if the angle between (pa,pb) and (pa,pc) is obtuse i.e. (angle > π/2 || angle < -π/2)
*
* @private
* @param {!XY} pa point object with {x,y}
* @param {!XY} pb point object with {x,y}
* @param {!XY} pc point object with {x,y}
* @return {boolean} true if angle is obtuse
*/
function isAngleObtuse(pa, pb, pc) {
var ax = pb.x - pa.x;
var ay = pb.y - pa.y;
var bx = pc.x - pa.x;
var by = pc.y - pa.y;
return (ax * bx + ay * by) < 0;
}
exports.isAngleObtuse = isAngleObtuse;