Making a 3D plot and rotating it

147 ビュー (過去 30 日間)
Ahmet Turan
Ahmet Turan 2022 年 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 件のコメント
Ahmet Turan
Ahmet Turan 2022 年 6 月 22 日
@Sam Chak Yes, that is because i couldn't do it that way. How can i implement this in 3D?
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')
  2 件のコメント
Sam Chak
Sam Chak 2022 年 6 月 23 日
+1 for you. 👍
Ahmet Turan
Ahmet Turan 2022 年 6 月 24 日
Thanks, i grasped the idea behind it!

サインインしてコメントする。

その他の回答 (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 件のコメント
Karim
Karim 2022 年 6 月 23 日
@Sam Chak below you can find the idea where i also added the top face...
However 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 quick 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 = 50;
numV = 50;
numR = 20;
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
view([-55 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])
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 👍👍👍.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by