MCPcopy Create free account
hub / github.com/breck7/scroll / packSiblingsRandom

Function packSiblingsRandom

external/.d3.js:13459–13525  ·  view source on GitHub ↗
(circles, random)

Source from the content-addressed store, hash-verified

13457}
13458
13459function packSiblingsRandom(circles, random) {
13460 if (!(n = (circles = array$1(circles)).length)) return 0;
13461
13462 var a, b, c, n, aa, ca, i, j, k, sj, sk;
13463
13464 // Place the first circle.
13465 a = circles[0], a.x = 0, a.y = 0;
13466 if (!(n > 1)) return a.r;
13467
13468 // Place the second circle.
13469 b = circles[1], a.x = -b.r, b.x = a.r, b.y = 0;
13470 if (!(n > 2)) return a.r + b.r;
13471
13472 // Place the third circle.
13473 place(b, a, c = circles[2]);
13474
13475 // Initialize the front-chain using the first three circles a, b and c.
13476 a = new Node(a), b = new Node(b), c = new Node(c);
13477 a.next = c.previous = b;
13478 b.next = a.previous = c;
13479 c.next = b.previous = a;
13480
13481 // Attempt to place each remaining circle…
13482 pack: for (i = 3; i < n; ++i) {
13483 place(a._, b._, c = circles[i]), c = new Node(c);
13484
13485 // Find the closest intersecting circle on the front-chain, if any.
13486 // “Closeness” is determined by linear distance along the front-chain.
13487 // “Ahead” or “behind” is likewise determined by linear distance.
13488 j = b.next, k = a.previous, sj = b._.r, sk = a._.r;
13489 do {
13490 if (sj <= sk) {
13491 if (intersects(j._, c._)) {
13492 b = j, a.next = b, b.previous = a, --i;
13493 continue pack;
13494 }
13495 sj += j._.r, j = j.next;
13496 } else {
13497 if (intersects(k._, c._)) {
13498 a = k, a.next = b, b.previous = a, --i;
13499 continue pack;
13500 }
13501 sk += k._.r, k = k.previous;
13502 }
13503 } while (j !== k.next);
13504
13505 // Success! Insert the new circle c between a and b.
13506 c.previous = a, c.next = b, a.next = b.previous = b = c;
13507
13508 // Compute the new closest circle pair to the centroid.
13509 aa = score(a);
13510 while ((c = c.next) !== b) {
13511 if ((ca = score(c)) < aa) {
13512 a = c, aa = ca;
13513 }
13514 }
13515 b = a.next;
13516 }

Callers 2

siblingsFunction · 0.85
packChildrenRandomFunction · 0.85

Calls 5

array$1Function · 0.85
intersectsFunction · 0.85
scoreFunction · 0.85
packEncloseRandomFunction · 0.85
placeFunction · 0.70

Tested by

no test coverage detected