Skip to content

Instantly share code, notes, and snippets.

@christophergorexyz
Last active April 7, 2022 17:18
Show Gist options
  • Select an option

  • Save christophergorexyz/afb76b36ae21b34b0cadfbaf35527d11 to your computer and use it in GitHub Desktop.

Select an option

Save christophergorexyz/afb76b36ae21b34b0cadfbaf35527d11 to your computer and use it in GitHub Desktop.
an implementation of the sierpenski carpet
<html>
<head>
<title>sierpenski</title>
</head>
<body>
<canvas id="sierpenski" width="2187" height="2187"></canvas>
<script>
let canvas = document.getElementById('sierpenski');
let context = canvas.getContext('2d');
context.fillStyle = 'black';
function sierpinski(size, x, y) {
if (size > 1) {
sierpinski(size / 3, x, y);
sierpinski(size / 3, x + 2 * size / 3, y);
sierpinski(size / 3, x + size / 3, y + size / 3);
sierpinski(size / 3, x, y + 2 * size / 3);
sierpinski(size / 3, x + 2 * size / 3, y + 2 * size / 3);
}
else {
return context.fillRect(x, y, 1, 1);
}
}
sierpinski(3 ** 7, 0, 0);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment