Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d3-polygon.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // https://d3js.org/d3-polygon/ v3.0.1 Copyright 2010-2021 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, (function (exports) { 'use strict';
  7. function area(polygon) {
  8. var i = -1,
  9. n = polygon.length,
  10. a,
  11. b = polygon[n - 1],
  12. area = 0;
  13. while (++i < n) {
  14. a = b;
  15. b = polygon[i];
  16. area += a[1] * b[0] - a[0] * b[1];
  17. }
  18. return area / 2;
  19. }
  20. function centroid(polygon) {
  21. var i = -1,
  22. n = polygon.length,
  23. x = 0,
  24. y = 0,
  25. a,
  26. b = polygon[n - 1],
  27. c,
  28. k = 0;
  29. while (++i < n) {
  30. a = b;
  31. b = polygon[i];
  32. k += c = a[0] * b[1] - b[0] * a[1];
  33. x += (a[0] + b[0]) * c;
  34. y += (a[1] + b[1]) * c;
  35. }
  36. return k *= 3, [x / k, y / k];
  37. }
  38. // Returns the 2D cross product of AB and AC vectors, i.e., the z-component of
  39. // the 3D cross product in a quadrant I Cartesian coordinate system (+x is
  40. // right, +y is up). Returns a positive value if ABC is counter-clockwise,
  41. // negative if clockwise, and zero if the points are collinear.
  42. function cross(a, b, c) {
  43. return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
  44. }
  45. function lexicographicOrder(a, b) {
  46. return a[0] - b[0] || a[1] - b[1];
  47. }
  48. // Computes the upper convex hull per the monotone chain algorithm.
  49. // Assumes points.length >= 3, is sorted by x, unique in y.
  50. // Returns an array of indices into points in left-to-right order.
  51. function computeUpperHullIndexes(points) {
  52. const n = points.length,
  53. indexes = [0, 1];
  54. let size = 2, i;
  55. for (i = 2; i < n; ++i) {
  56. while (size > 1 && cross(points[indexes[size - 2]], points[indexes[size - 1]], points[i]) <= 0) --size;
  57. indexes[size++] = i;
  58. }
  59. return indexes.slice(0, size); // remove popped points
  60. }
  61. function hull(points) {
  62. if ((n = points.length) < 3) return null;
  63. var i,
  64. n,
  65. sortedPoints = new Array(n),
  66. flippedPoints = new Array(n);
  67. for (i = 0; i < n; ++i) sortedPoints[i] = [+points[i][0], +points[i][1], i];
  68. sortedPoints.sort(lexicographicOrder);
  69. for (i = 0; i < n; ++i) flippedPoints[i] = [sortedPoints[i][0], -sortedPoints[i][1]];
  70. var upperIndexes = computeUpperHullIndexes(sortedPoints),
  71. lowerIndexes = computeUpperHullIndexes(flippedPoints);
  72. // Construct the hull polygon, removing possible duplicate endpoints.
  73. var skipLeft = lowerIndexes[0] === upperIndexes[0],
  74. skipRight = lowerIndexes[lowerIndexes.length - 1] === upperIndexes[upperIndexes.length - 1],
  75. hull = [];
  76. // Add upper hull in right-to-l order.
  77. // Then add lower hull in left-to-right order.
  78. for (i = upperIndexes.length - 1; i >= 0; --i) hull.push(points[sortedPoints[upperIndexes[i]][2]]);
  79. for (i = +skipLeft; i < lowerIndexes.length - skipRight; ++i) hull.push(points[sortedPoints[lowerIndexes[i]][2]]);
  80. return hull;
  81. }
  82. function contains(polygon, point) {
  83. var n = polygon.length,
  84. p = polygon[n - 1],
  85. x = point[0], y = point[1],
  86. x0 = p[0], y0 = p[1],
  87. x1, y1,
  88. inside = false;
  89. for (var i = 0; i < n; ++i) {
  90. p = polygon[i], x1 = p[0], y1 = p[1];
  91. if (((y1 > y) !== (y0 > y)) && (x < (x0 - x1) * (y - y1) / (y0 - y1) + x1)) inside = !inside;
  92. x0 = x1, y0 = y1;
  93. }
  94. return inside;
  95. }
  96. function length(polygon) {
  97. var i = -1,
  98. n = polygon.length,
  99. b = polygon[n - 1],
  100. xa,
  101. ya,
  102. xb = b[0],
  103. yb = b[1],
  104. perimeter = 0;
  105. while (++i < n) {
  106. xa = xb;
  107. ya = yb;
  108. b = polygon[i];
  109. xb = b[0];
  110. yb = b[1];
  111. xa -= xb;
  112. ya -= yb;
  113. perimeter += Math.hypot(xa, ya);
  114. }
  115. return perimeter;
  116. }
  117. exports.polygonArea = area;
  118. exports.polygonCentroid = centroid;
  119. exports.polygonContains = contains;
  120. exports.polygonHull = hull;
  121. exports.polygonLength = length;
  122. Object.defineProperty(exports, '__esModule', { value: true });
  123. })));