Matlab - subtraction of two three-dimensional figures with the function of surfing and mines.

I am trying to make an adventure plot that looks like this:

enter image description here

So far, I:

x = [-1:1/100:1];
y = [-1:1/100:1];

[X,Y] = meshgrid(x,y);

Triangle1 = -abs(X) + 1.5;
Triangle2 = -abs(Y) + 1.5;

Z = min(Triangle1, Triangle2);

surf(X,Y,Z);
shading flat
colormap winter;
hold on;

[X,Y,Z] = sphere();
Sphere = surf(X, Y, Z + 1.5 );% sphere with radius 1 centred at (0,0,1.5)
hold off;

This code creates a graph that looks like this:

enter image description here

  • There is a pyramid with a square base ([-1,1] x [-1,1]) and a vertex at a height c = 1,5 above the beginning (0,0).

  • The top of the pyramid is hollowed out, removing part of it falling into a sphere of radius r = 1, centered at the top.

Therefore, I need to save part of the surface of the sphere that is inside the pyramid, and delete the rest. Note that the y axis on each graph is different, so the second graph is slightly condensed. Yes, there is a pyramid falling into a sphere that is difficult to understand from this angle.

70 () 35 (). , ( ). AXIS TIGHT, .

+4
2

:

N = 400; % resolution
x = linspace(-1,1,N);
y = linspace(-1,1,N);
[X,Y] = meshgrid(x,y);
Triangle1 = -abs(X)+1.5 ;
Triangle2 = -abs(Y)+1.5 ;
Z = min(Triangle1, Triangle2);
Trig = alphaShape(X(:),Y(:),Z(:),2);
[Xs,Ys,Zs] = sphere(N-1);
Sphere = alphaShape(Xs(:),Ys(:),Zs(:)+2,2);
% get all the points from the pyramid that are within the sphere:
inSphere = inShape(Sphere,X(:),Y(:),Z(:));
Zt = Z;
Zt(inSphere) = nan; % remove the points in the sphere
surf(X,Y,Zt)
shading interp
view(70,35)
axis tight

alphaShape :

enter image description here

, , , . , :

hold on;
Zc = Zs;
inTrig = inShape(Trig,Xs(:),Ys(:),Zs(:)+1.5);
Zc(~inTrig) = nan;
surf(Xs,Ys,Zc+1.5)
hold off

, .

, , .

+1

EBH.

3d MATLAB. , r (x0,y0,z0)

r^2 = (x-x0)^2 + (y-y0)^2 + (z-z0)^2

z z = z0 +/- sqrt(r^2-(x-x0)^2-(y-y0)^2), + , - . . , z .

, r^2-(x-x0)^2-(y-y0)^2 >= 0. , .

N = 400; % resolution
z0 = 1.5;  % sphere z offset
r = 1;     % sphere radius
x = linspace(-1,1,N);
y = linspace(-1,1,N);
[X,Y] = meshgrid(x,y);
% pyramid
Triangle1 = -abs(X)+1.5 ;
Triangle2 = -abs(Y)+1.5 ;
Pyramid = min(Triangle1, Triangle2);    
% half-sphere (hemisphere)
sqrt_term = r^2 - X.^2 - Y.^2;
HalfSphere = -sqrt(sqrt_term) + z0;
HalfSphere(sqrt_term < 0) = inf;
Z = min(HalfSphere, Pyramid);
surf(X,Y,Z)
shading interp
view(70,35)
axis tight

enter image description here

+1

All Articles