earcut.js 20 KB

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