geoIntersectArc
Spherical intersection
The ● orange dot i is the intersection of the geodesic segments [A, B] and [C, D].
const i = geoIntersectArc([A, B], [C, D])
You can drag the points, and rotate the sphere.
Acknowledgment: this page is based on Jason Davies’ visual demo, both for the inspiration, and for the original algorithm.
Below, a few cases showing why this is challenging to implement:
Explanation. Let [A,B] and [C,D] be two arcs of great circles on the globe. Finding their intersection is a bit more challenging than it seems.
Leaving aside edge cases, the procedure begins by finding a candidate as one of the points where the great circles defined by the normal vectors
.
To check if i lies between A and B, we compare the direction one has to take from i to B with the direction from A to B. If it is the same direction, and similarly for all points, then the candidate is the solution.
Of course, the two great circles intersect in two places: the candidate and its antipode. If the candidate is rejected, we test the antipode (tests are the same, but with negative signs).
The algorithm also needs to validate edge cases (see #7).
code adapted from d3-geo-polygon
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);
}
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);
axb = cartesianNormalize(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;
}
sugar
function circleintersection(a, b, c, d) {
return intersect(
new intersectSegment(a, b),
new intersectSegment(c, d),
/* candidate = */ true
);
}
function intersection(a, b, c, d) {
return intersect(
new intersectSegment(a, b),
new intersectSegment(c, d),
/* candidate = */ false
);
}
geometry
draw
libs