0
0

知名度低そうなシェルピンスキーのギャスケットの作り方

Last updated at Posted at 2024-10-03

1. 真っ黒!

2. 右下を白にする

3. 各黒い区間の右下を白にする

4. 繰り返す

5. 繰り返す

6. 繰り返しまくる!

終わり。

どうでもいいけど、こいつ(n→∞)の面積は0らしいね。面白い。

関連

おまけ

JSのコード
const canvas = document.querySelector("canvas");
const context = canvas.getContext("2d");
canvas.width = 800;
canvas.height = 800;

const count = 10;

draw();

function draw() {
    context.fillStyle = "black";
    context.fillRect(0, 0, canvas.width, canvas.height);

    let blockList = [{
        color: "black",
        startX: 0,
        startY: 0,
        width: canvas.width,
        height: canvas.height
    }];

    for (let i = 0; i <= count; i++) {
        let tmpBlockList = [];
        for (const block of blockList) {
            if (block.width < 2 || block.height < 2) {
                i = Number.POSITIVE_INFINITY;
                break;
            }
            drawBlock(block);
            if (block.color === "black") {
                tmpBlockList = tmpBlockList.concat(splitBlock(block));
            }
        }
        blockList = tmpBlockList;
    }
}

function splitBlock(block) {
    const {startX, startY, width, height} = block;

    const width1 = Math.floor(width / 2);
    const width2 = width - width1;
    const height1 = Math.floor(height / 2);
    const height2 = height - height1;
    const startX1 = startX;
    const startX2 = startX + width1;
    const startY1 = startY;
    const startY2 = startY + height1;

    return [
        {color: "black", startX: startX1, startY: startY1, width: width1, height: height1},
        {color: "black", startX: startX2, startY: startY1, width: width2, height: height1},
        {color: "black", startX: startX1, startY: startY2, width: width1, height: height2},
        {color: "white", startX: startX2, startY: startY2, width: width2, height: height2},
    ];
}

function drawBlock(block) {
    if (block.color === "black") {
        return;
    }
    context.fillStyle = "white";
    context.fillRect(block.startX, block.startY, block.width, block.height);
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0