Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created October 22, 2015 15:50
Show Gist options
  • Save timruffles/5b6293bd57abb4bee742 to your computer and use it in GitHub Desktop.
Save timruffles/5b6293bd57abb4bee742 to your computer and use it in GitHub Desktop.
normalizes a set of positive coordinates to 0..1
function normaliseCoordinates(asObjects) {
var dimensions = {
x: [Infinity, -Infinity],
y: [Infinity, -Infinity],
}
var axisToDeltas = {
x: "width",
y: "height",
};
asObjects.forEach(function(row) {
["x","y"].forEach(function(d) {
var minMax = dimensions[d];
minMax[0] =
Math.min(minMax[0], row[d]);
minMax[1] =
Math.max(minMax[1], row[d]);
});
});
var transformed = [];
asObjects.forEach(function(row) {
var obj = {};
["x","y"].forEach(function(d) {
var minMax = dimensions[d];
obj[d] = row[d] - minMax[0];
obj[d] /= minMax[1];
});
transformed.push(obj);
});
return transformed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment