Published
Edited
May 7, 2019
Importers
Geometry
Also listed in…
utilities
Insert cell
md`# Geometry`
Insert cell
function intersectionLL([[xa1, ya1], [xa2, ya2]], [[xb1, yb1], [xb2, yb2]]) {
// https://observablehq.com/@timhau/geometry
// inspired by: http://www.kevlindev.com/gui/math/intersection/Intersection.js
const ua_t = (xb2 - xb1) * (ya1 - yb1) - (yb2 - yb1) * (xa1 - xb1);
const ub_t = (xa2 - xa1) * (ya1 - yb1) - (ya2 - ya1) * (xa1 - xb1);
const u_b = (yb2 - yb1) * (xa2 - xa1) - (xb2 - xb1) * (ya2 - ya1);
if (u_b === 0) return false;
const ua = ua_t / u_b;
const ub = ub_t / u_b;
if (ua < 0 || ua > 1 || ub < 0 || ub > 1) return false;
return [xa1 + ua * (xa2 - xa1), ya1 + ua * (ya2 - ya1)];
}
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