Created
June 18, 2017 23:41
-
-
Save stephanbogner/19e4642e2a59e9b85cec845c4f5d00b5 to your computer and use it in GitHub Desktop.
Example for ternary plotting (https://en.wikipedia.org/wiki/Ternary_plot) like in moovellab's mobility space explorer
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
console.log(); | |
console.log('Examples'); | |
console.log(); | |
console.log('- All Bike (dot bottom right)'); | |
console.log(' ', mobilityTriangle(1123, 0, 0) ); | |
console.log(); | |
console.log('- All Car (dot top)'); | |
console.log(' ', mobilityTriangle(0, 124, 0) ); | |
console.log(); | |
console.log('- All Rail (dot bottom left)'); | |
console.log(' ', mobilityTriangle(0, 0, 647) ); | |
console.log(); | |
console.log('- Balanced (dot in center)'); | |
console.log(' ', mobilityTriangle(25, 25, 25) ); | |
console.log(); | |
console.log('- More realistic example'); | |
console.log(' ', mobilityTriangle(15, 231, 45) ); | |
function mobilityTriangle(bike, car, rail) { | |
return coordinateInTriangle(bike, car, rail); | |
} | |
function coordinateInTriangle(a, b, c, edgeLength) { | |
edgeLength = edgeLength || 1; | |
// See https://en.wikipedia.org/wiki/Ternary_plot | |
var sum = a + b + c; | |
var fractionA = a / sum; | |
var fractionB = b / sum; | |
var fractionC = c / sum; | |
var y = fractionB * Math.sin(toRadians(60)); | |
var x = fractionA + y * cot(toRadians(60)); | |
return { | |
"x": x * edgeLength, | |
"y": y * edgeLength | |
} | |
} | |
function toRadians(degrees) { | |
return degrees * Math.PI / 180; | |
} | |
function toDegrees(radians) { | |
return radians / Math.PI * 180; | |
} | |
function cot(x) { | |
return 1 / Math.tan(x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment