# Matlab-based production of moving pictures such as plane cutting and rotating surface

created at 07-30-2021 views: 14

## 1 Introduction¶

MATLAB has a convenient data visualization function to display vectors and matrices graphically, and can mark and print the graphics. High-level drawing includes 2D and 3D visualization, image processing, animation and expression drawing. It can be used for scientific calculation and engineering drawing. The new version of MATLAB has made great improvements and perfections to the entire graphics processing function, making it not only more complete in the functions that general data visualization software has (such as the drawing and processing of two-dimensional curves and three-dimensional surfaces, etc.), but also For some functions that other software does not have (such as graphics light processing, chromaticity processing, and four-dimensional data performance, etc.), MATLAB also shows excellent processing capabilities.

In this paper, the Matlab software platform is used to draw the rotating motion picture and generate the corresponding .gif picture. The specific content includes the dynamic process of the horizontal plane moving vertically along the Z axis to cut the saddle surface, the dynamic process of hyperbolic horizontal rotation and vertical rotation to form a curved surface, and the dynamic process of circular curve rotation to form a ring.

## 2. Plane cutting animation production¶

Make the dynamic process of cutting the saddle surface by moving the horizontal plane vertically along the Z axis, and mark the cutting curve (the line of intersection between the horizontal plane and the saddle surface).
Programming ideas:

1. Using the meshgrid and mesh functions, combined with the definition of the saddle surface, the saddle surface can be drawn directly.
2. The horizontal plane can also be drawn using the meshgrid and mesh functions. In the function definition formula, the value of the z coordinate can be a constant.
3. The up and down movement of the horizontal plane along the Z axis can be completed by using the for loop combined with the mesh function. In each loop, only the z coordinate value of the horizontal plane needs to be superimposed with a step value.
4. Cutting curve mark. In order to simplify the calculation, the find function is used to find the points whose z value difference with the horizontal plane is within a predefined range, and the points are marked with the plot3 function. Marked points are used to replace the tangent between the horizontal plane and the saddle surface. This method has a certain error from the theory, but the calculation is small and easy to implement, which is acceptable to the human eye.

Source code: see appendix
operation result ## 3. Revolving surface production¶

3.1 The dynamic process of hyperbola horizontal rotation and vertical rotation to form a curved surface

Programming ideas:

1. Use the cylinder function to combine the defined hyperbola to obtain the data matrix corresponding to the surface;
2. Draw the obtained data matrix step by step through the for loop to simulate the rotation process.
3. The principle of horizontal rotation and vertical rotation and the required data are the same, and the dynamic process drawing of the two situations can be completed by adjusting the transmission parameters of the mesh function appropriately.

Source code: see appendix
operation result  3.2 The dynamic process of a circular curve rotating to form a torus

Programming ideas:

1. First, realize a data matrix corresponding to a circle whose normal vector is parallel to the x-y plane and the center of the circle is (R, 0, 0) in the three-dimensional space.
2. Based on the data in 1), with the Z axis as the center and the radius of each x corresponding to the circle in 1), a data array corresponding to the ring centered at (0,0,0) is obtained.
3. Use the for loop combined with the surface function to gradually draw the data corresponding to the ring to simulate the rotation process.

Source code: see appendix
operation result ## Appendix¶

1 Cutting animation of horizontal plane and saddle surface

clc
clear
close all

pic_num=1;

x=-4:0.03:4;
y=-4:0.03:4;

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

Z=-X.^2+Y.^2;

[r,c]=size(Z);
figure
title('马鞍面与水平面相切');
L=min(min(Z));
H=max(max(Z));
time=[L:1:H  H:-1:L];
delt=0.03;
% view(-68,22);% Change the viewing angle
for i=time
Z2=zeros(r,c)+i;
alpha(0.1)
mesh(X,Y,Z)
hold on

alpha(0.1)
mesh(X,Y,Z2,'facecolor',[0.2 0.2 0])
hold on

XL=X(find(Z>i-delt&Z<=i+delt));
YL=Y(find(Z>i-delt&Z<=i+delt));
ZL=repmat(i,1,length(find(Z>i-delt&Z<=i+delt)));
plot3(XL,YL,ZL,'.','color',[1 0 0],'LineWidth',5)

hold off

F=getframe(gcf);
I=frame2im(F);
[I,map]=rgb2ind(I,256);
if pic_num == 1
imwrite(I,map,'马鞍.gif','gif', 'Loopcount',inf,'DelayTime',0.15);
else
imwrite(I,map,'马鞍.gif','gif','WriteMode','append','DelayTime',0.15);
end
pic_num = pic_num + 1;

end


2 The dynamic process of hyperbolic horizontal rotation and vertical rotation to form a curved surface


clc
clear
close all

pic_num=1;

a=4;
c=4;
z=-15:0.1:15;
x=a*sqrt(1+z.^2/c^2);
y=zeros(1,length(z));

[x1,y1,z1]=cylinder(x,51);

figure;
plot3(x,y,z,'color',[1 0 0],'LineWidth',2);
hold on
plot3(-x,y,z,'color',[1 0 0],'LineWidth',2);
title('双曲线绕Z轴旋转')
z1=30.*z1-15;
axis([-20,20,-20,20,-10,10]);
hold on
grid on
n=size(z1,2);
for i=1:n/2+1
z11=z1;
z11(:,i+1:n)=NaN;
mesh(x1,y1,z11);
mesh(-x1,-y1,z11);
drawnow
%   pause(0.1)

F=getframe(gcf);
I=frame2im(F);
[I,map]=rgb2ind(I,256);
if pic_num == 1
imwrite(I,map,'垂直.gif','gif', 'Loopcount',inf,'DelayTime',0.15);
else
imwrite(I,map,'垂直.gif','gif','WriteMode','append','DelayTime',0.15);
end
pic_num = pic_num + 1;
end

v=x';
r=z; %自变量
th=linspace(0,2*pi,100);

[R,Th]=meshgrid(r,th);
[X,Y] = pol2cart(Th,R);
Z=(v*ones(size(th)))';

pic_num=1;
figure
plot3(X(1,:),Z(1,:),Y(1,:),'color',[1 0 0],'LineWidth',2);
hold on
plot3(X(1,:),-Z(1,:),Y(1,:),'color',[1 0 0],'LineWidth',2);
title('双曲线绕水平轴旋转')
view(-68,22);% 改变观测视角
axis([-20,20,-20,20,-20,20]);
n=size(X,1);
hold on
grid on
for i=1:n/2
mesh(X(i:i+1,:),Z(i:i+1,:),Y(i:i+1,:));
mesh(X(i:i+1,:),-Z(i:i+1,:),Y(i:i+1,:));
drawnow;
pause(0.2);

F=getframe(gcf);
I=frame2im(F);
[I,map]=rgb2ind(I,256);
if pic_num == 1
imwrite(I,map,'水平.gif','gif', 'Loopcount',inf,'DelayTime',0.15);
else
imwrite(I,map,'水平.gif','gif','WriteMode','append','DelayTime',0.15);
end
pic_num = pic_num + 1;
end


3 The dynamic process of a circular curve rotating to form a torus

clc
clear
close all

pic_num = 1;
R=10; %大圆半径

aplha1=0:pi/40:2*pi;
N=length(aplha1);
x0=r*cos(aplha1)+R;
z0=r*sin(aplha1);
y0=zeros(1,length(x0));

aplha2=0:pi/40:2*pi;
M=length(aplha2);

x= zeros(M,N);
y= zeros(M,N);
z= zeros(M,N);

for i=1:N
for j=1:M
x(j,i)=cos(aplha2(1,j))*x0(1,i);
y(j,i)=sin(aplha2(1,j))*x0(1,i);
z(j,i)=z0(1,i);
end
end
figure
mesh(x,y,z)
axis equal

figure
plot3(x(1,:),y(1,:),z(1,:),'color',[1 0 0],'LineWidth',2);
hold on
axis([-20,20,-20,20,-10,10]);
n=size(x,1);
hold on
grid on
line([0 0],[0 0],[-10 10],'linewidth',3);

for i=1:n-1
mesh(x(i:i+1,:),y(i:i+1,:),z(i:i+1,:));
drawnow;
%     pause(0.2);
%     alpha(0.5)
F=getframe(gcf);
I=frame2im(F);
[I,map]=rgb2ind(I,256);
if pic_num == 1
imwrite(I,map,'圆环.gif','gif', 'Loopcount',inf,'DelayTime',0.15);
else
imwrite(I,map,'圆环.gif','gif','WriteMode','append','DelayTime',0.15);
end
pic_num = pic_num + 1;
end

created at:07-30-2021 robot18