Public
Edited
Nov 3
14 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function intersectSegment(from, to) {
this.from = from;
this.to = to;
this.normal = cartesianCross(from, to);
this.fromNormal = cartesianCross(this.normal, from);
this.toNormal = cartesianCross(this.normal, to);
}
Insert cell
function intersect(a, b, candidate) {
if (cartesianEqual(a.from, b.from) || cartesianEqual(a.from, b.to))
return a.from;
if (cartesianEqual(a.to, b.from) || cartesianEqual(a.to, b.to)) return a.to;

var axb = cartesianCross(a.normal, b.normal),
norm2 = cartesianDot(axb, axb);
cartesianNormalizeInPlace(axb);

if (candidate) return axb;

// if A=B or are antipodals, or C=D or antipodals
// the solution is undefined.
// If ABCD are on the same circle, same answer for the moment (todo?)
if (norm2 < 1e-30) return undefined;

var a0 = cartesianDot(axb, a.fromNormal),
a1 = cartesianDot(axb, a.toNormal),
b0 = cartesianDot(axb, b.fromNormal),
b1 = cartesianDot(axb, b.toNormal);

// check if the candidate lies on both segments
// or is almost equal to one of the four points
if (
(a0 > 0 && a1 < 0 && b0 > 0 && b1 < 0) ||
(a0 > 0 && cartesianEqual(axb, a.from)) ||
(a1 < 0 && cartesianEqual(axb, a.to)) ||
(b0 > 0 && cartesianEqual(axb, b.from)) ||
(b1 < 0 && cartesianEqual(axb, b.to))
)
return axb;

// same test for the antipode
axb[0] = -axb[0];
axb[1] = -axb[1];
axb[2] = -axb[2];
a0 = -a0;
a1 = -a1;
b0 = -b0;
b1 = -b1;

if (
(a0 > 0 && a1 < 0 && b0 > 0 && b1 < 0) ||
(a0 > 0 && cartesianEqual(axb, a.from)) ||
(a1 < 0 && cartesianEqual(axb, a.to)) ||
(b0 > 0 && cartesianEqual(axb, b.from)) ||
(b1 < 0 && cartesianEqual(axb, b.to))
)
return axb;

return false;
}
Insert cell
Insert cell
function circleintersection(a, b, c, d) {
return intersect(
new intersectSegment(a, b),
new intersectSegment(c, d),
/* candidate = */ true
);
}
Insert cell
function intersection(a, b, c, d) {
return intersect(
new intersectSegment(a, b),
new intersectSegment(c, d),
/* candidate = */ false
);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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