Making a 3D plot and rotating it

3 次查看(过去 30 天)
Ahmet Turan
Ahmet Turan2022-6-22
评论: Ahmet Turan ,2022-6-24
Hi everyone, i have a code that plots 3D according to some parametric equations and rotates that 3D plot first around y-axis then around z-axis. But my problem is i get this wiggly 3D side surfaces instead of smooth one. I think it is because for loop plots 100 times. How can i get smooth side surfaces? Also is there any efficient method to write this code?
a = 0.0201; b = 0.0924;
alpha = 0.1063; beta = 0;
u = linspace(0,pi);
v = linspace(0,2*pi);
r = linspace(25,75);
[U V R] = meshgrid(u,v,r);
Z = -a*R.*cos(U).*cos(V);
Y = b*R.*cos(U).*sin(V);
X = sqrt((R.^2)-(Z.^2)-(Y.^2));
figure
hold on
for i = 1:100
s = surf(X(:,:,i),Y(:,:,i),Z(:,:,i));
direction = [0 1 0];
rotate(s,direction,-rad2deg(alpha));
direction = [0 0 1];
rotate(s,direction,rad2deg(beta));
end
view(3)
xlabel('x');
ylabel('y');
zlabel('z');
grid on
shading interp
colorbar
hold off
  3 个评论
Sam Chak
Sam Chak 2022-6-22
My geometry is relatively weak. 😅
Are you trying to generate a closed surface? Something like the Tower of Hanoi.

请先登录,再进行评论。

采纳的回答

Karim
Karim 2022-6-23
@Sam Chak as requested a seperate answer to show the "patch" approach
As mentioned in the comments, there remains a part to combine the two patches and close them up (i.e. we need to define a bit of logic to determine the connectivity).
Below i did it quickly by creating 2 patches. However, i needs a bit of further thinking about the logic to build the grid and connect everything properly. However i hope i was able to show the idea/concept.
alpha = 0.1063;
beta = 0;
a = 0.0201;
b = 0.0924;
numU = 30;
numV = 30;
numR = 15;
Rmin = 25;
Rmax = 75;
[GridShell,FacesShell] = GetPatchDataShell(numU,numV,numR,Rmin,Rmax,a,b);
[GridTop,FacesTop] = GetPatchDataTop(numU,numV,numR,Rmin,Rmax,a,b);
% rotate the grid
GridShell = (roty(-rad2deg(alpha)) * GridShell')';
GridShell = (rotz( rad2deg( beta)) * GridShell')';
GridTop = (roty(-rad2deg(alpha)) * GridTop')';
GridTop = (rotz( rad2deg( beta)) * GridTop')';
% plot the patch
figure
hold on
patch('Faces',FacesShell,'Vertices',GridShell,'FaceColor','r','FaceAlpha',0.25)
patch('Faces',FacesTop,'Vertices',GridTop,'FaceColor','r','FaceAlpha',0.25)
hold off
grid on
title('Combined plot')
view([-65 -10])
% seperate plot to only display the "top face"
figure
patch('Faces',FacesTop,'Vertices',GridTop,'FaceColor','r','FaceAlpha',0.25)
grid on
view([-55 10])
title('Top face')
% seperate plot to only display the "outer shell"
figure
patch('Faces',FacesShell,'Vertices',GridShell,'FaceColor','r','FaceAlpha',0.25)
grid on
view([-50 30])
title('Outer shell')

更多回答(1 个)

Karim
Karim 2022-6-22
编辑:Karim 2022-6-22
To create a figure like the tower of hanoi, you can try something like the code below.
Have fun :)
edit: added the rotation
alpha = 0.1063;
beta = 0;
x0 = 0; % start at x = 0, y = 0, z = 0
y0 = 0;
z0 = 0;
R1 = 100; % starting ring radius
R2 = 20; % radius for the cross section
numRings = 5;
figure
hold on
for i = 1:numRings
[X,Y,Z] = Create_3D_ring(x0,y0,z0,R1,R2);
s = surf(X,Y,Z,'EdgeAlpha',0);
rotate(s,[0 1 0],-rad2deg(alpha));
rotate(s,[0 0 1], rad2deg(beta));
% reduce radius of the ring
R1 = R1-R2;
% increase the z step
z0 = z0 + 1.5*R2;
end
hold off
axis equal
grid on
view([25 20])
  7 个评论
Sam Chak
Sam Chak 2022-6-23
Hi @Karim, really appreciate your contributions on this topic.
You should post in New Answer so that I (and others) can vote it 👍👍👍.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by