Published
Edited
Sep 26, 2019
7 stars
Insert cell
Insert cell
Insert cell
{
const context = DOM.context2d(width, width - width / 5);
context.textAlign = "center";

// number line
const offset = 20;
const start = [offset, width / 2 + 100];
const end = [width - offset, width / 2 + 100];
line(start, end, context);

const connected = {};
for (let i = 0; i < points.length; ++i) {
connected[i] = [];
}

for (let i = 0; i < points.length; ++i) {
const [nom, denom] = points[i];
const val = nom / denom;
const p = pointOnLine([start, end], val);
circle([...p, 2], context);

if (n < 20) {
if (val === 0 || val === 1) {
context.fillText(nom, p[0], p[1] + 15);
} else {
context.fillText(nom, p[0], p[1] + 15);
context.fillText("-", p[0], p[1] + 22);
context.fillText(denom, p[0], p[1] + 28);
}
}

for (let j = 0; j < points.length; ++j) {
const [otherNom, otherDenom] = points[j];
const otherVal = otherNom / otherDenom;
const criteria = otherNom * denom - otherDenom * nom;

if (Math.abs(criteria) === 1 && !connected[j].includes(i)) {
const other = pointOnLine([start, end], otherVal);
const half = (otherVal + nom / denom) / 2;
const halfPoint = pointOnLine([start, end], half);
context.beginPath();
context.arc(...halfPoint, dist2D(p, other) / 2, 0, Math.PI, 1);
context.stroke();
connected[i].push(j);
}
}
}

return context.canvas;
}
Insert cell
points = {
const res = [];

for (let denom = 1; denom <= n; ++denom) {
for (let nom = 0; nom <= denom; ++nom) {
const value = nom / denom;
const found = res.filter(([n, d]) => n / d === value);
if (found.length === 0) {
res.push([nom, denom]);
}
}
}

return res;
}
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more