Published
Edited
Oct 8, 2019
Insert cell
Insert cell
Insert cell
Insert cell
{
rearrange;
const context = DOM.context2d(width, width);
context.translate(width / 2, width / 2);

drawEllipse(a, b, context);
const points = [
pointOnEllipse(-Math.PI * (basicConfig ? 3 / 4 : randRange(3 / 4, 1))),
pointOnEllipse(Math.PI * (basicConfig ? 1 / 2 : randRange(1 / 4, 3 / 4))),
pointOnEllipse(-Math.PI * (basicConfig ? 1 / 4 : randRange(0, 1 / 4))),
pointOnEllipse(Math.PI * (basicConfig ? 3 / 4 : randRange(3 / 4, 1))),
pointOnEllipse(-Math.PI * (basicConfig ? 1 / 2 : randRange(1 / 4, 3 / 4))),
pointOnEllipse(Math.PI * (basicConfig ? 1 / 4 : randRange(0, 1 / 4)))
];

const lines = [];
for (let i = 0; i < points.length; ++i) {
circle([...points[i], 3], context);
context.fillText("P" + (i + 1), points[i][0] + 10, points[i][1] + 10);

const con = points[(i + 1) % points.length];
const l = line(points[i], con, context);
lines.push(l);
}

const intersections = [];
for (let i = 0; i < 3; ++i) {
const intersection = intersectionLL(lines[i], lines[i + 3]);
circle([...intersection, 3], context, { color: "red" });
intersections.push(intersection);
}

line(intersections[0], intersections[1], context, { color: "red" });

return context.canvas;
}
Insert cell
Insert cell
Insert cell
function randRange(min, max) {
return Math.random() * (max - min) + min;
}
Insert cell
function pointOnEllipse(angle) {
// angle is in radian
const sign = -Math.PI / 2 <= angle && angle <= Math.PI / 2 ? 1 : -1;
const x =
sign *
((a * b) / Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle)));
const y = x * Math.tan(angle);
return [x, y];
}
Insert cell
function drawEllipse(a, b, context) {
const points = [];
for (let i = 0; i < 2 * Math.PI; i += 0.01) {
points.push([a * Math.cos(i), b * Math.sin(i)]);
}

context.beginPath();
context.moveTo(...points[0]);
for (let point of points.slice(1)) {
context.lineTo(...point);
}
context.stroke();
}
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