polygon.js 589 B

12345678910111213141516171819202122
  1. import windingLine from './windingLine.js';
  2. var EPSILON = 1e-8;
  3. function isAroundEqual(a, b) {
  4. return Math.abs(a - b) < EPSILON;
  5. }
  6. export 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. }
  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. }