+ radius = d3.median(points, function(p) { return iD.geo.euclideanDistance(centroid, p); }),
+ sign = d3.geom.polygon(points).area() > 0 ? 1 : -1,
+ ids;
+
+ // we need atleast two key nodes for the algorithm to work
+ if (!keyNodes.length) {
+ keyNodes = [nodes[0]];
+ keyPoints = [points[0]];
+ }
+
+ if (keyNodes.length == 1) {
+ var index = nodes.indexOf(keyNodes[0]),
+ oppositeIndex = Math.floor((index + nodes.length / 2) % nodes.length);
+
+ keyNodes.push(nodes[oppositeIndex]);
+ keyPoints.push(points[oppositeIndex]);
+ }
+
+ // key points and nodes are those connected to the ways,
+ // they are projected onto the circle, inbetween nodes are moved
+ // to constant internals between key nodes, extra inbetween nodes are
+ // added if necessary.
+ for (var i = 0; i < keyPoints.length; i++) {
+ var nextKeyNodeIndex = (i + 1) % keyNodes.length,
+ startNodeIndex = nodes.indexOf(keyNodes[i]),
+ endNodeIndex = nodes.indexOf(keyNodes[nextKeyNodeIndex]),
+ numberNewPoints = -1,
+ indexRange = endNodeIndex - startNodeIndex,
+ distance, totalAngle, eachAngle, startAngle, endAngle,
+ angle, loc, node, j;
+
+ if (indexRange < 0) {
+ indexRange += nodes.length;
+ }
+
+ // position this key node
+ distance = iD.geo.euclideanDistance(centroid, keyPoints[i]);
+ keyPoints[i] = [
+ centroid[0] + (keyPoints[i][0] - centroid[0]) / distance * radius,
+ centroid[1] + (keyPoints[i][1] - centroid[1]) / distance * radius];
+ graph = graph.replace(keyNodes[i].move(projection.invert(keyPoints[i])));
+
+ // figure out the between delta angle we want to match to
+ startAngle = Math.atan2(keyPoints[i][1] - centroid[1], keyPoints[i][0] - centroid[0]);
+ endAngle = Math.atan2(keyPoints[nextKeyNodeIndex][1] - centroid[1], keyPoints[nextKeyNodeIndex][0] - centroid[0]);
+ totalAngle = endAngle - startAngle;
+
+ // detects looping around -pi/pi
+ if (totalAngle*sign > 0) {
+ totalAngle = -sign * (2 * Math.PI - Math.abs(totalAngle));
+ }
+
+ do {
+ numberNewPoints++;
+ eachAngle = totalAngle / (indexRange + numberNewPoints);
+ } while (Math.abs(eachAngle) > maxAngle);
+
+ // move existing points
+ for (j = 1; j < indexRange; j++) {
+ angle = startAngle + j * eachAngle;