Skip to content

Instantly share code, notes, and snippets.

@martijnkunstman
Created March 20, 2021 23:13
Show Gist options
  • Select an option

  • Save martijnkunstman/53ee2220a41f5f35168448bf1d18ab06 to your computer and use it in GitHub Desktop.

Select an option

Save martijnkunstman/53ee2220a41f5f35168448bf1d18ab06 to your computer and use it in GitHub Desktop.
Sierpiński triangle - Pythagoras tree
<canvas id="myCanvas" width="600" height="600" style="border: 1px solid black"></canvas>
let c = document.getElementById("myCanvas");
let ctx = c.getContext("2d");
function drawLine(x, y, length, direction) {
if (length > 1) {
ctx.beginPath();
ctx.moveTo(x, y);
x = x + length * Math.cos((Math.PI * direction) / 180.0);
y = y + length * Math.sin((Math.PI * direction) / 180.0);
ctx.lineTo(x, y);
ctx.stroke();
let factor = 2;
drawLine(x, y, length / factor, direction);
drawLine(x, y, length / factor, direction + 120);
drawLine(x, y, length / factor, direction + 240);
}
}
drawLine(300, 600, 300, -90);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment