A Pen by Martijn Kunstman on CodePen.
Created
March 20, 2021 23:13
-
-
Save martijnkunstman/53ee2220a41f5f35168448bf1d18ab06 to your computer and use it in GitHub Desktop.
Sierpiński triangle - Pythagoras tree
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 id="myCanvas" width="600" height="600" style="border: 1px solid black"></canvas> |
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
| 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