Created
October 22, 2015 15:50
-
-
Save timruffles/5b6293bd57abb4bee742 to your computer and use it in GitHub Desktop.
normalizes a set of positive coordinates to 0..1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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