Sierpinski carpet

Whereas the previous program generates a stack of 2D Cantor sets over a range of levels, this program generates the set only at a single level:

Cantor dust and Sierpinski carpet

The program is written more generally to produce other fractal sets.

function makeCantor(retainF, level, mat, len=10) {
    if (level == 0) {
        let geom = new THREE.BoxGeometry(len, 0.4, len);
        return new THREE.Mesh(geom, mat);
    } else {
        let cantor = makeCantor(retainF, level-1, mat, len);
        let root = new THREE.Object3D();
        root.scale.set(1/3, 1, 1/3);
        for (x of [-len, 0, len]) {
            for (z of [-len, 0, len]) {
                if (retainF(x, z, len)) {
                    let clone = cantor.clone();
                    clone.position.set(x, 0, z);
                    root.add(clone);
                }
            }
        }
        return root;
    }
}

The first parameter, retainF, is a predicate function used to decide which level sets to retain. Recall, the variable cantor references a lower-level cantor set. A scaled-down copy of cantor is translated to the corner (x, 0, z) only if the call to retainF on line 11 returns true. To generate Cantor sets, we define retainF like this:

function retainCantor(x, z, len) {
    return (Math.abs(x) + Math.abs(z)) > len;
}

The position (x, 0, z) is a corner just if x=\pm len and z=\pm len, the condition which retainCantor tests.

To build a Sierpinski carpet, we start with a square. To generate the carpet at level n+1, generate it at level n and then move scaled-down clones to eight of nine possible positions, omitting only the center subsquare. To see a picture of this, choose Sierpinski carpet from the dropdown menu and set nbrLevels to 1. Its fractal dimension is d = \log_3 8\approx 1.89, much ‘denser’ than Cantor dust with dimension 1.26.

To generate the Sierpinski carpet, call makeCantor and pass the following function as the first argument:

function retainSierpinskiCarpet(x, z, len) {
    return (Math.abs(x) + Math.abs(z)) > 0;
}

The program positions a scaled-down clone at 8 positions, omitting only the center position (0, 0, 0).

The program on this page lets us view a sequence of level sets by stepping through them in time. The program on the previous page depicts a sequence as a stack of sets that extend into the third (vertical) dimension. This is possible because each level set is constructed in two dimensions, leaving an additional spatial dimension for the sequence. We won’t have the same luxury when generating fractal sets in three dimensions since there is no fourth spatial dimension. We’ll have to use time as our fourth dimension, for stepping through a sequence of level sets.