MATLAB Answers


Finding point of intersection between a line and a sphere

André C. D. さんによって質問されました 2019 年 6 月 6 日
最新アクティビティ André C. D. さんによって コメントされました 2019 年 6 月 7 日
Hello all,
I have a MATLAB code that plots a 3D sphere using:
[xs,ys,zs] = sphere(10);
surface = surf(350*zs+1000,350*ys,350*xs);
I also have a line that represents the normal between three points (labeled P0, P1, P2) on a plane, which is plotted from the middle-point between all three points:
P0 = [tz1,ty1,tx1]; P1 = [tz2,ty2,tx2]; P2 = [tz3,ty3,tx3]; %represent a triangle
Pm = mean([P0;P1;P2]); %represents the midpoint between P0, P1 and P2
normal = cross(P0-P1,P0-P2);
cn = normal + Pm;
normal_vector = plot3([Pm(1) cn(1)],[Pm(2) cn(2)],[Pm(3) cn(3)],'k--'); %normal
What I am trying to do is find the coordinates of the point of intersection between the line "normal_vector" and the sphere "surface".
This is what the plot looks like:
The points P0, P1 and P2 are shown as coloured circles and are always inside the sphere, so their normal is always showing 'outwards' through the surface of the sphere.
Thank you in advance!

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: Torsten
2019 年 6 月 6 日
編集済み: Torsten
2019 年 6 月 6 日

(x-xs)^2 + (y-ys)^2 + (z-zs)^2 = R^2
[x y z] = Pm + l*normal
(Pm(1)+l*normal(1)-xs)^2 + (Pm(2)+l*normal(2)-ys)^2 + (Pm(3)+l*normal(3)-zs)^2 = R^2
Solve for (the positive) l.

  7 件のコメント

André C. D. 2019 年 6 月 7 日
xs, ys and zs are not scalars. The sphere(10) function returns three 11x11 doubles in order to 'simulate' the surface of the sphere.
That is probably where the issue is at; however, I am not sure how to get from the xs, ys, zs matrices to ordinary coordinate vectors that represent every point in the surface.
2019 年 6 月 7 日
Ah, I thought (xs,ys,zs) is the center of the sphere.
You have to solve
sol = solve((Pm1+(l*normal1) - xc)^2 + (Pm2+(l*normal2) - yc)^2 + (Pm3+(l*normal3) - zc)^2 == R^2,l)
where (xc,yc,zc) is the center of the sphere.
André C. D. 2019 年 6 月 7 日
Ahh thank you so much, now it works perfectly!

サインイン to comment.

Translated by