Making a 3D plot and rotating it

조회 수: 200 (최근 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 CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by