Skip to content

Instantly share code, notes, and snippets.

@namin
Created December 17, 2013 00:56
Show Gist options
  • Save namin/7998099 to your computer and use it in GitHub Desktop.
Save namin/7998099 to your computer and use it in GitHub Desktop.
Nature of Code: Cantor Set
<html>
<head>
<style>
* { margin: 0; padding: 0; overflow: hidden;}
#canvas { display: block; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
window.onload = function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var W = window.innerWidth, H = window.innerHeight;
canvas.width = W; canvas.height = H;
function cantor(x, y, len)
{
if (len >= 1) {
ctx.strokeStyle = "black";
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x+len, y);
ctx.lineWidth = 5;
ctx.stroke();
y += 20;
cantor(x, y, len/3);
cantor(x+len*2/3, y, len/3);
}
}
cantor(10, 10, W-20);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment