# save (e.g. gif) a rotation of a 3D geometry around to an axis

5 visualizaciones (últimos 30 días)
Alberto Acri el 18 de Jun. de 2024
Editada: Alberto Acri el 24 de Jun. de 2024
I would like to rotate the 3D geometry of a cube around two known nodes.
How can I modify the code?
% ======== CUBE
xc=1; yc=1; zc=1; % coordinated of the center
L=10; % cube size (length of an edge)
alpha=0.8; % transparency (max=1=opaque)
X = [0 0 0 0 0 1; 1 0 1 1 1 1; 1 0 1 1 1 1; 0 0 0 0 0 1];
Y = [0 0 0 0 1 0; 0 1 0 0 1 1; 0 1 1 1 1 1; 0 0 1 1 1 0];
Z = [0 0 1 0 0 0; 0 0 1 0 0 0; 1 1 1 0 1 1; 1 1 1 0 1 1];
C='blue'; % unicolor
X = L*(X-0.5) + xc;
Y = L*(Y-0.5) + yc;
Z = L*(Z-0.5) + zc;
fill3(X,Y,Z,C,'FaceAlpha',alpha); % draw cube
axis equal
% ======== NODES x ROTATION
axes_2_nodes = [-4,-4,6; -4,-4,-4];
% ======== ROTATION
set(gcf, 'Color', [1 1 1])
set(gca, 'Visible', 'off');
for i=1:5:300
view(i,33)
pause(0.15)
end
EDIT
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

Kautuk Raj el 24 de Jun. de 2024
I understand that you would like to rotate the 3D geometry of a cube around an axis defined by two known nodes and save this rotation as a GIF.
To rotate the 3D geometry of a cube around two known nodes, you need to apply a rotation transformation to the cube vertices. To save the rotating cube animation as a GIF, you can use MATLAB's built-in functionality to capture frames and write them to a GIF file. Here is the modified version of your code:
% ======== CUBE
xc=1; yc=1; zc=1; % coordinates of the center
L=10; % cube size (length of an edge)
alpha=0.8; % transparency (max=1=opaque)
X = [0 0 0 0 0 1; 1 0 1 1 1 1; 1 0 1 1 1 1; 0 0 0 0 0 1];
Y = [0 0 0 0 1 0; 0 1 0 0 1 1; 0 1 1 1 1 1; 0 0 1 1 1 0];
Z = [0 0 1 0 0 0; 0 0 1 0 0 0; 1 1 1 0 1 1; 1 1 1 0 1 1];
C='blue'; % unicolor
X = L*(X-0.5) + xc;
Y = L*(Y-0.5) + yc;
Z = L*(Z-0.5) + zc;
% ======== NODES x ROTATION
axes_2_nodes = [-4,-4,6; -4,-4,-4];
% Compute the rotation axis and angle
p1 = axes_2_nodes(1, :);
p2 = axes_2_nodes(2, :);
v = p2 - p1; % direction vector of the axis
v = v / norm(v); % normalize the vector
theta = pi/180; % rotation angle in radians (1 degree)
% Create rotation matrix
R = @(theta) [
cos(theta) + v(1)^2*(1-cos(theta)), v(1)*v(2)*(1-cos(theta)) - v(3)*sin(theta), v(1)*v(3)*(1-cos(theta)) + v(2)*sin(theta);
v(2)*v(1)*(1-cos(theta)) + v(3)*sin(theta), cos(theta) + v(2)^2*(1-cos(theta)), v(2)*v(3)*(1-cos(theta)) - v(1)*sin(theta);
v(3)*v(1)*(1-cos(theta)) - v(2)*sin(theta), v(3)*v(2)*(1-cos(theta)) + v(1)*sin(theta), cos(theta) + v(3)^2*(1-cos(theta))
];
% Initialize GIF settings
filename = 'rotating_cube.gif';
delay_time = 0.15;
frame_count = 0;
% Apply rotation
set(gcf, 'Color', [1 1 1])
set(gca, 'Visible', 'off');
hold on;
for i=1:5:300
% Rotate each vertex of the cube
for j = 1:numel(X)
point = [X(j) - p1(1); Y(j) - p1(2); Z(j) - p1(3)];
rotated_point = R(theta * i) * point;
X(j) = rotated_point(1) + p1(1);
Y(j) = rotated_point(2) + p1(2);
Z(j) = rotated_point(3) + p1(3);
end
% Redraw the cube
fill3(X,Y,Z,C,'FaceAlpha',alpha);
axis equal
view(i,33)
drawnow;
% Capture the frame
frame = getframe(gcf);
im = frame2im(frame);
[imind, cm] = rgb2ind(im, 256);
% Write to the GIF file
if frame_count == 0
imwrite(imind, cm, filename, 'gif', 'Loopcount', inf, 'DelayTime', delay_time);
else
imwrite(imind, cm, filename, 'gif', 'WriteMode', 'append', 'DelayTime', delay_time);
end
frame_count = frame_count + 1;
cla; % Clear current axes
end
hold off;
This code rotates the cube around the specified axis defined by the two nodes. The rotation is incremented in each step of the loop to create an animation effect. Adjust the theta value and the loop parameters as needed to achieve the desired rotation speed and smoothness. Further, the code captures each frame of the rotating cube and writes it to a GIF file named rotating_cube.gif. The delay_time variable controls the time delay between frames in the GIF.
I trust this response will guide you towards resolving your query.
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Alberto Acri el 24 de Jun. de 2024
Editada: Alberto Acri el 24 de Jun. de 2024
How can I adjust the rotation speed and impose it smoothly throughout the rotation? (as in the code I showed at the beginning: the rotation is not around two nodes but is kept smooth during the rotation)
For example I modified your code here:
theta_deg = 0.1;
and the cube rotates initially slowly and then faster and faster. How can I avoid this?
EDIT: I need the rotation around the red axis.

Iniciar sesión para comentar.

### Categorías

Más información sobre Bodies en Help Center y File Exchange.

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by