MCPcopy Create free account
hub / github.com/microsoft/SandDance / packEnclose

Function packEnclose

docs/app/js/sanddance-app.js:137886–137939  ·  view source on GitHub ↗
(circles)

Source from the content-addressed store, hash-verified

137884 this.previous = null;
137885}
137886function packEnclose(circles) {
137887 if (!(n = (circles = (0, _arrayJsDefault.default)(circles)).length)) return 0;
137888 var a, b, c, n, aa, ca, i, j, k, sj, sk;
137889 // Place the first circle.
137890 a = circles[0], a.x = 0, a.y = 0;
137891 if (!(n > 1)) return a.r;
137892 // Place the second circle.
137893 b = circles[1], a.x = -b.r, b.x = a.r, b.y = 0;
137894 if (!(n > 2)) return a.r + b.r;
137895 // Place the third circle.
137896 place(b, a, c = circles[2]);
137897 // Initialize the front-chain using the first three circles a, b and c.
137898 a = new Node(a), b = new Node(b), c = new Node(c);
137899 a.next = c.previous = b;
137900 b.next = a.previous = c;
137901 c.next = b.previous = a;
137902 // Attempt to place each remaining circle…
137903 pack: for(i = 3; i < n; ++i){
137904 place(a._, b._, c = circles[i]), c = new Node(c);
137905 // Find the closest intersecting circle on the front-chain, if any.
137906 // “Closeness” is determined by linear distance along the front-chain.
137907 // “Ahead” or “behind” is likewise determined by linear distance.
137908 j = b.next, k = a.previous, sj = b._.r, sk = a._.r;
137909 do if (sj <= sk) {
137910 if (intersects(j._, c._)) {
137911 b = j, a.next = b, b.previous = a, --i;
137912 continue pack;
137913 }
137914 sj += j._.r, j = j.next;
137915 } else {
137916 if (intersects(k._, c._)) {
137917 a = k, a.next = b, b.previous = a, --i;
137918 continue pack;
137919 }
137920 sk += k._.r, k = k.previous;
137921 }
137922 while (j !== k.next);
137923 // Success! Insert the new circle c between a and b.
137924 c.previous = a, c.next = b, a.next = b.previous = b = c;
137925 // Compute the new closest circle pair to the centroid.
137926 aa = score(a);
137927 while((c = c.next) !== b)if ((ca = score(c)) < aa) a = c, aa = ca;
137928 b = a.next;
137929 }
137930 // Compute the enclosing circle of the front chain.
137931 a = [
137932 b._
137933 ], c = b;
137934 while((c = c.next) !== b)a.push(c._);
137935 c = (0, _encloseJsDefault.default)(a);
137936 // Translate the circles to put the enclosing circle around the origin.
137937 for(i = 0; i < n; ++i)a = circles[i], a.x -= c.x, a.y -= c.y;
137938 return c.r;
137939}
137940exports.default = function(circles) {
137941 packEnclose(circles);
137942 return circles;

Callers 1

sanddance-app.jsFile · 0.70

Calls 3

placeFunction · 0.70
intersectsFunction · 0.70
scoreFunction · 0.70

Tested by

no test coverage detected