fractals = {
let canvas;
if (example == "Sierpinski triangle") {
let sierpIFS = new IteratedFunctionSystem([
scale(1 / 2),
scale(1 / 2, [1, 0]),
scale(1 / 2, [1 / 2, Math.sqrt(3) / 2])
]);
canvas = sierpIFS.render_deterministic({
max_depth: 7,
init: [
[0, 0],
[1, 0],
[1 / 2, Math.sqrt(3) / 2]
],
extent: [
[0, 1],
[-0.1, 0.9]
],
image_width: 512,
image_height: 512
});
} else if (example == "Sparse spiral") {
let ifs = new IteratedFunctionSystem([
scale(0.95).compose(rotate(15 * degree)),
scale(0.15, [1, 0])
]);
canvas = ifs.render_stochastic({
n: 20000,
image_width: 1024,
image_height: 1024
});
} else if (example == "Phylo-spiral") {
let ifs = new IteratedFunctionSystem([
scale(0.99).compose(rotate(137.5 * degree)),
scale(0.1, [1, 0])
]);
canvas = ifs.render_stochastic({
n: 100000,
image_width: 1024,
image_height: 1024
});
} else if (example == "Koch curve") {
let f0 = scale(1 / 3, [0, 0]);
let f1 = shift([1 / 3, 0])
.compose(scale(1 / 3))
.compose(rotate(Math.PI / 3));
let f2 = shift([1 / 2, Math.sqrt(3) / 6])
.compose(scale(1 / 3))
.compose(rotate(-Math.PI / 3));
let f3 = scale(1 / 3, [1, 0]);
canvas = new IteratedFunctionSystem([f0, f1, f2, f3]).render_deterministic({
max_depth: 6,
init: [
[0, 0],
[1, 0]
],
extent: [
[0, 1],
[0, 1 / 2]
],
image_width: 512,
image_height: 256
});
} else if (example == "Kieswetter's curve") {
canvas = new IteratedFunctionSystem([
new AffineFunction([
[
[1 / 4, 0],
[0, -1 / 2]
],
[0, 0]
]),
new AffineFunction([
[
[1 / 4, 0],
[0, 1 / 2]
],
[1 / 4, -1 / 2]
]),
new AffineFunction([
[
[1 / 4, 0],
[0, 1 / 2]
],
[1 / 2, 0]
]),
new AffineFunction([
[
[1 / 4, 0],
[0, 1 / 2]
],
[3 / 4, 1 / 2]
])
]).render_deterministic({
max_depth: 6,
init: [
[0, 0],
[1, 1]
],
extent: [
[0, 1],
[-1, 1]
],
image_width: 512,
image_height: 1024
});
} else if (example == "Barnsley's fern") {
canvas = new IteratedFunctionSystem([
[
[
[0.85, 0.04],
[-0.04, 0.85]
],
[0, 1.6]
],
[
[
[-0.15, 0.28],
[0.26, 0.24]
],
[0, 0.44]
],
[
[
[0.2, -0.26],
[0.23, 0.22]
],
[0, 1.6]
],
[
[
[0, 0],
[0, 0.16]
],
[0, 0]
]
]).render_stochastic({ n: 80000, image_width: 512, image_height: 1024 });
} else if (example == "Line segment") {
canvas = d3.create("canvas").attr("width", 1024).attr("height", 256).node();
let ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 200);
ctx.lineTo(1000, 20);
ctx.lineWidth = 1.5;
ctx.stroke();
ctx.closePath();
} else if (example == "Squiggle") {
let w = 1024;
let h = 512;
let xmin = 0;
let xmax = 13;
let ymin = 0;
let ymax = 7;
let x_scale = d3.scaleLinear().domain([xmin, xmax]).range([0, w]);
let y_scale = d3.scaleLinear().domain([ymin, ymax]).range([h, 0]);
canvas = d3.create("canvas").attr("width", w).attr("height", h).node();
let ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(x_scale(0), y_scale(0));
for (let t = 0; t <= 2 * Math.PI; t = t + Math.PI / 100) {
ctx.lineTo(x_scale(Math.sin(3 * t) + 2 * t), y_scale(t));
}
ctx.lineWidth = 1.5;
ctx.stroke();
ctx.closePath();
} else if (example == "Solid circle") {
canvas = d3.create("canvas").attr("width", 512).attr("height", 512).node();
let ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(250, 255, 200, 0, 2 * Math.PI);
ctx.fill();
} else if (example == "British coast") {
let pts = british_coast.features[0].geometry.coordinates;
let xpts = pts.map((pt) => pt[0]);
let xmin = Math.floor(d3.min(xpts));
let xmax = Math.ceil(d3.max(xpts));
let ypts = pts.map((pt) => pt[1]);
let ymin = Math.floor(d3.min(ypts));
let ymax = Math.ceil(d3.max(ypts));
let w = 512;
let h = (w * (ymax - ymin)) / (xmax - xmin);
canvas = d3.create("canvas").attr("width", w).attr("height", h).node();
let x_scale = d3.scaleLinear().domain([xmin, xmax]).range([0, w]);
let y_scale = d3.scaleLinear().domain([ymin, ymax]).range([h, 0]);
let ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(x_scale(xpts[0]), y_scale(ypts[0]));
for (let i = 1; i < pts.length; i++) {
ctx.lineTo(x_scale(xpts[i]), y_scale(ypts[i]));
}
ctx.stroke();
ctx.closePath();
} else if (example == "Trees") {
let bits = await FileAttachment("tree_bits.txt").text();
bits = bits.split(";").map((s) => s.split("").map((c) => parseInt(c)));
let w = bits[0].length;
let h = bits.length;
canvas = d3.create("canvas").node();
canvas.width = w;
canvas.height = h;
let ctx = canvas.getContext("2d");
for (let i = 0; i < h; i++) {
for (let j = 0; j < w; j++) {
if (bits[i][j] == 0) {
ctx.rect(j, i, 1, 1);
}
}
}
ctx.fill();
}
return {
canvas: canvas,
cnts: box_count(canvas),
dataURL: canvas.toDataURL()
};
}