Skip to content

Commit 9b21dd3

Browse files
authored
Create cantor-set.js
1 parent 7edace2 commit 9b21dd3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

cantor-set.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const drawHorizontalLine = (canvas, x1, y1, length) => {
2+
canvas.beginPath();
3+
canvas.moveTo(x1, y1);
4+
canvas.lineTo(x1 + length, y1);
5+
canvas.stroke();
6+
};
7+
8+
const cantorSet = (canvas, x, y, length, levels) => {
9+
if (levels > 0) {
10+
drawHorizontalLine(canvas, x, y, length);
11+
cantorSet(canvas, x, y + 20, length / 3, levels - 1);
12+
cantorSet(canvas, x + 2 * length / 3, y + 20, length / 3, levels - 1);
13+
}
14+
};
15+
16+
const c = document.getElementById("myCanvas");
17+
const canvas = c.getContext("2d");
18+
cantorSet(canvas, 0, 0, 300, 3);

0 commit comments

Comments
 (0)