Published
Edited
Apr 18, 2021
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
MBP = {
let perimeter = [],
remainingIndices = d3.range(0, border.length),
lastBearing,
distance = 0;

//Starting point
perimeter.push(border[d3.maxIndex(border, p => p[1])]); // most northerly point
lastBearing = 90; // East

//Subsequent points
while (
!arrEq(perimeter[0], perimeter[perimeter.length - 1]) || //stop when back at first point
perimeter.length == 1
) {
yield Promises.tick(200, {
type: "LineString",
coordinates: perimeter,
distance
});

//bearings to candidate points
const bearings = remainingIndices.map(i =>
calculateBearing(perimeter[perimeter.length - 1], border[i])
);

//selection of minimal acceptable bearing
const nextIndex = d3.minIndex(bearings, b => {
if (b > lastBearing) {
return b - lastBearing;
} else if (b > lastBearing - 90) {
//great circle bearing is relative
return b - lastBearing;
} else if (b < lastBearing - 180) {
return b + 360 - lastBearing;
} else {
return NaN;
}
});

lastBearing = bearings[nextIndex];
perimeter.push(border[nextIndex]);
distance += calculateDistance(
perimeter[perimeter.length - 2],
perimeter[perimeter.length - 1]
);
}
yield { type: "Polygon", coordinates: [perimeter], distance };
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more