MATLAB旋转动图的绘制

MATLAB旋转动图的绘制

文章目录

利用matlab实现三维旋转动图的绘制。

1、动图效果


2、matlab代码

复制代码
close all
clear
clc


f=@(x,y,z)(x.^2+ (9./4).*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9./80).*y.^2.*z.^3;
[x,y,z]=meshgrid(linspace(-3,3));
val=f(x,y,z);
[p,v]=isosurface(x,y,z,val,0);
patch('faces',p,'vertices',v,'facevertexcdata',jet(size(v,1)),'facecolor','w','edgecolor','flat');
view(3);grid on;axis equal;


% f=@(x,y,z)(x.^2+ (9./4).*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9./80).*y.^2.*z.^3;
% [x,y,z]=meshgrid(linspace(-1.5,1.5));
% val=f(x,y,z);
% isosurface(x,y,z,val,0); 
% axis equal;view(3);colormap([1 0.2 0.2])
% 

spinningGIF('心形图1.gif');

% el=-45;  %设置仰角为30度。
% for az=0:1:1080  %让方位角从0变到360,绕z轴一周
%     view(az,el);
%     drawnow;
% end

% az= 345;   %设置方位角为0
% for el=0:1:360*1000   %仰角从0变到360
%     view(az,el);
%     drawnow;
% end

% spinningGIF(fname): makes a spinning GIF of the current plot and saves it
% Usage: make your 3D plot (using plot3(...) or scatter3(...) etc.) and
% then call SpinningGIF with the file name that you want
function spinningGIF(fname)
%     axis off
%     view(0,10)
    center = get(gca, 'CameraTarget');
    pos = get(gca, 'CameraPosition');
    radius = norm(center(1:2) - pos(1:2));
    angles = 0.5*pi:0.02*pi:1.5*pi;
    for ii=1:length(angles)
       angle = angles(ii);
       set(gca, 'CameraPosition', [center(1) + radius * cos(angle),...
                                   center(2) + radius * sin(angle),...
                                   pos(3)]);
       drawnow;
       frame = getframe(1);
       im = frame2im(frame);
       [imind,cm] = rgb2ind(im,256);
       if ii == 1
           imwrite(imind,cm,fname,'gif', 'Loopcount',inf);
       else
           imwrite(imind,cm,fname,'gif','WriteMode','append','DelayTime', 0.25);
       end
    end
end

t = 0:0.5:10;   % 设置间隔

filename = 'test.gif';   % 打开或者创建一个gif文件

for i = 1:length(t)
    
    % 动画效果
    c = (1+0.3*cos(2*t(i)));
    th=0: pi/50: pi;
    phi=0: pi/50:2* pi;
    [Th, Phi]=meshgrid(th, phi);
    X=0.9* abs(Th).* sin(Th).* cos(Phi)*c;
    Y=abs(Th).* sin(Th).* sin(Phi)*c;
    Z=abs(Th).* cos(Th)*c;
    surf(X,Y,Z)
    axis equal
    axis([-3 3 -3 3 -4 1]);
    shading interp
    colormap([1 0 0])
    light('Position',[2 1 2])
    
    % 写入gif过程
    CurrFrame = getframe;   % 获取像素,否则无法显示动画
    im = frame2im(CurrFrame);   % 
    [A,map] = rgb2ind(im,256);  % 将RGB图像转换为索引图像
	if i == 1
		imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0.1);  % DelayTime表示写入的时间间隔
	else
		imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.1);
    end
end
相关推荐
计算机安禾7 小时前
【c++面向对象编程】第25篇:仿函数(函数对象):重载operator()
开发语言·c++·算法
Rust语言中文社区7 小时前
【Rust日报】2026-05-14 Pyrefly v1.0 正式发布:快速的 Python 类型检查器和语言服务器
开发语言·后端·python·rust
kkeeper~8 小时前
0基础C语言积跬步之深入理解指针(4)
c语言·开发语言
周末也要写八哥8 小时前
在C++中使用预定义宏
开发语言·c++·算法
Data_Journal8 小时前
使用Python lxml轻松进行网络爬取
开发语言·php
xcLeigh8 小时前
IoTDB JDBC 完整使用教程:连接、查询、批处理与字符集配置
开发语言·数据库·qt·iotdb·查询·批处理·连接
学会870上岸华师8 小时前
C 语言程序设计——第一章课后编程题
c语言·开发语言·学习·算法
小小编程路8 小时前
新手快速学 Python 极简速成指南
开发语言·c++·python
rabbit_pro9 小时前
SpringBoot3集成Langchain4j使用Ollama
java·开发语言
计算机安禾9 小时前
【c++面向对象编程】第26篇:对象的内存模型:成员变量与成员函数的存储分离
开发语言·c++·算法