加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[原创]matlab展示旋转直线形成单叶双曲面

(2012-11-25 11:04:12)
标签:

单叶双曲面

直线

旋转

形成

matlab

分类: 程序Linux电脑学习
给朋友做个展示图,展示“旋转一条直线形成单叶双曲面的过程”。gif图片如下:
http://s13/mw690/59cf67264cf4923a52b3c&690

自我感觉东西并不完美,但是过程展示已经很清楚了。

matlab代码如下:
% Function: Demonstrate the formation of Uniparted Hyperboloid
%           by rotating a line
% Author: felonwan@gmail.com
% Last Modified: 2012-12-25
close all
% coordinates
x=[-0.6,0.8];
y=[0.8,-0.6];
z=[3.0,-3.0];
[th,phi,r]=cart2sph(x,y,z);
n=120;%divided the circle into n pieces
k=24;%pause and write it to the animation every k pieces
% figure properties setting
hf=figure;
axis([-2,2,-2,2,-4,4]);
hold on
% lines
hc=plot3([0,0],[0,0],[-3.9,3.9],'b');
hl0=plot3(x,y,z,'r');
hlt0=plot3([0,x(1)],[0,y(1)],[z(1),z(1)],'r--');
hlb0=plot3([0,x(2)],[0,y(2)],[z(2),z(2)],'r--');
% gif figure setting
gifFilename='UnipartedHyperboloid.gif';
ifr=0;
for i=1:n
    % temporary 
    [xt,yt,zt]=sph2cart(th+2*pi*i/n,phi,r);
    % draw a pieces of patch (to get a surface by many pieces)
    hp=fill3([x(1),xt(1),xt(2),x(2)],[y(1),yt(1),yt(2),y(2)],...
        [z(1),zt(1),zt(2),z(2)],'g');%[1,0.667,0.333,0]
    set(hp,'LineStyle','none')
    set(hp,'FaceAlpha',0.7)
    % plot lines
    plot3([x(1),xt(1)],[y(1),yt(1)],[z(1),zt(1)],'r');
    plot3([x(2),xt(2)],[y(2),yt(2)],[z(2),zt(2)],'r');
    x=xt;y=yt;z=zt;
    if i>=2
        delete(hl)
        delete(hlt)
        delete(hlb)
    end
    hl=plot3(x,y,z,'r');
    hlt=plot3([0,x(1)],[0,y(1)],[z(1),z(1)],'r--');
    hlb=plot3([0,x(2)],[0,y(2)],[z(2),z(2)],'r--');
    if(mod(n,k)==0)
        pause(.05)
        %write the frame to gif file
        ifr=ifr+1;
        hfr=getframe(hf);
        im=frame2im(hfr);
        [I,map]=rgb2ind(im,256);
        if ifr==1
            imwrite(I,map,gifFilename,'gif','Loopcount',1,'DelayTime',0.1);
        else
            imwrite(I,map,gifFilename,'gif','WriteMode','append','DelayTime',0.1);  
        end
    end
end

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有