Published
Edited
Dec 19, 2019
Insert cell
md`# Barnsley Fern: II `
Insert cell
paper = require("paper");
Insert cell
simplex = {
const f1 = new Affine2D([[0., 0.], [0., 0.16]],[0., 0.]);
const f2 = new Affine2D([[0.85, 0.04], [0.1, 0.95]],[0., 1.5]);
const f3 = new Affine2D([[-0.2, -0.23], [0.23, 0.22]],[0., 1.6]);
const f4 = new Affine2D([[-0.15, 0.28], [0.26, 0.24]],[0., 0.44]);
const simplex = new Simplex(
[0.01, 0.85, 0.07, 0.07],
[f1, f2, f3, f4],
)
return simplex;
}
Insert cell
{
// Drawing technical details.
let canvas = this || DOM.canvas(500, 650);
let ps = canvas.ps || (() => {
let ps = new paper.PaperScope();
ps.setup(canvas);
return ps;
})();
canvas.ps = ps;
const iterations = 15000;
const scale = 20;
let start = new ps.Point(250, 625);
let i = 0;
let turtle = [0., 0.];
while (i++ < iterations) {
turtle = simplex.transformation.forward(turtle)
let point = new paper.Point(
start.x + turtle[0] * scale, start.y - turtle[1] * scale);
let circle = new ps.Path.Circle(point, 0.05);
circle.strokeColor = "black";
};
return canvas;
}
Insert cell
Insert cell
class Simplex {
constructor(probs, events) {
this.probs = probs;
this.events = events;
}
get transformation() {
let r = Math.random();
let total = 0;
for (let i in this.probs) {
total += this.probs[i];
if (r <= total) {
return this.events[i];
}
}
throw Error("Impossible!");
}
}
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