智慧水务管理系统 - 精河县供水工程综合管理平台

earcut.dev.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.earcut = {}));
  5. })(this, (function (exports) { 'use strict';
  6. function earcut(data, holeIndices, dim = 2) {
  7. const hasHoles = holeIndices && holeIndices.length;
  8. const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
  9. let outerNode = linkedList(data, 0, outerLen, dim, true);
  10. const triangles = [];
  11. if (!outerNode || outerNode.next === outerNode.prev) return triangles;
  12. let minX, minY, invSize;
  13. if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
  14. // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
  15. if (data.length > 80 * dim) {
  16. minX = data[0];
  17. minY = data[1];
  18. let maxX = minX;
  19. let maxY = minY;
  20. for (let i = dim; i < outerLen; i += dim) {
  21. const x = data[i];
  22. const y = data[i + 1];
  23. if (x < minX) minX = x;
  24. if (y < minY) minY = y;
  25. if (x > maxX) maxX = x;
  26. if (y > maxY) maxY = y;
  27. }
  28. // minX, minY and invSize are later used to transform coords into integers for z-order calculation
  29. invSize = Math.max(maxX - minX, maxY - minY);
  30. invSize = invSize !== 0 ? 32767 / invSize : 0;
  31. }
  32. earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
  33. return triangles;
  34. }
  35. // create a circular doubly linked list from polygon points in the specified winding order
  36. function linkedList(data, start, end, dim, clockwise) {
  37. let last;
  38. if (clockwise === (signedArea(data, start, end, dim) > 0)) {
  39. for (let i = start; i < end; i += dim) last = insertNode(i / dim | 0, data[i], data[i + 1], last);
  40. } else {
  41. for (let i = end - dim; i >= start; i -= dim) last = insertNode(i / dim | 0, data[i], data[i + 1], last);
  42. }
  43. if (last && equals(last, last.next)) {
  44. removeNode(last);
  45. last = last.next;
  46. }
  47. return last;
  48. }
  49. // eliminate colinear or duplicate points
  50. function filterPoints(start, end) {
  51. if (!start) return start;
  52. if (!end) end = start;
  53. let p = start,
  54. again;
  55. do {
  56. again = false;
  57. if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
  58. removeNode(p);
  59. p = end = p.prev;
  60. if (p === p.next) break;
  61. again = true;
  62. } else {
  63. p = p.next;
  64. }
  65. } while (again || p !== end);
  66. return end;
  67. }
  68. // main ear slicing loop which triangulates a polygon (given as a linked list)
  69. function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
  70. if (!ear) return;
  71. // interlink polygon nodes in z-order
  72. if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
  73. let stop = ear;
  74. // iterate through ears, slicing them one by one
  75. while (ear.prev !== ear.next) {
  76. const prev = ear.prev;
  77. const next = ear.next;
  78. if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
  79. triangles.push(prev.i, ear.i, next.i); // cut off the triangle
  80. removeNode(ear);
  81. // skipping the next vertex leads to less sliver triangles
  82. ear = next.next;
  83. stop = next.next;
  84. continue;
  85. }
  86. ear = next;
  87. // if we looped through the whole remaining polygon and can't find any more ears
  88. if (ear === stop) {
  89. // try filtering points and slicing again
  90. if (!pass) {
  91. earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
  92. // if this didn't work, try curing all small self-intersections locally
  93. } else if (pass === 1) {
  94. ear = cureLocalIntersections(filterPoints(ear), triangles);
  95. earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
  96. // as a last resort, try splitting the remaining polygon into two
  97. } else if (pass === 2) {
  98. splitEarcut(ear, triangles, dim, minX, minY, invSize);
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. // check whether a polygon node forms a valid ear with adjacent nodes
  105. function isEar(ear) {
  106. const a = ear.prev,
  107. b = ear,
  108. c = ear.next;
  109. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  110. // now make sure we don't have other points inside the potential ear
  111. const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
  112. // triangle bbox
  113. const x0 = Math.min(ax, bx, cx),
  114. y0 = Math.min(ay, by, cy),
  115. x1 = Math.max(ax, bx, cx),
  116. y1 = Math.max(ay, by, cy);
  117. let p = c.next;
  118. while (p !== a) {
  119. if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
  120. pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) &&
  121. area(p.prev, p, p.next) >= 0) return false;
  122. p = p.next;
  123. }
  124. return true;
  125. }
  126. function isEarHashed(ear, minX, minY, invSize) {
  127. const a = ear.prev,
  128. b = ear,
  129. c = ear.next;
  130. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  131. const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
  132. // triangle bbox
  133. const x0 = Math.min(ax, bx, cx),
  134. y0 = Math.min(ay, by, cy),
  135. x1 = Math.max(ax, bx, cx),
  136. y1 = Math.max(ay, by, cy);
  137. // z-order range for the current triangle bbox;
  138. const minZ = zOrder(x0, y0, minX, minY, invSize),
  139. maxZ = zOrder(x1, y1, minX, minY, invSize);
  140. let p = ear.prevZ,
  141. n = ear.nextZ;
  142. // look for points inside the triangle in both directions
  143. while (p && p.z >= minZ && n && n.z <= maxZ) {
  144. if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
  145. pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
  146. p = p.prevZ;
  147. if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
  148. pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
  149. n = n.nextZ;
  150. }
  151. // look for remaining points in decreasing z-order
  152. while (p && p.z >= minZ) {
  153. if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
  154. pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
  155. p = p.prevZ;
  156. }
  157. // look for remaining points in increasing z-order
  158. while (n && n.z <= maxZ) {
  159. if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
  160. pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
  161. n = n.nextZ;
  162. }
  163. return true;
  164. }
  165. // go through all polygon nodes and cure small local self-intersections
  166. function cureLocalIntersections(start, triangles) {
  167. let p = start;
  168. do {
  169. const a = p.prev,
  170. b = p.next.next;
  171. if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
  172. triangles.push(a.i, p.i, b.i);
  173. // remove two nodes involved
  174. removeNode(p);
  175. removeNode(p.next);
  176. p = start = b;
  177. }
  178. p = p.next;
  179. } while (p !== start);
  180. return filterPoints(p);
  181. }
  182. // try splitting polygon into two and triangulate them independently
  183. function splitEarcut(start, triangles, dim, minX, minY, invSize) {
  184. // look for a valid diagonal that divides the polygon into two
  185. let a = start;
  186. do {
  187. let b = a.next.next;
  188. while (b !== a.prev) {
  189. if (a.i !== b.i && isValidDiagonal(a, b)) {
  190. // split the polygon in two by the diagonal
  191. let c = splitPolygon(a, b);
  192. // filter colinear points around the cuts
  193. a = filterPoints(a, a.next);
  194. c = filterPoints(c, c.next);
  195. // run earcut on each half
  196. earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
  197. earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
  198. return;
  199. }
  200. b = b.next;
  201. }
  202. a = a.next;
  203. } while (a !== start);
  204. }
  205. // link every hole into the outer loop, producing a single-ring polygon without holes
  206. function eliminateHoles(data, holeIndices, outerNode, dim) {
  207. const queue = [];
  208. for (let i = 0, len = holeIndices.length; i < len; i++) {
  209. const start = holeIndices[i] * dim;
  210. const end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  211. const list = linkedList(data, start, end, dim, false);
  212. if (list === list.next) list.steiner = true;
  213. queue.push(getLeftmost(list));
  214. }
  215. queue.sort(compareXYSlope);
  216. // process holes from left to right
  217. for (let i = 0; i < queue.length; i++) {
  218. outerNode = eliminateHole(queue[i], outerNode);
  219. }
  220. return outerNode;
  221. }
  222. function compareXYSlope(a, b) {
  223. let result = a.x - b.x;
  224. // when the left-most point of 2 holes meet at a vertex, sort the holes counterclockwise so that when we find
  225. // the bridge to the outer shell is always the point that they meet at.
  226. if (result === 0) {
  227. result = a.y - b.y;
  228. if (result === 0) {
  229. const aSlope = (a.next.y - a.y) / (a.next.x - a.x);
  230. const bSlope = (b.next.y - b.y) / (b.next.x - b.x);
  231. result = aSlope - bSlope;
  232. }
  233. }
  234. return result;
  235. }
  236. // find a bridge between vertices that connects hole with an outer ring and link it
  237. function eliminateHole(hole, outerNode) {
  238. const bridge = findHoleBridge(hole, outerNode);
  239. if (!bridge) {
  240. return outerNode;
  241. }
  242. const bridgeReverse = splitPolygon(bridge, hole);
  243. // filter collinear points around the cuts
  244. filterPoints(bridgeReverse, bridgeReverse.next);
  245. return filterPoints(bridge, bridge.next);
  246. }
  247. // David Eberly's algorithm for finding a bridge between hole and outer polygon
  248. function findHoleBridge(hole, outerNode) {
  249. let p = outerNode;
  250. const hx = hole.x;
  251. const hy = hole.y;
  252. let qx = -Infinity;
  253. let m;
  254. // find a segment intersected by a ray from the hole's leftmost point to the left;
  255. // segment's endpoint with lesser x will be potential connection point
  256. // unless they intersect at a vertex, then choose the vertex
  257. if (equals(hole, p)) return p;
  258. do {
  259. if (equals(hole, p.next)) return p.next;
  260. else if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
  261. const x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
  262. if (x <= hx && x > qx) {
  263. qx = x;
  264. m = p.x < p.next.x ? p : p.next;
  265. if (x === hx) return m; // hole touches outer segment; pick leftmost endpoint
  266. }
  267. }
  268. p = p.next;
  269. } while (p !== outerNode);
  270. if (!m) return null;
  271. // look for points inside the triangle of hole point, segment intersection and endpoint;
  272. // if there are no points found, we have a valid connection;
  273. // otherwise choose the point of the minimum angle with the ray as connection point
  274. const stop = m;
  275. const mx = m.x;
  276. const my = m.y;
  277. let tanMin = Infinity;
  278. p = m;
  279. do {
  280. if (hx >= p.x && p.x >= mx && hx !== p.x &&
  281. pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
  282. const tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
  283. if (locallyInside(p, hole) &&
  284. (tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
  285. m = p;
  286. tanMin = tan;
  287. }
  288. }
  289. p = p.next;
  290. } while (p !== stop);
  291. return m;
  292. }
  293. // whether sector in vertex m contains sector in vertex p in the same coordinates
  294. function sectorContainsSector(m, p) {
  295. return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
  296. }
  297. // interlink polygon nodes in z-order
  298. function indexCurve(start, minX, minY, invSize) {
  299. let p = start;
  300. do {
  301. if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
  302. p.prevZ = p.prev;
  303. p.nextZ = p.next;
  304. p = p.next;
  305. } while (p !== start);
  306. p.prevZ.nextZ = null;
  307. p.prevZ = null;
  308. sortLinked(p);
  309. }
  310. // Simon Tatham's linked list merge sort algorithm
  311. // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
  312. function sortLinked(list) {
  313. let numMerges;
  314. let inSize = 1;
  315. do {
  316. let p = list;
  317. let e;
  318. list = null;
  319. let tail = null;
  320. numMerges = 0;
  321. while (p) {
  322. numMerges++;
  323. let q = p;
  324. let pSize = 0;
  325. for (let i = 0; i < inSize; i++) {
  326. pSize++;
  327. q = q.nextZ;
  328. if (!q) break;
  329. }
  330. let qSize = inSize;
  331. while (pSize > 0 || (qSize > 0 && q)) {
  332. if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
  333. e = p;
  334. p = p.nextZ;
  335. pSize--;
  336. } else {
  337. e = q;
  338. q = q.nextZ;
  339. qSize--;
  340. }
  341. if (tail) tail.nextZ = e;
  342. else list = e;
  343. e.prevZ = tail;
  344. tail = e;
  345. }
  346. p = q;
  347. }
  348. tail.nextZ = null;
  349. inSize *= 2;
  350. } while (numMerges > 1);
  351. return list;
  352. }
  353. // z-order of a point given coords and inverse of the longer side of data bbox
  354. function zOrder(x, y, minX, minY, invSize) {
  355. // coords are transformed into non-negative 15-bit integer range
  356. x = (x - minX) * invSize | 0;
  357. y = (y - minY) * invSize | 0;
  358. x = (x | (x << 8)) & 0x00FF00FF;
  359. x = (x | (x << 4)) & 0x0F0F0F0F;
  360. x = (x | (x << 2)) & 0x33333333;
  361. x = (x | (x << 1)) & 0x55555555;
  362. y = (y | (y << 8)) & 0x00FF00FF;
  363. y = (y | (y << 4)) & 0x0F0F0F0F;
  364. y = (y | (y << 2)) & 0x33333333;
  365. y = (y | (y << 1)) & 0x55555555;
  366. return x | (y << 1);
  367. }
  368. // find the leftmost node of a polygon ring
  369. function getLeftmost(start) {
  370. let p = start,
  371. leftmost = start;
  372. do {
  373. if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
  374. p = p.next;
  375. } while (p !== start);
  376. return leftmost;
  377. }
  378. // check if a point lies within a convex triangle
  379. function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
  380. return (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
  381. (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
  382. (bx - px) * (cy - py) >= (cx - px) * (by - py);
  383. }
  384. // check if a point lies within a convex triangle but false if its equal to the first point of the triangle
  385. function pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, px, py) {
  386. return !(ax === px && ay === py) && pointInTriangle(ax, ay, bx, by, cx, cy, px, py);
  387. }
  388. // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
  389. function isValidDiagonal(a, b) {
  390. return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // doesn't intersect other edges
  391. (locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
  392. (area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
  393. equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
  394. }
  395. // signed area of a triangle
  396. function area(p, q, r) {
  397. return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  398. }
  399. // check if two points are equal
  400. function equals(p1, p2) {
  401. return p1.x === p2.x && p1.y === p2.y;
  402. }
  403. // check if two segments intersect
  404. function intersects(p1, q1, p2, q2) {
  405. const o1 = sign(area(p1, q1, p2));
  406. const o2 = sign(area(p1, q1, q2));
  407. const o3 = sign(area(p2, q2, p1));
  408. const o4 = sign(area(p2, q2, q1));
  409. if (o1 !== o2 && o3 !== o4) return true; // general case
  410. if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
  411. if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
  412. if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
  413. if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
  414. return false;
  415. }
  416. // for collinear points p, q, r, check if point q lies on segment pr
  417. function onSegment(p, q, r) {
  418. return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
  419. }
  420. function sign(num) {
  421. return num > 0 ? 1 : num < 0 ? -1 : 0;
  422. }
  423. // check if a polygon diagonal intersects any polygon segments
  424. function intersectsPolygon(a, b) {
  425. let p = a;
  426. do {
  427. if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  428. intersects(p, p.next, a, b)) return true;
  429. p = p.next;
  430. } while (p !== a);
  431. return false;
  432. }
  433. // check if a polygon diagonal is locally inside the polygon
  434. function locallyInside(a, b) {
  435. return area(a.prev, a, a.next) < 0 ?
  436. area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
  437. area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
  438. }
  439. // check if the middle point of a polygon diagonal is inside the polygon
  440. function middleInside(a, b) {
  441. let p = a;
  442. let inside = false;
  443. const px = (a.x + b.x) / 2;
  444. const py = (a.y + b.y) / 2;
  445. do {
  446. if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
  447. (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
  448. inside = !inside;
  449. p = p.next;
  450. } while (p !== a);
  451. return inside;
  452. }
  453. // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
  454. // if one belongs to the outer ring and another to a hole, it merges it into a single ring
  455. function splitPolygon(a, b) {
  456. const a2 = createNode(a.i, a.x, a.y),
  457. b2 = createNode(b.i, b.x, b.y),
  458. an = a.next,
  459. bp = b.prev;
  460. a.next = b;
  461. b.prev = a;
  462. a2.next = an;
  463. an.prev = a2;
  464. b2.next = a2;
  465. a2.prev = b2;
  466. bp.next = b2;
  467. b2.prev = bp;
  468. return b2;
  469. }
  470. // create a node and optionally link it with previous one (in a circular doubly linked list)
  471. function insertNode(i, x, y, last) {
  472. const p = createNode(i, x, y);
  473. if (!last) {
  474. p.prev = p;
  475. p.next = p;
  476. } else {
  477. p.next = last.next;
  478. p.prev = last;
  479. last.next.prev = p;
  480. last.next = p;
  481. }
  482. return p;
  483. }
  484. function removeNode(p) {
  485. p.next.prev = p.prev;
  486. p.prev.next = p.next;
  487. if (p.prevZ) p.prevZ.nextZ = p.nextZ;
  488. if (p.nextZ) p.nextZ.prevZ = p.prevZ;
  489. }
  490. function createNode(i, x, y) {
  491. return {
  492. i, // vertex index in coordinates array
  493. x, y, // vertex coordinates
  494. prev: null, // previous and next vertex nodes in a polygon ring
  495. next: null,
  496. z: 0, // z-order curve value
  497. prevZ: null, // previous and next nodes in z-order
  498. nextZ: null,
  499. steiner: false // indicates whether this is a steiner point
  500. };
  501. }
  502. // return a percentage difference between the polygon area and its triangulation area;
  503. // used to verify correctness of triangulation
  504. function deviation(data, holeIndices, dim, triangles) {
  505. const hasHoles = holeIndices && holeIndices.length;
  506. const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
  507. let polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
  508. if (hasHoles) {
  509. for (let i = 0, len = holeIndices.length; i < len; i++) {
  510. const start = holeIndices[i] * dim;
  511. const end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  512. polygonArea -= Math.abs(signedArea(data, start, end, dim));
  513. }
  514. }
  515. let trianglesArea = 0;
  516. for (let i = 0; i < triangles.length; i += 3) {
  517. const a = triangles[i] * dim;
  518. const b = triangles[i + 1] * dim;
  519. const c = triangles[i + 2] * dim;
  520. trianglesArea += Math.abs(
  521. (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
  522. (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
  523. }
  524. return polygonArea === 0 && trianglesArea === 0 ? 0 :
  525. Math.abs((trianglesArea - polygonArea) / polygonArea);
  526. }
  527. function signedArea(data, start, end, dim) {
  528. let sum = 0;
  529. for (let i = start, j = end - dim; i < end; i += dim) {
  530. sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
  531. j = i;
  532. }
  533. return sum;
  534. }
  535. // turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
  536. function flatten(data) {
  537. const vertices = [];
  538. const holes = [];
  539. const dimensions = data[0][0].length;
  540. let holeIndex = 0;
  541. let prevLen = 0;
  542. for (const ring of data) {
  543. for (const p of ring) {
  544. for (let d = 0; d < dimensions; d++) vertices.push(p[d]);
  545. }
  546. if (prevLen) {
  547. holeIndex += prevLen;
  548. holes.push(holeIndex);
  549. }
  550. prevLen = ring.length;
  551. }
  552. return {vertices, holes, dimensions};
  553. }
  554. exports.default = earcut;
  555. exports.deviation = deviation;
  556. exports.flatten = flatten;
  557. Object.defineProperty(exports, '__esModule', { value: true });
  558. }));