Created
July 18, 2013 02:37
-
-
Save timrwood/6026291 to your computer and use it in GitHub Desktop.
Approximating an answer to the Quarter Tank Problem http://mathworld.wolfram.com/Quarter-TankProblem.html
This file contains hidden or 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
<canvas></canvas> | |
<style>canvas{background:#ddd;}</style> | |
<script> | |
var canvas = document.getElementsByTagName('canvas')[0]; | |
var ctx = canvas.getContext('2d'); | |
var step = 0.01; | |
var pctx = 0.50; | |
var pcty = 0.06; | |
canvas.width = canvas.height = 400; | |
function heightToArea (h) { | |
var rad = h * Math.PI * 2; | |
return (rad - Math.sin(rad)) / (Math.PI * 2); | |
} | |
function areaToHeight (h) { | |
if (h > 0.5) { | |
h = h * h; | |
} | |
return h; | |
} | |
ctx.moveTo(0, 0); | |
for (var i = 0; i < 1 + step; i+= step) { | |
var x = canvas.width * i; | |
var y = canvas.height * heightToArea(i); | |
ctx.lineTo(x, y); | |
} | |
ctx.stroke(); | |
ctx.beginPath(); | |
ctx.strokeStyle = "#f00"; | |
ctx.moveTo(0, 0); | |
ctx.bezierCurveTo( | |
canvas.width * pctx, -canvas.height * pcty, | |
canvas.width * (1 - pctx), canvas.height * (pcty + 1), | |
canvas.width, canvas.height | |
); | |
ctx.stroke(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment