Skip to content

Instantly share code, notes, and snippets.

Last active Nov 25, 2019
What would you like to do?
Smoother polygon transitions

Smooth transitioning US tour in the same vein as this example. Steps:

  1. Compares both shapes and adds evenly-spaced points to whichever polygon has fewer so that both have the same number of points
  2. Picks the winding of the first polygon that minimizes the sum of the squared distances between point pairs

Some possible improvements:

  • Adding additional points to both shapes first such that every segment longer than a certain distance is bisected
  • Tweaking the placement of added points with simulated annealing
  • Using a cost function that factors in self-intersections at the halfway mark in addition to distance traveled
  • Try reversing the order of points too

States are preprojected to appropriate State Plane projections and don't include islands or multipart states (AK, HI, MI).

See also: Jigsaw morphing

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
path {
fill: #e3e3e3;
stroke-width: 1px;
stroke: #666;
circle {
stroke: none;
fill: #fc0;
.added {
fill: #f0f;
<svg width="960" height="500"></svg>
<script src=""></script>
<script src=""></script>
var svg ="svg"),
path = svg.append("path"),
circles = svg.append("g");
d3.json("us.topo.json", function(err, topo){
var states = topojson.feature(topo, topo.objects.states){
return d.geometry.coordinates[0];
function draw() {
var a = states[0].slice(0),
b = states[1].slice(0);
// Same number of points on each ring
if (a.length < b.length) {
addPoints(a, b.length - a.length);
} else if (b.length < a.length) {
addPoints(b, a.length - b.length);
// Pick optimal winding
a = wind(a, b);
path.attr("d", join(a));
// Redraw points
// Morph
var t = d3.transition()
.on("end", function(){
setTimeout(draw, 100);
.attr("d", join(b));
return d[0];
return d[1];
function updateCircles(sel) {
var circles = sel.selectAll("circle")
.data(function(d){ return d; });
var merged = circles.enter()
.attr("r", 2)
merged.classed("added", function(d){
return d.added;
return d[0];
return d[1];
function addPoints(ring, numPoints) {
var desiredLength = ring.length + numPoints,
step = d3.polygonLength(ring) / numPoints;
var i = 0,
cursor = 0,
insertAt = step / 2;
do {
var a = ring[i],
b = ring[(i + 1) % ring.length];
var segment = distanceBetween(a, b);
if (insertAt <= cursor + segment) {
ring.splice(i + 1, 0, pointBetween(a, b, (insertAt - cursor) / segment));
insertAt += step;
cursor += segment;
} while (ring.length < desiredLength);
function pointBetween(a, b, pct) {
var point = [
a[0] + (b[0] - a[0]) * pct,
a[1] + (b[1] - a[1]) * pct
point.added = true;
return point;
function distanceBetween(a, b) {
return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2));
function join(d) {
return "M" + d.join("L") + "Z";
function wind(ring, vs) {
var len = ring.length,
min = Infinity,
for (var offset = 0, len = ring.length; offset < len; offset++) {
var sum = d3.sum(, i){
var distance = distanceBetween(ring[(offset + i) % len], p);
return distance * distance;
if (sum < min) {
min = sum;
bestOffset = offset;
return ring.slice(bestOffset).concat(ring.slice(0, bestOffset));
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copy link

alexjlockwood commented Jul 6, 2017

Just curious... could you explain this possible improvement in a bit more detail? I wasn't sure what you meant.

Using a cost function that factors in self-intersections at the halfway mark in addition to distance traveled

Copy link

alexjlockwood commented Jul 6, 2017

I'm also curious about this possible improvement:

Tweaking the placement of added points with simulated annealing

Is there a specific simulated annealing algorithm that could be used here? I assume that you would want to place the points in such a way that minimizes the distance traveled during the morph... but the points are added before the optimal winding is calculated, so I'm not sure how this would be possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment