windingLine.js 558 B

1234567891011121314151617181920212223
  1. function windingLine(x0, y0, x1, y1, x, y) {
  2. if (y > y0 && y > y1 || y < y0 && y < y1) {
  3. return 0;
  4. } // Ignore horizontal line
  5. if (y1 === y0) {
  6. return 0;
  7. }
  8. var dir = y1 < y0 ? 1 : -1;
  9. var t = (y - y0) / (y1 - y0); // Avoid winding error when intersection point is the connect point of two line of polygon
  10. if (t === 1 || t === 0) {
  11. dir = y1 < y0 ? 0.5 : -0.5;
  12. }
  13. var x_ = t * (x1 - x0) + x0; // If (x, y) on the line, considered as "contain".
  14. return x_ === x ? Infinity : x_ > x ? dir : 0;
  15. }
  16. module.exports = windingLine;