Published
Edited
May 22, 2022
Importers
2 stars
Insert cell
Insert cell
angleOfInclination = (x1, y1, x2, y2) => Math.atan2(y2 - y1, x2 - x1)
Insert cell
divideLine = (x1, y1, x2, y2, ratio) => [
x1 + ratio * (x2 - x1),
y1 + ratio * (y2 - y1)
]
Insert cell
partitionLine = (x1, y1, x2, y2, parts) =>
Array.from({ length: parts + 1 })
.map((_, i) => divideLine(x1, y1, x2, y2, i / parts))
.reduce((acc, v, i, src) => {
if (i === 0) return acc;

return [...acc, [src[i - 1], v]];
}, [])
Insert cell
rotatePoint = (x, y, angle, xc = 0, yc = 0) => {
const xO = x - xc;
const yO = y - yc;

const xOR = xO * Math.cos(angle) - yO * Math.sin(angle);
const yOR = xO * Math.sin(angle) + yO * Math.cos(angle);

return [xOR + xc, yOR + yc];
}
Insert cell
distance = (x1, y1, x2, y2) =>
Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
Insert cell
function movePointAtAngle(x, y, angle, distance) {
return [x + Math.sin(angle) * distance, y - Math.cos(angle) * distance];
}
Insert cell
midpointOfALine = (x1, y1, x2, y2) => [(x1 + x2) / 2, (y1 + y2) / 2]
Insert cell
slope = (x1, y1, x2, y2) => (y2 - y1) / (x2 - x1)
Insert cell
distanceFromPointToLine = (point, line) => {
const [x0, y0] = point;
const [[x1, y1], [x2, y2]] = line;

return (
Math.abs((y1 - y2) * x0 + (x2 - x1) * y0 + x1 * y2 - x2 * y1) /
Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
);
}
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