1. 程式人生 > >MATLAB--繪圖4

MATLAB--繪圖4

x=[0 1 1 0 0];
y=[0 0 1 1 0];
t=[1 1 1 1 1];
plot(x,y)
h=patch(x,y,'b');
h.FaceAlpha=0.5;
hold on
% axis([-4 4 -4 4])
% axis equal
% M=[1 0 2;
%    0 1 3;
%    0 0 1];
% M1=power(M,1/50);
axis([-6 6 -6 6])
axis equal
M=[cos(pi) -sin(pi) 2;
   sin(pi) cos(pi)  3;
       0         0      1];
M1=M^(1/50);
for i=1:50
T=M1^i*[x;y;t];
plot(T(1,:),T(2,:))
h1=patch(T(1,:),T(2,:),'r');
h1.FaceAlpha=0.5;
hold on
% axis([-4 4 -4 4])
axis([-6 6 -6 6])
axis equal
pause(0.1)
end

%% 旋轉的五角星
clear
clc
point1=[0 1];
point2=[cos(18/180*pi),sin(18/180*pi)];
point3=[cos(54/180*pi),-sin(54/180*pi)];
point4=[-cos(54/180*pi),-sin(54/180*pi)];
point5=[-cos(18/180*pi),sin(18/180*pi)];
x=[point1(1),point3(1),point5(1),point2(1),point4(1),point1(1)];
y=[point1(2),point3(2),point5(2),point2(2),point4(2),point1(2)];
h1=plot(x,y)
hold on
axis equal
axis([-2 2 -2 2])
plot([0 0],[-1 1.5],':b','Linewidth',1)
%delete(h1)
%% 
%    for i=i:100
%        M=[cos(10*pi/100*i) 0;
%        0      1;];
%        wx=M*[x;y];
%        h=plot(wx(1,:),wx(2,:))
%        hold on
%        axis([-2 2 -2 2])
%        pause(0.1)
%        delete(h)%防止原來的圖形停留
%    end
%% 迴圈前不能刪除h1,否則就沒有圖形了
   for i=i:100
       M=[cos(10*pi/100*i) 0;
       0      1;];
       wx=M*[x;y];
       h1.XData=(wx(1,:));%屬性裡有XData,YData
       h1.YData=(wx(2,:));
       hold on
       axis([-2 2 -2 2])
       pause(0.1)
   end

%% 小石頭
t=0:0.01:0.3;
v=10;
g=-9.8;
x=v*t;
y=1/2*g*t.^2;
comet(x,y,0);%只寫y則按計算機給的座標值。若寫x,y則按實際的x的範圍繪圖,寫0後無小尾巴,【0 1】之間,越大尾巴越延後

%% comet3()
close
t=0:0.001:1;
r=8*t;
sita=2*pi*t*4;%仰角
fai=-2*pi*t*8;%方位角
[x,y,z]=sph2cart(fai,pi/2-sita,r);%在matlab中球座標的引數與數學中的座標系有些區別,sita為其餘角
comet3(x,y,z,0.2)

%% animatedline 


clear
clc
ax1=subplot(2,1,1);
axis([0 2*pi -1 1])
ax2=subplot(2,1,2);
axis([0 2*pi -1 1])
h=animatedline(ax1);
%h=animatedline(ax1,x(1:50000),y(1:50000));%先繪製前50000點
h1=animatedline(ax2);
h1.MaximumNumPoints=100;
x=linspace(0,2*pi,100000);
y=sin(x);
for i=1:100000
    %for i=50001:100000
    addpoints(h,x(i),y(i));
    drawnow limitrate
end
x1=linspace(0,2*pi,1000);
y1=cos(x1);
for i=1:1000
    addpoints(h1,x1(i),y1(i));
    drawnow 
end
[xx,yy]=getpoints(h);%得到資料
clearpoints(h1)%清除資料

%% drawnow
figure
hold on
for i=2:1000
    x(i)=i;
    y(i)=i^2;
    plot([x(i-1),x(i)],[y(i-1),y(i)])
    drawnow limitrate
end

%% 畫圖
r=3;
t=linspace(0,2*pi,200);
x=r*cos(t);
y=r*sin(t);
plot(x,y);
%% 產生線
l=8;%發生線的長度
fx=[r r];
fy=[0 l];
hold on
h=plot(fx,fy,'r','linewidth',1);
h1=plot(r,0,'o','markersize',3);
axis([-4*r 4*r -4*r 4*r])
axis equal
fx=[0;0;1];
fy=[0;1;1];
v=VideoWriter('jiankaixian,avi');
open(v)
for i=linspace(0,pi,500)
    M1=[1 0 r*cos(i);
        0 1 r*sin(i);
        0 0    1;];
    M2=[cos(i) -sin(i) 0;
        sin(i) cos(i) 0;
           0     0   1;];
       l1=i*r;
       detax=l1*sin(i);
       detay=-l1*cos(i);
    M3=[1  0 detax;
        0  1 detay;
        0  0   1;];
    fxx=M3*M1*M2*fx;
    fyy=M3*M1*M2*fy;
    axis([-4*r 4*r -4*r 4*r])
    axis equal
    plot(fxx(1),fxx(2),'o','markersize',3)
    h1.XData=fxx(1);
    h1.YData=fxx(2);
    h.XData=[fxx(1),fyy(1)];
    h.YData=[fxx(2),fyy(2)];
    M=getframe;
    M.cdata=imresize(M.cdata,[343,434]);
    pause(0.01)
    writeVideo(v,M);
end
close(v)