[Math] How to calculate the intersect of two spheres in a 3d space

3dcirclesgeometryspheres

Right now, I have the equation for working out the two intersects of two circles in 2D space, however when I was studying http://paulbourke.net/geometry/circlesphere/ and reading about the 3D intersection it did not make much sense it what they are doing. I am aware that in a 3D space with two spheres intersecting there is an infinite number of possible solution along a circle.

Here is what I have so far in RBX.Lua for 2D intersection:

function calculateIntersect(p1, p2, j1, j2, el)
    local x1, y1 = p1.Position.X, p1.Position.Y
    local x2, y2 = p2.Position.X, p2.Position.Y
    local r1, r2 = j1.Size.Z, j2.Size.Z
    local DX, DY = x2 - x1, y2 - y1

    local d = sqrt(DX^2 + DY^2)

    if d > r1 + r2 then
        --Out of range
        return nil
    elseif d < abs(r2-r1) then
        --One circle is inside another
        return nil
    else
        local cd = (r1^2 - r2^2 + d^2)/(2*d)
        local halfcd = sqrt(r1^2 - cd^2)
        local cmx, cmy = x1 + (cd*DX)/d, y1 + (cd*DY)/d
        local intersect = {(cmx + (halfcd*DY)/d), (cmy - (halfcd*DX)/d)}

        return intersect, 
            cf(el.p, p1.Position) * cf(0, 0, -r1/2),
            cf(el.p, p2.Position) * cf(0, 0, -r2/2)
    end
end

Given that you have $x_1, y_1, x_2, y_2$ for the positions of the circles and $r_1, r_2$ for the radii you can do:

$$d = \sqrt{(x_2 – x_1)^2 + (y_2 – y_1)^2}$$

Where $d$ is the distance between the circles.

$$chorddistance = {\frac{r_1^2 – r_2^2 + d^2}{2d}}$$

Where $chorddistance$ is the distance from the first circles center to the chord between intersects.

$$halfchordlength = \sqrt{r_1^2 – chorddistance^2}$$
$$chordmidpointx = {\frac{x_1 + (chorddistance * (x_2 – x_1))}{d}}$$
$$chordmidpointy = {\frac{y_1 + (chorddistance * (y_2 – y_1))}{d}}$$

And therefore the intersection:

$$intersectionx = {\frac{chordmidpointx+(halfchordlength*(y_2-y_1))}{d}}$$
$$intersectiony = {\frac{chordmidpointy+(halfchordlength*(x_2-x_1))}{d}}$$

Which is what is being used to calculate the 2 dimensional intersection, however the 3 dimensional intersection is much more complex, and is what I need help with. If when you explain could show and explain your logic and reasoning behind what you are doing, that would be very helpful, and also explaining what everything means.
I would also like to refrain from using trigonometric functions such as $sin$ and $cos$ since I would like to explain it to other people using RBX.Lua.

To sum up : I would like to know a solution to solve a suitable intersection along a circle when two spheres intersect in a 3 dimensional space, and how to change it, explained in a way that would make sense.

Given that you only know their position and radii.

Thank you!

Best Answer

Because of the radial symmetry of the problem, it’s not substantially different from the two-dimensional one. Indeed, if you restrict your attention to a plane that passes through the centers of both spheres, then you have precisely that two dimensional problem of finding the intersections of two circle. By symmetry, one can see that the intersection of the two spheres lies in a plane perpendicular to the line joining their centers, therefore once you have the solutions to the restricted circle intersection problem, rotating them around the line joining the sphere centers produces the other sphere intersection points. Actually, because of the symmetry of the 2-d problem, you really only need one intersection point since the other circle intersection, if any, will generate the same circle in 3-d.

The real complications arise when you try to describe this intersection circle with equations. A single implicit three-dimensional Cartesian equation generally describes a surface, not a curve; you need two such equations to describe a curve. Using the equations of the two spheres takes you right back to step one, so you’d need to find a different pair of equations to describe this same circle. A reasonably descriptive choice might be the intersection of the plane on which the circle lies with a rectangular cylinder that’s perpendicular to that plane, but even that’s going to be rather opaque.

A more immediately understandable description is via a parametric vector equation of the form $$\mathbf c+r\mathbf u\cos t+r\mathbf v\sin t.$$ (This is where those trigonometric functions that you mention are likely coming into the picture.) Here, $\mathbf c$ is the circle’s center, $r$ its radius and $\mathbf u$ and $\mathbf v$ an orthogonal pair of orthogonal vectors that are parallel to the circle’s plane. Geometrically, this is just the parametric equation $r\cos t(1,0,0)+r\sin t(0,1,0)$ of a circle in the $x$-$y$ plane of radius $r$ centered at the origin that’s been rotated and translated into position. To tie this to the first paragraph, $\mathbf c+r \mathbf u$ is a solution to a reduced planar circle intersection problem (in the plane through the sphere centers and $\mathbf c+\mathbf u$), and the above formula describes all possible rotations of this point around the line through the sphere centers. I’m sure that one can find other parameterizations of the intersection circle, but I think this one is the most transparent.

Related Question