How to find all the intersecting points between 4 spheres ?

20 次查看(过去 30 天)
I am looking to find the intersecting points between 4 spheres. I am planning to do it in a simple and efficient way. I am thinking to use analytical equations of the sphere and subracting them. I am not sure How will I find the x,y,z terms of the equation.
Given conditions are - Center positions of all spheres, constant radius of spheres = 1 m
I have written a part of the code and partial approach, I have seen internet and got confused with approaches of vectors and optimisation for this problem. I would like to know your views on solving this problem
%Spheres
S1 = [1;1;1]
S2 = [2;1;1]
S3 = [1.5; 0;1]
S4 = [1.5; 2;1]
r = 1;
% Sphere equations
(x-S1(1))^2 + (y-S1(2))^2 + (z-S1(3))^2 = r^2;
(x-S2(1))^2 + (y-S2(2))^2 + (z-S2(3))^2 = r^2;
(x-S3(1))^2 + (y-S3(2))^2 + (z-S3(3))^2 = r^2;
(x-S4(1))^2 + (y-S4(2))^2 + (z-S4(3))^2 = r^2;
% find x,y,z - subtract the equations to find x,y,z and maybe use symbolic
% toolbox

回答(3 个)

John D'Errico
John D'Errico 2024-1-26
编辑:John D'Errico 2024-1-26
There will generally be no common point of intersection between all 4 spheres, so where all the spheres intersect. Of course, you may get lucky. And, yes, you can just throw a tool like solve at it. Sometimes solve will work, or not. It won't really tell you why it failed, or how it solved the problem. It looks very much like you want to understand why and how to solve the problem, and I think that is what your question really comes down to, one of understanding.
%Spheres
S1 = [1;1;1];
S2 = [2;1;1];
S3 = [1.5; 0;1];
S4 = [1.5; 2;1];
r = 1;
% Sphere equations
syms x y z % These are the unknowns
EQ(1) = (x-S1(1))^2 + (y-S1(2))^2 + (z-S1(3))^2 == r^2;
EQ(2) = (x-S2(1))^2 + (y-S2(2))^2 + (z-S2(3))^2 == r^2;
EQ(3) = (x-S3(1))^2 + (y-S3(2))^2 + (z-S3(3))^2 == r^2;
EQ(4) = (x-S4(1))^2 + (y-S4(2))^2 + (z-S4(3))^2 == r^2;
The common trick is to subtract a pair of equations. What does that do? You will see in general that the squared terms in all of the variables disappear. And what does that mean? Imagine two spheres in some configuration where the two spheres intersect. Always, unless one sphere is inside the other, or they touch at a single point, the result will be a nice perfect circle, that lies in some plane.
TRY IT!
EQ12 = simplify(expand(EQ(1) - EQ(2)))
EQ12 = 
So we learn that spheres 1 and 2 intersect in the plane x = 3/2, with y and z extending parallel to the y and z axes. I'll draw those two spheres, so we can see what happens.
[x1,y1,z1] = sphere(1000);
H1 = surf(x1+S1(1),y1+S1(2),z1+S1(3));
H1.EdgeColor = 'none';
H1.FaceColor = 'r';
H1.FaceAlpha = 0.3;
hold on
% each sphere has the same unit radius with a different center,
% so I can just reuse x1,y1,z1
H2 = surf(x1+S2(1),y1+S2(2),z1+S2(3));
H2.EdgeColor = 'none';
H2.FaceColor = 'g';
H2.FaceAlpha = 0.3;
hold off
axis equal
So those first two spheres intersect along a plane, with x==3/2. As I said, the intersection is a circle, and that circle lies parallel to the y and z axes.
Do you see how this works? If not, go back and think about what I did before you go any further.
Next, we can look at other pairs of spheres. How do they interact?
EQ13 = simplify(expand(EQ(1) - EQ(3)))
EQ13 = 
Similarly, EQ13 defines the plane of the circle where the corresponding spheres intersect. This plane lies at an angle to the axes, but that is not important.
EQ14 = simplify(expand(EQ(1) - EQ(4)))
EQ14 = 
EQ23 = simplify(expand(EQ(2) - EQ(3)))
EQ23 = 
HOWEVER, these last two resulting equations imply a problem, as those two planes are parallel, but not coincident. They never intersect. And that proves there can be no common point of intersection for all 4 spheres.
As I originally suggested, that will usually be the case. At best, you might decide to find the point that best satisfies all of the equations. But there is NO common solution in this case. We just proved that. How might we automate this process? A simple approach might be to reduce all pairs of sphere intersections into linear equations.
k = 0;
for i = 1:4
for j = i+1:4
k = k + 1;
eqpairs(k) = simplify(expand(EQ(i) - EQ(j)));
end
end
[A,B] = equationsToMatrix(eqpairs,[x,y,z])
A = 
B = 
We now have a system of 6 linear equations, in three unknowns, of the form
A*[x;y;z]==B
Any solution that exists must satyisfy ALL 6 of those linear equations. Does an exact solution exist? That will be true ONLY is the vector B can be wrtten as a linear combination of the columns of A. We can learn that from rank.
rank(A)
ans = 2
rank([A,B])
ans = 3
So the matrix A has rank 2. But when as append the vector B to A, and again test the rank, the result has rank 3. Again, this proves no exact solution can exist.
Finally, I'll plot all 4 spheres, viewing from the top down, so we can visualize what happened, and why no solution existed.
H1 = surf(x1+S1(1),y1+S1(2),z1+S1(3));
H1.EdgeColor = 'none';
H1.FaceColor = 'r';
H1.FaceAlpha = 0.3;
hold on
H2 = surf(x1+S2(1),y1+S2(2),z1+S2(3));
H2.EdgeColor = 'none';
H2.FaceColor = 'g';
H2.FaceAlpha = 0.3;
H3 = surf(x1+S3(1),y1+S3(2),z1+S3(3));
H3.EdgeColor = 'none';
H3.FaceColor = 'b';
H3.FaceAlpha = 0.3;
H4 = surf(x1+S4(1),y1+S4(2),z1+S4(3));
H4.EdgeColor = 'none';
H4.FaceColor = 'y';
H4.FaceAlpha = 0.3;
hold off
axis equal
view(-90,90)
Do you see the yellow and blue spheres intersect at one point? They touch, just a brief kiss. But the red and green spheres intersect at a circle, that completely surrounds the intersection point of the yellow and blue spheres. So graphically, you can visualize that no solution exists.
Sorry.

Hassaan
Hassaan 2024-1-26
编辑:Hassaan 2024-1-26
syms x y z;
% Sphere centers
S1 = [1; 1; 1];
S2 = [2; 1; 1];
S3 = [1.5; 0; 1];
S4 = [1.5; 2; 1];
% Radius
r = 1;
% Sphere equations
eq1 = (x - S1(1))^2 + (y - S1(2))^2 + (z - S1(3))^2 == r^2;
eq2 = (x - S2(1))^2 + (y - S2(2))^2 + (z - S2(3))^2 == r^2;
eq3 = (x - S3(1))^2 + (y - S3(2))^2 + (z - S3(3))^2 == r^2;
eq4 = (x - S4(1))^2 + (y - S4(2))^2 + (z - S4(3))^2 == r^2;
% Solve the system (using three of the equations)
[solx, soly, solz] = solve([eq1, eq2, eq3], [x, y, z]);
disp([solx, soly, solz])
% Display the solutions
if ~isempty(solx)
for i = 1:length(solx)
fprintf('Solution %d: x = %f, y = %f, z = %f\n', i, double(solx(i)), double(soly(i)), double(solz(i)));
% Verify if the solution satisfies the fourth equation
isOnFourthSphere = subs(eq4, [x, y, z], [solx(i), soly(i), solz(i)]);
fprintf('Does solution %d satisfy the fourth sphere equation? %s\n', i, char(isOnFourthSphere));
end
else
fprintf('No real intersection points found.\n');
end
Solution 1: x = 1.500000, y = 0.625000, z = 0.219375
Does solution 1 satisfy the fourth sphere equation? 5/2 == 1
Solution 2: x = 1.500000, y = 0.625000, z = 1.780625
Does solution 2 satisfy the fourth sphere equation? 5/2 == 1
  • The solution might be complex, indicating that there is no real intersection.
  • If you have real solutions, you need to check if they satisfy all four equations. This is because using three equations might give you intersection points of only those three spheres.
  • The approach can become more complex if the spheres have different radii.
For more complex configurations or if precision is critical, numerical methods or optimization techniques might be more suitable. These methods can handle cases where an analytical solution is too complex or not feasible.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Aquatris
Aquatris 2024-1-26
编辑:Aquatris 2024-1-26
You can use symbolic toolbox for this. However I dont think your 4 spheres intersect.
S1 = [1;1;1];
S2 = [2;1;1];
S3 = [1.5; 0;1];
S4 = [1.5; 2;1];
% S2 = S1
r = 1 ;
syms x y z real
% Sphere equations
f(1) = (x-S1(1))^2 + (y-S1(2))^2 + (z-S1(3))^2 == r^2;
f(2) = (x-S2(1))^2 + (y-S2(2))^2 + (z-S2(3))^2 == r^2;
f(3) = (x-S3(1))^2 + (y-S3(2))^2 + (z-S3(3))^2 == r^2;
f(4) = (x-S4(1))^2 + (y-S4(2))^2 + (z-S4(3))^2 == r^2;
sol1 = solve(f,[x y z],'ReturnCondition',true); % all 4 sphere intersection
[sol1.x sol1.y sol1.z]
ans = Empty sym: 0-by-3
sol2 = solve(f(:,1:3),[x y z],'ReturnCondition',true); % first 3 sphere intersection
[sol2.x sol2.y sol2.z]
ans = 
sol3 = solve(f(:,1:2),[x y z],'ReturnCondition',true); % first 2 sphere intersection
[sol3.x sol3.y sol3.z]
ans = 
sol3.conditions
ans = 

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by