polygon.js 593 B

123456789101112131415161718192021222324252627282930313233
  1. var windingLine = require("./windingLine");
  2. var EPSILON = 1e-8;
  3. function isAroundEqual(a, b) {
  4. return Math.abs(a - b) < EPSILON;
  5. }
  6. function contain(points, x, y) {
  7. var w = 0;
  8. var p = points[0];
  9. if (!p) {
  10. return false;
  11. }
  12. for (var i = 1; i < points.length; i++) {
  13. var p2 = points[i];
  14. w += windingLine(p[0], p[1], p2[0], p2[1], x, y);
  15. p = p2;
  16. } // Close polygon
  17. var p0 = points[0];
  18. if (!isAroundEqual(p[0], p0[0]) || !isAroundEqual(p[1], p0[1])) {
  19. w += windingLine(p[0], p[1], p0[0], p0[1], x, y);
  20. }
  21. return w !== 0;
  22. }
  23. exports.contain = contain;