Public
Edited
Nov 16, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sceneJSON = jsonFile
? jsonFile.json()
: FileAttachment("scene-custom.json").json()
Insert cell
Insert cell
Insert cell
fractalScene = {
let scene = loadedScene.clone(); //initially clone loaded scene

//find the 4 possible matrix transformations: scale by half + translate in one of the 4 directions (A, B, C, D)
let matrices = [];
for (let tetra of scene.children) { //iterate over all scene objects
if (tetra.name.substring(0,4) === "copy") { //if object name begins with "copy":
matrices.push(tetra.matrix); //save matrix
}
}
//iterated function system (structure + color)
/* This function iterates over the different levels according to the choice of 'maxLevel' to create a fractal.
The 'colorLevel' option basically switches between two possible algorithms: 'mode X' and 'mode Y', as follows:
mode X: replicates 4 copies of each object, reduces their sizes by half, and translate each copy in 1 of 4 directions
mode Y: replicates 4 copies of each object, reduces their sizes by half, reposition each copy in 1 of 4 locations and
translate all 4 copies in the same direction, each object associated to only one of the 4 directions.
colorLevel 0 1 2 3 4 5
maxLevel 1: X Y
maxLevel 2: XX YX YY
maxLevel 3: XXX YXX YYX YYY
maxLevel 4: XXXX YXXX YYXX YYYX YYYY
maxLevel 5: XXXXX YXXXX YYXXX YYYXX YYYYX YYYYY
Level 0 objects are [copy1, copy2, copy3, copy4] and their matrices are [A, B, C, D]
Level 1 objects are: 4 times [copy1, copy2, copy3, copy4] and their respective matrices can be either:
[AA, AB, AC, AD], [BA, BB, BC, BD], [CA, CB, CC, CD], [DA, DB, DC, DC] using mode X, OR
[AA, BA, CA, DA], [AB, BB, CB, DB], [AC, BC, CC, DC], [AD, BD, CD, DD] using mode Y.
*/
for (let level = 1; level < maxLevel+1; ++level) { //for each level
let old_tetras = []; //list of old tetrahedrons
let new_tetras = []; //list of created tetrahedrons
for (let tetra of scene.children) { //iterate over scene objects
if (tetra.name.substring(0,4) === "copy") { //if object name begins with "copy":
old_tetras.push(tetra); //save old tetrahedron
for (let matrix of matrices) { //for the 4 possible transformations:
let t = tetra.clone(); // create cloned tetahedron
if (level > colorLevel)
t.matrix.multiplyMatrices(t.matrix, matrix); //mode X
else
t.matrix.multiplyMatrices(matrix, t.matrix); //mode Y
t.matrixAutoUpdate = false; //necessary technicality
new_tetras.push(t); //save new transformed tetahedron
}
}
}
for (let t of old_tetras) scene.remove(t); //remove old tetahedrons
for (let t of new_tetras) scene.add(t); //add new objects to the scene
} //end IFS
return scene
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more