温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何利用Matlab绘制有趣图像

发布时间:2022-03-07 11:53:30 来源:亿速云 阅读:646 作者:小新 栏目:开发技术

这篇文章主要介绍了如何利用Matlab绘制有趣图像,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1.随机樱花树

如何利用Matlab绘制有趣图像

如何利用Matlab绘制有趣图像

function sakura
% @author:slandarer
% 随机形状二叉树樱花树绘制
hold on,axis equal
axis(0.5+[-10,50,0,50])
set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')
set(gca,'color',[0.5020    0.5020    0.5020])

length_trunk=6;
width_trunk=4;
k1=0.9;
k2=0.8;
number_branch=15;
alp=pi/10;
length_branch=k1*length_trunk;
width_branch=k2*width_trunk;
trunk=[12,0;12,length_trunk];
plot(trunk(:,1),trunk(:,2),'color',[0 0 0],'Linewidth',width_trunk)
begins=[trunk(2,:),pi/2,1];
grow=begins;
plotdata=[0 0 0 0 0 0 0 0];
plotdata(1,:)=[];
for i=1:number_branch
    control=randi(25,[length(grow(:,1)),1])>=10;
    ag=grow(:,3);
    l=length(ag);
    parta=[length_branch.*k1.^grow(:,4).*cos(ag+ones(l,1)*alp),length_branch.*k1.^grow(:,4).*sin(ag+ones(l,1)*alp),ones(l,1)*alp,ones(l,1)];
    partb=[length_branch.*k1.^grow(:,4).*cos(ag-ones(l,1)*alp),length_branch.*k1.^grow(:,4).*sin(ag-ones(l,1)*alp),-ones(l,1)*alp,ones(l,1)];
    parta2=[0.8.*length_branch.*k1.^grow(:,4).*cos(ag),0.8.*length_branch.*k1.^grow(:,4).*sin(ag),zeros(l,1),ones(l,1)];
    partb2=[0.8.*length_branch.*k1.^grow(:,4).*cos(ag),0.8.*length_branch.*k1.^grow(:,4).*sin(ag),zeros(l,1),ones(l,1)];
    parta=control.*parta+(1-control).*parta2;
    partb=control.*partb+(1-control).*partb2;
    parta=parta+grow;
    partb=partb+grow;
    congress=[parta;partb];
    grow=[grow;grow];
    judge=[grow,congress];
    judge=unique(judge,'rows');
    grow=judge(:,5:end);
    plotdata=[plotdata;judge];
end
for i=1:number_branch
    temp_w=width_branch*0.8^i;
    temp_branch=plotdata(plotdata(:,4)==i,:);
    plx=[temp_branch(:,1),temp_branch(:,5)];
    ply=[temp_branch(:,2),temp_branch(:,6)];
    plx=plx';ply=ply';
    plot(plx,ply,'color',[0 0 0]+i*[0.3020 0.3020 0.3020]./number_branch,'Linewidth',temp_w)
end

bloom_pos=plotdata(plotdata(:,8)==number_branch+1,[5,6]);
scatter(bloom_pos(:,1),bloom_pos(:,2),10,'CData',[0.8549    0.6824    0.6824])
bloom_pos=plotdata(plotdata(:,8)==number_branch,[5,6]);
scatter(bloom_pos(:,1),bloom_pos(:,2),8,'CData',[0.7451    0.5961    0.5961].*0.97)
end

2.苹果绘制

如何利用Matlab绘制有趣图像

function appleG

[u,v]=meshgrid(linspace(-pi,pi,100));

XMesh=(1+cos(v)).*cos(u)+0.085.*cos(5.*u)+(0.994.*v./pi).^100;
YMesh=(1+cos(v)).*sin(u);
ZMesh=(6.*sin(v)+2.*cos(v)-2.9.*log(1-v./pi))./4.2;

ax=gca;
ax.DataAspectRatio=[1 1 1];
hold on;grid on
surf(XMesh,YMesh,ZMesh,'FaceColor',[140,192,30]./255,'EdgeColor','none')
view(45,25)

h=light;
h.Color=[1 1 1];
h.Style='infinite';
end

3.南瓜绘制

如何利用Matlab绘制有趣图像

% 构造网格
[t,p]=meshgrid(linspace(0,2*pi,200),linspace(0,pi-.05,200));
% 二元分段函数
foutline=@(t,p)(p<=.14).*90.*cos(7.*p)+(p>.14).*(cos(20.*t)+70).*(p.*(pi-p+.1)).^.2;

% 球面坐标转化为X,Y,Z
h=cos(p).*foutline(t,p);
R=sin(p).*foutline(t,p);
R=R;
X=cos(t).*R;
Y=sin(t).*R;

% 颜色矩阵构造
CMap=ones([size(t),3]);
tMap=ones(size(t));
c1=[253,158,3]./255;
c2=[76,103,86]./255;
for i=1:3
    tMap(:,:)=c1(i);
    tMap(p<=.14)=c2(i);
    CMap(:,:,i)=tMap;
end

figure()
surf(X,Y,h,'CData',CMap,'EdgeColor',[0,0,0],'EdgeAlpha',.2)

4.一堆三角形绘制

如何利用Matlab绘制有趣图像

function tritritri
% @author:slandarer
hold on,axis equal
axis([-10,10,-10,10])
set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')
set(gca,'color','w')

for N=1:5
    drawTri([0,0],[cos(pi/2+2*N*pi/5),sin(pi/2+2*N*pi/5)].*5,...
        [cos(pi/2+pi/5+2*N*pi/5),sin(pi/2+pi/5+2*N*pi/5)].*5,10)
    drawTri([0,0],[cos(pi/2+2*N*pi/5),sin(pi/2+2*N*pi/5)].*5,...
        [cos(pi/2-pi/5+2*N*pi/5),sin(pi/2-pi/5+2*N*pi/5)].*5,10)
end
L5=5/sin(pi/2-pi/10)*sin(pi/5);
L5s=(5+L5)/sin(3*pi/10)*sin(3*pi/5);
for N=1:5
    drawPent([cos(pi/2+2*N*pi/5),sin(pi/2+2*N*pi/5)].*5,...
        [cos(pi/2+2*N*pi/5),sin(pi/2+2*N*pi/5)].*(5+L5),...
        [cos(pi/2+pi/10+2*N*pi/5),sin(pi/2+pi/10+2*N*pi/5)].*L5s,...
        [cos(pi/2+pi/5+2*N*pi/5),sin(pi/2+pi/5+2*N*pi/5)].*(5+L5),...
        [cos(pi/2+pi/5+2*N*pi/5),sin(pi/2+pi/5+2*N*pi/5)].*5,15)
    drawPent([cos(pi/2+2*N*pi/5),sin(pi/2+2*N*pi/5)].*5,...
        [cos(pi/2+2*N*pi/5),sin(pi/2+2*N*pi/5)].*(5+L5),...
        [cos(pi/2-pi/10+2*N*pi/5),sin(pi/2-pi/10+2*N*pi/5)].*L5s,...
        [cos(pi/2-pi/5+2*N*pi/5),sin(pi/2-pi/5+2*N*pi/5)].*(5+L5),...
        [cos(pi/2-pi/5+2*N*pi/5),sin(pi/2-pi/5+2*N*pi/5)].*5,15)
end

% 功能函数=================================================================
    function drawTri(A,B,C,times)
        LW=1.2;
        COLOR=[0 0 0];
        k=1/6;
        k2=0.8;
        plot([A(1);B(1)],[A(2);B(2)],'color',COLOR,'Linewidth',LW)
        plot([B(1);C(1)],[B(2);C(2)],'color',COLOR,'Linewidth',LW)
        plot([C(1);A(1)],[C(2);A(2)],'color',COLOR,'Linewidth',LW)
        for i=1:times
            b=k*(C-B)+B;plot([A(1);b(1)],[A(2);b(2)],'color',COLOR,'Linewidth',LW);B=b;
            pause(0.01)
            c=k*(A-C)+C;plot([B(1);c(1)],[B(2);c(2)],'color',COLOR,'Linewidth',LW);C=c;
            pause(0.01)
            a=k*(B-A)+A;plot([C(1);a(1)],[C(2);a(2)],'color',COLOR,'Linewidth',LW);A=a;
            pause(0.01)
            LW=LW*k2;
        end
    end
    function drawPent(A,B,C,D,E,times)
        LW=1.2;
        COLOR=[0 0 0];
        k=1/4;
        k2=0.9;
        plot([A(1);B(1)],[A(2);B(2)],'color',COLOR,'Linewidth',LW)
        plot([B(1);C(1)],[B(2);C(2)],'color',COLOR,'Linewidth',LW)
        plot([C(1);D(1)],[C(2);D(2)],'color',COLOR,'Linewidth',LW)
        plot([D(1);E(1)],[D(2);E(2)],'color',COLOR,'Linewidth',LW)
        plot([E(1);A(1)],[E(2);A(2)],'color',COLOR,'Linewidth',LW)
        for i=1:times
            b=k*(C-B)+B;plot([A(1);b(1)],[A(2);b(2)],'color',COLOR,'Linewidth',LW);B=b;
            pause(0.01)
            c=k*(D-C)+C;plot([B(1);c(1)],[B(2);c(2)],'color',COLOR,'Linewidth',LW);C=c;
            pause(0.01)
            d=k*(E-D)+D;plot([C(1);d(1)],[C(2);d(2)],'color',COLOR,'Linewidth',LW);D=d;
            pause(0.01)
            e=k*(A-E)+E;plot([D(1);e(1)],[D(2);e(2)],'color',COLOR,'Linewidth',LW);E=e;
            pause(0.01)
            a=k*(B-A)+A;plot([E(1);a(1)],[E(2);a(2)],'color',COLOR,'Linewidth',LW);A=a;
            pause(0.01)
            LW=LW*k2;
        end
    end
end

5.月饼绘制

如何利用Matlab绘制有趣图像

function moonCake
% @author:slandarer
ax=gca;
hold(ax,'on');
axis equal
ax.XLim=[-15,15];
ax.YLim=[-15,15];
CSet=[0.92 0.51 0.11;1 0.7 0.09;0.87 0.41 0.05];

for i=[1:7,9,8]
    if i==1
        tt=linspace(0,-pi/16,100);
    elseif i==9
        tt=linspace(-pi+pi/16,-pi,100);
    else
        tt=linspace(-pi/16-(i-2)*pi/8,-pi/16-(i-1)*pi/8,100);
    end
    xSet=cos(tt).*(10+abs(cos(tt.*8)));
    xMin=find(xSet==min(xSet));tt(xMin)
    xMax=find(xSet==max(xSet));
    t1=min([xMin(1),xMax(1)]);
    t2=max([xMin(1),xMax(1)]);
    xSet=cos(tt(t1:t2)).*(10+abs(cos(tt(t1:t2).*8)));
    ySet=sin(tt(t1:t2)).*(10+abs(cos(tt(t1:t2).*8)))-3;
    fill([xSet(1),xSet,xSet(end)],[ySet(1)+3,ySet,ySet(end)+3],CSet(mod(i,2)+1,:),'EdgeColor','none')
    
    
end
t=linspace(0,2*pi,640);
fill(cos(t).*(10+abs(cos(t.*8))),sin(t).*(10+abs(cos(t.*8))),CSet(1,:),'EdgeColor','none')
plot(cos(t).*(9+abs(cos(t.*8))),sin(t).*(9+abs(cos(t.*8)))-0.3,'Color',CSet(3,:),'LineWidth',6)
plot(cos(t).*8.7,sin(t).*8.7-0.3,'Color',CSet(3,:),'LineWidth',4)
plot(cos(t).*(9+abs(cos(t.*8))),sin(t).*(9+abs(cos(t.*8))),'Color',CSet(2,:),'LineWidth',6)
plot(cos(t).*8.7,sin(t).*8.7,'Color',CSet(2,:),'LineWidth',4)

plot([0 0],[-7 7]-0.3,'Color',CSet(3,:),'LineWidth',4)
plot([-7 7],[0 0]-0.3,'Color',CSet(3,:),'LineWidth',4)
plot([0 0],[-7 7],'Color',CSet(2,:),'LineWidth',4)
plot([-7 7],[0 0],'Color',CSet(2,:),'LineWidth',4)

t4=linspace(0,pi/2,100);
xSet4=[cos(t4).*6+1,1,6,cos(t4(1:end-12)).*5+1,2,4.8,cos(t4(17:end-40)).*3.9+1];
ySet4=[sin(t4).*6+1,1,1,sin(t4(1:end-12)).*5+1,2,2,sin(t4(17:end-40)).*3.9+1];

plot(xSet4,ySet4-0.3,'Color',CSet(3,:),'LineWidth',4)
plot(-xSet4,ySet4-0.3,'Color',CSet(3,:),'LineWidth',4)
plot(xSet4,-ySet4-0.3,'Color',CSet(3,:),'LineWidth',4)
plot(-xSet4,-ySet4-0.3,'Color',CSet(3,:),'LineWidth',4)
plot(xSet4,ySet4,'Color',CSet(2,:),'LineWidth',4)
plot(-xSet4,ySet4,'Color',CSet(2,:),'LineWidth',4)
plot(xSet4,-ySet4,'Color',CSet(2,:),'LineWidth',4)
plot(-xSet4,-ySet4,'Color',CSet(2,:),'LineWidth',4)
end

6.大钻石绘制

如何利用Matlab绘制有趣图像

如何利用Matlab绘制有趣图像

function diamondx2
% @author:slandarer
hold on
ax=gca;
axis equal
grid on
ax.GridLineStyle='--';
ax.LineWidth=1.2;
ax.XLim=[-2,2];
ax.YLim=[-2,2];
ax.ZLim=[-1.8,1.5];
ax.XColor=[1,1,1].*0.4;
ax.YColor=[1,1,1].*0.4;
ax.ZColor=[1,1,1].*0.4;
ax.DataAspectRatio=[1,1,1];
ax.DataAspectRatioMode='manual';
ax.CameraPosition=[-67.6287 -204.5276   82.7879];

light('Position',[5,6,5])
light('Position',[5,6,5])


faceColor=[0 71 177];
facePara={faceColor./255,'FaceAlpha',0.4,'EdgeColor',faceColor./255.*0.8,'EdgeAlpha',0.2,...
      'FaceLighting','gouraud','EdgeLighting','gouraud',...
      'SpecularStrength',0.9,'DiffuseStrength',0.8,'AmbientStrength',0.7}; % 面参数 
drawDiamond(facePara)

faceColor=[237 64 153];
facePara={faceColor./255,'FaceAlpha',0.4,'EdgeColor',faceColor./255.*0.8,'EdgeAlpha',0.2,...
      'FaceLighting','gouraud','EdgeLighting','gouraud',...
      'SpecularStrength',0.9,'DiffuseStrength',0.8,'AmbientStrength',0.7}; % 面参数 
drawDiamond(facePara) 

    function drawDiamond(facePara)
        
        thetaSet8=linspace(0,2*pi-pi/4,8)';
        thetaSet16=linspace(0,2*pi-pi/8,16)';
        
        % 绘制最上方八边形
        pntSet1=[cos(thetaSet8),sin(thetaSet8),ones(8,1)];
        fill3(pntSet1(:,1),pntSet1(:,2),pntSet1(:,3),facePara{:})
        
        % 一圈8个三角形
        pntSet2=[cos(thetaSet8+pi/8).*1.3,sin(thetaSet8+pi/8).*1.3,ones(8,1).*0.7];
        for i=1:8
            p1=i;
            p2=mod(i,8)+1;
            p3=i;
            fill3([pntSet1([p1,p2],1);pntSet2(p3,1)],...
                [pntSet1([p1,p2],2);pntSet2(p3,2)],...
                [pntSet1([p1,p2],3);pntSet2(p3,3)],facePara{:})
        end
        
        % 一圈8个四边形
        pntSet3=[cos(thetaSet8).*1.7,sin(thetaSet8).*1.7,ones(8,1).*0.3];
        for i=1:8
            p1=mod(i,8)+1;
            p2=i;
            p3=p1;
            p4=p1;
            fill3([pntSet1(p1,1);pntSet2(p2,1);pntSet3(p3,1);pntSet2(p4,1)],...
                [pntSet1(p1,2);pntSet2(p2,2);pntSet3(p3,2);pntSet2(p4,2)],...
                [pntSet1(p1,3);pntSet2(p2,3);pntSet3(p3,3);pntSet2(p4,3)],facePara{:})
        end
        
        % 一圈16个三角形
        pntSet4=[cos(thetaSet16).*1.7,sin(thetaSet16).*1.7,ones(16,1).*0.3];
        for i=1:8
            p1=i;
            p2=2*i-1;
            p3=2*i;
            fill3([pntSet2(p1,1);pntSet4([p2,p3],1)],...
                [pntSet2(p1,2);pntSet4([p2,p3],2)],...
                [pntSet2(p1,3);pntSet4([p2,p3],3)],facePara{:})
            p2=2*i;
            p3=mod(2*i,16)+1;
            fill3([pntSet2(p1,1);pntSet4([p2,p3],1)],...
                [pntSet2(p1,2);pntSet4([p2,p3],2)],...
                [pntSet2(p1,3);pntSet4([p2,p3],3)],facePara{:})
        end
        
        % 一圈16个四边角形
        pntSet5=[cos(thetaSet16).*1.7,sin(thetaSet16).*1.7,ones(16,1).*0.15];
        for i=1:16
            p1=i;p2=mod(i,16)+1;
            p3=mod(i,16)+1;p4=i;
            fill3([pntSet4(p1,1);pntSet4(p2,1);pntSet5(p3,1);pntSet5(p4,1)],...
                [pntSet4(p1,2);pntSet4(p2,2);pntSet5(p3,2);pntSet5(p4,2)],...
                [pntSet4(p1,3);pntSet4(p2,3);pntSet5(p3,3);pntSet5(p4,3)],facePara{:})
        end
        pntSet6=[cos(thetaSet8+pi/8).*0.4,sin(thetaSet8+pi/8).*0.4,ones(8,1).*(-1.15)];
        
        % 一圈16个长三角形
        for i=1:8
            p1=i;
            p2=2*i-1;
            p3=2*i;
            fill3([pntSet6(p1,1);pntSet5([p2,p3],1)],...
                [pntSet6(p1,2);pntSet5([p2,p3],2)],...
                [pntSet6(p1,3);pntSet5([p2,p3],3)],facePara{:})
            p2=2*i;
            p3=mod(2*i,16)+1;
            fill3([pntSet6(p1,1);pntSet5([p2,p3],1)],...
                [pntSet6(p1,2);pntSet5([p2,p3],2)],...
                [pntSet6(p1,3);pntSet5([p2,p3],3)],facePara{:})
        end
        
        % 一圈8个长四边形
        pntSet7=[cos(thetaSet8).*1.7,sin(thetaSet8).*1.7,ones(8,1).*0.15];
        for i=1:8
            p1=mod(i,8)+1;
            p2=i;
            p4=mod(i,8)+1;
            fill3([pntSet7(p1,1);pntSet6(p2,1);0;pntSet6(p4,1)],...
                [pntSet7(p1,2);pntSet6(p2,2);0;pntSet6(p4,2)],...
                [pntSet7(p1,3);pntSet6(p2,3);-1.5;pntSet6(p4,3)],facePara{:})
            
        end
    end
end

7.有趣曲线1

如何利用Matlab绘制有趣图像

% 鱼形曲线
hold on
axis equal
grid on
X=0:1:1023;
Y=0:1:1023;
[gridX,gridY]=meshgrid(X,Y);
FishPatternFcn=@(x,y)mod(abs(x.*sin(sqrt(x))+y.*sin(sqrt(y))).*pi./1024,1);
contour(gridX,gridY,FishPatternFcn(gridX,gridY),[0.7,0.7])

8.有趣曲线2

如何利用Matlab绘制有趣图像

% 生成一个40x40 DFT矩阵
% 并在复平面将点连接起来
plot(fft(eye(40)));
axis equal

9.有趣曲线3&mdash;&mdash;蝴蝶曲线

如何利用Matlab绘制有趣图像

x=linspace(0,2*pi,1000);
hdl=polarplot(x,sin(x)+sin(3.5.*x).^3,'LineWidth',1.5);
ax=gca;
ax.RTick=[];

10.有趣曲线4&mdash;&mdash;心形曲线

如何利用Matlab绘制有趣图像

function loveFunc
% @author:slandarer
LoveFunchdl=@(x,a)(x.^2).^(1/3)+0.9.*((3.3-x.^2).^(1/2)).*sin(a.*pi.*x);
hold on
grid on
axis([-3 3,-2 4])
x=-1.8:0.01:1.8;
text(0,3.3,'$f(x)=x^{\frac{2}{3}}+0.9(3.3-x^2)^{\frac{1}{2}}\sin(\alpha\pi x)$',...
    'FontSize',13,'HorizontalAlignment','center','Interpreter','latex');
txt2=text(-0.35,2.9,'','FontSize',13,'HorizontalAlignment','left','Interpreter','latex','tag','alphadata');
hdl=plot(x,LoveFunchdl(x,1),'color',[0.2118 0.4667 0.9961],'LineWidth',1.5);

for a=1:0.01:20
    AlphaString=['$\alpha=',num2str(a),'$'];
    Color=([1.0000 0.4902 0.6627]-[0.2118 0.4667 0.9961]).*(a/20)+[0.2118 0.4667 0.9961];
    set(txt2,'string',AlphaString)
    hdl.YData=LoveFunchdl(x,a);
    hdl.Color=Color;
    drawnow
end
end

11.有趣曲线5

如何利用Matlab绘制有趣图像

[x,y]=meshgrid(-10:.01:10,-10:.01:10);
cm=cos(cos(min(sin(x)+y,x+sin(y))))-cos(sin(max(sin(y)+x,y+sin(x))))>0;
imshow(cm)

12.会害羞的含羞草

如何利用Matlab绘制有趣图像

function mimose
% @author:slandarer

% =========================================================================
% <构造含羞草叶子轮廓曲线>
% 大体是画一个一个压扁倾斜的半圆弧拼在一起
% 然后乘以一个类似弧形曲线让每片小叶子不一样长

% 计算一个压扁倾斜的半圆弧曲线
t1=0:pi/100:pi+2*pi/100;
y=5*abs(sin(t1).^(1/2));
y(t1>pi)=-y(t1>pi);
t1=t1.*cos(pi/9)-y.*sin(pi/9);
y=t1.*sin(pi/9)+y.*cos(pi/9);

% 将多个压扁倾斜的半圆弧曲线拼接
T=[];
Y=[];
for i=1:20
    T=[T,(i-1)*(pi+2*pi/100)+t1];
    Y=[Y,y];   
end

% 乘以一个类似弧形曲线让每片小叶子不一样长
L=length(T);
t2=linspace(pi/8,pi-2*pi/5,L);
Y=Y.*sin(t2);

% 旋转并调整比例
X=T.*cos(pi)-Y.*sin(pi);
Y=T.*sin(pi)+Y.*cos(pi);
X=X-min(X);
X=X.*0.8;
Y=Y.*1.5;


% 创建figure及axes=========================================================
fig=figure('units','pixels',...
        'position',[500 100 500 500],...
        'Numbertitle','off',...
        'Color',[1 1 1],...
        'resize','off',...
        'visible','on',...
        'name','mimose',...
         'menubar','none');

ax=axes('Units','pixels',...
        'parent',fig,...  
        'Color',[0.2667    0.4039    0.2745],...
        'Position',[0 0 500 500],...
        'XLim',[0 100],...
        'YLim',[0 100],...
        'XColor',[1 1 1],...
        'YColor',[1 1 1]);
hold(ax,'on')
%plot(ax,X+20,Y+50)

% 调用后面定义的函数绘图===================================================
% 绘制树枝
drawBranch([20,0],[50,10])
drawBranch([20,0],[35,12])
drawBranch([0,0],[15,45])
drawBranch([4,0],[0,20])

% 绘制树叶
leaf.l1=drawLeaf([50,10]+1.*[cos(pi/1.7),sin(pi/1.7)],pi/1.7,X,Y,0.8,1);
leaf.l2=drawLeaf([50,10]+1.*[cos(-pi/8),sin(-pi/8)],-pi/8,X,Y,0.8,1);
leaf.l3=drawLeaf([50,10]+1.*[cos(pi/12),sin(pi/12)],pi/12,X,Y,1,1);
leaf.l4=drawLeaf([50,10]+1.*[cos(pi/3),sin(pi/3)],pi/3,X,Y,1,1);
leaf.l5=drawLeaf([0,45]+1.*[cos(pi/3),sin(pi/3)],pi/3,X,Y,1,1);
leaf.l6=drawLeaf([0,30]+1.*[cos(pi/5),sin(pi/5)],pi/5,X,Y,1,1);

% 绘制花朵
flower.fl1=drawFlower([35,12]);
flower.fl2=drawFlower([20,5]);

% 鼠标移动回调=============================================================
fps=25;
mitimer=timer('ExecutionMode', 'fixedRate', 'Period',1/fps, 'TimerFcn', @miMove);
start(mitimer)

    % 花朵树叶回缩函数
    function miMove(~,~)
        for ii=1:6
            if leaf.(['l',num2str(ii)]).ratio<1 %如果收缩率小于1则每隔一段时间+0.05,以下同理
                leaf.(['l',num2str(ii)]).ratio=leaf.(['l',num2str(ii)]).ratio+0.05;
            end
            resetH(leaf.(['l',num2str(ii)]),X,Y); % 依据收缩率重新绘制叶子
        end
        for ii=1:2
            if flower.(['fl',num2str(ii)]).ratio<1
                flower.(['fl',num2str(ii)]).ratio=flower.(['fl',num2str(ii)]).ratio+0.05;
            end
            resetL(flower.(['fl',num2str(ii)]));
        end
    end
set(gcf,'WindowButtonMotionFcn',@whilemovefcn)  

    % 鼠标移动调用的函数
    function whilemovefcn(~,~)
        % 检测鼠标位置
        xy=get(gca,'CurrentPoint');
        xw=xy(1,1);yw=xy(1,2);
        pos=[xw,yw];
        
        % 鼠标附近的叶子收缩率-0.2
        for ii=1:6
            LF=leaf.(['l',num2str(ii)]);
            if closeLeaf(LF.pos1,LF.pos2,pos)% 如果距离叶子距离足够近
                if leaf.(['l',num2str(ii)]).ratio>0.2
                    leaf.(['l',num2str(ii)]).ratio=leaf.(['l',num2str(ii)]).ratio-0.2;
                end
            end
        end
        
        % 鼠标附近的花收缩率-0.2
        for ii=1:2
            FL=flower.(['fl',num2str(ii)]);
            if closeFlower(FL.pos,pos)% 如果距离花距离足够近
                if flower.(['fl',num2str(ii)]).ratio>0.6
                    flower.(['fl',num2str(ii)]).ratio=flower.(['fl',num2str(ii)]).ratio-0.2;
                end
            end
        end
    end

    % 距离检测函数,叶子形状近乎于椭圆,当距离俩焦点和小于一定值认为足够近
    % 若足够近返回TRUE 否则返回FALSE
    function bool=closeLeaf(pos1,pos2,pos3)
        pL=pos1+(pos2-pos1).*0.1;
        pR=pos1+(pos2-pos1).*0.9;
        lTotal=norm(pL-pos3)+norm(pR-pos3);
        bool=lTotal<=norm(pos1-pos2); 
    end
    
    % 距离检测函数,花形状近乎于圆,当距离圆心小于一定值认为足够近
    % 若足够近返回TRUE 否则返回FALSE
    function bool=closeFlower(pos1,pos2)
       bool=norm(pos1-pos2)<=5;
    end

    % 依据收缩率重新计算并绘制叶子
    function resetH(lf,X,Y)
        X=X.*lf.Len;
        Y=Y.*lf.h.*lf.ratio;
        xl1=X.*cos(lf.alf)-Y.*sin(lf.alf)+lf.pos1(1);
        yl1=X.*sin(lf.alf)+Y.*cos(lf.alf)+lf.pos1(2);
        xl2=X.*cos(lf.alf)+Y.*sin(lf.alf)+lf.pos1(1);
        yl2=X.*sin(lf.alf)-Y.*cos(lf.alf)+lf.pos1(2);
        XL=[xl1,xl2(end:-1:1)];
        YL=[yl1,yl2(end:-1:1)];
        set(lf.LHdl,'XData',XL,'YData',YL);  
    end

    % 依据收缩率重新计算并绘制花朵
    function resetL(fl)
        xf=fl.x.*fl.ratio+fl.pos(1);
        yf=fl.y.*fl.ratio+fl.pos(2);
        xxf(2:2:2*length(xf))=xf;
        yyf(2:2:2*length(yf))=yf;
        xxf(1:2:2*length(xf))=fl.pos(1);
        yyf(1:2:2*length(yf))=fl.pos(2);
        %set(fl.plHdl,'XData',xxf,'YData',yyf);
        set(fl.scHdl,'XData',xf,'YData',yf);
        set(fl.plHdl,'XData',xxf,'YData',yyf); 
    end

    % 绘制花朵(圆心)
    function fl=drawFlower(pos)
        % 随机生成一些点作为花蕊并绘图
        theta=rand([1,120]).*2.*pi;
        r=rand([1,120]).*3+5;
        xf=r.*cos(theta)+pos(1);
        yf=r.*sin(theta)+pos(2);
        xxf(2:2:2*length(xf))=xf;
        yyf(2:2:2*length(yf))=yf;
        xxf(1:2:2*length(xf))=pos(1);
        yyf(1:2:2*length(yf))=pos(2);
        plHdl=plot(ax,xxf,yyf,'Color',[0.7608 0.4863 0.7216],'LineWidth',1);
        scHdl=scatter(ax,xf,yf,3,'filled','CData',[0.9569 0.9804 0.9333]);
        % 存储基础信息到实类
        fl.x=xf-pos(1);
        fl.y=yf-pos(2);
        fl.pos=pos;
        fl.scHdl=scHdl;
        fl.plHdl=plHdl;
        fl.ratio=1;
        % 计算可伸缩部分花蕊坐标,绘图,存储信息
        theta2=rand([1,80]).*2.*pi;
        r2=rand([1,80]).*1+3;
        xf2=r2.*cos(theta2)+pos(1);
        yf2=r2.*sin(theta2)+pos(2);
        xxf2=[ones([1,length(xf2)]).*pos(1);xf2];
        yyf2=[ones([1,length(yf2)]).*pos(2);yf2];
        
        plot(ax,xxf2,yyf2,'Color',[0.7804 0.6078 0.5020],'LineWidth',1)
    end

    % 绘制树枝 (端点1,端点2)
    function drawBranch(pos1,pos2)
        % 依据起始及末尾两点计算一个一头宽一头窄四边形坐标数据并绘图
        dir=(pos2-pos1)./norm(pos2-pos1);
        len=norm(pos2-pos1);
        xb=[0 1 1 0].*len;
        yb=[len.*0.02,len.*0.012,-len.*0.012,-len.*0.02];
       
        xxb=xb.*dir(1)-yb.*dir(2)+pos1(1);
        yyb=xb.*dir(2)+yb.*dir(1)+pos1(2);
        fill(ax,xxb,yyb,[0.8157 0.6431 0.6078],'EdgeColor',[0.6157 0.5529 0.4510],'LineWidth',1.5)
    end

    % 绘制叶子(起始位置,叶子方向,轮廓x数据,轮廓y数据,轮廓x方向放缩比例,轮廓y方向放缩比例)
    function lf=drawLeaf(pos,alf,X,Y,l,h)
        X=X.*l; % 放缩叶子长度
        Y=Y.*h; % 放缩叶子宽窄
        % 叶子数据旋转并把两个叶子数据并在一起
        xl1=X.*cos(alf)-Y.*sin(alf)+pos(1);
        yl1=X.*sin(alf)+Y.*cos(alf)+pos(2);
        xl2=X.*cos(alf)+Y.*sin(alf)+pos(1);
        yl2=X.*sin(alf)-Y.*cos(alf)+pos(2);
        XL=[xl1,xl2(end:-1:1)];
        YL=[yl1,yl2(end:-1:1)];
        % 绘图
        lf.LHdl=fill(XL,YL,[0.4980 0.7216 0.3176]);
        % 基础数据存到实类里
        lf.pos1=pos;
        lf.pos2=[pos(1)+l*51*cos(alf),pos(2)+l*51*sin(alf)];
        lf.alf=alf;
        lf.Len=l;
        lf.h=h;
        lf.ratio=1;     
        % 绘制一下叶子中线
        plot(ax,[pos(1),pos(1)+l*51*cos(alf)],[pos(2),pos(2)+l*51*sin(alf)],'Color',[0.6627 0.6980 0.4431],'LineWidth',3);
    end
end

13.随机雪景

如何利用Matlab绘制有趣图像

function snowCover
% @author:slandarer
MainFig=figure('units','pixels','position',[300 80 500 500],...
    'Numbertitle','off','menubar','none','resize','off',...
    'name','snow covered landscape');
axes('parent',MainFig,'position',[0 0 1 1],...
    'XLim', [0 500],...
    'YLim', [0 500],...
    'NextPlot','add',...
    'layer','bottom',...
    'Visible','on',...
    'Color',[0 59 129]./255,...
    'XTick',[], ...
    'YTick',[]);
hold on
%画雪地====================================================================
layerBEPos=[150;140;100;90];
layerColor=[146 210 245;101 183 231;0 101 181;0 59 129];
excursion=15;
interval=10;
xSep=25;
layerPos=zeros(size(layerBEPos,1),length(xSep:xSep:500));
layerPos(:,1)=layerBEPos(:,1);
for i=1:size(layerBEPos,1)
    for j=xSep:xSep:500
        listPos=round(j./xSep+1);
        tempRandi=randi([-excursion,excursion]);
        yPos=tempRandi+layerPos(i,listPos-1);
        if i>1&&yPos>=layerPos(i-1,listPos)-5
            yPos=layerPos(i-1,listPos)-interval;
        end
        yPos(yPos<0)=0;
        layerPos(i,listPos)=yPos;
    end   
end
for i=1:size(layerBEPos,1)
    XData=0:xSep:500;
    YData=layerPos(i,:);
    Yq=interp1(XData,YData,0:1:500,'spline');
    Xq=[0,0:1:500,500];
    Yq=[0 Yq 0];
    fill(Xq,Yq,layerColor(i,:)./255,'EdgeColor','none')
end
%画星星====================================================================
XRandiS=randi([50 450],[1,15]);
YRandiS=randi([300 460],[1,15]);
scatter(XRandiS,YRandiS,10,'o','filled','CData',[252 241 0]./255)
XRandiB=randi([50 450],[1,15]);
YRandiB=randi([300 460],[1,15]);
scatter(XRandiB,YRandiB,18,'o','filled','CData',[252 241 0]./255)
%画雪花====================================================================
snowXpos=[randi([0,30],[1,6]),randi([470,500],[1,6]),randi([0,500],[1,12])];
snowYpos=[randi([220,470],[1,6]),randi([220,470],[1,6]),randi([470,500],[1,12])];
snowSize=randi([25,50],[1,24]);
snowAngle=2*pi*rand([1,24]);
snowWidth=1+0.6*rand([1,24]);
for i=1:24
    drawSnow(snowXpos(i),snowYpos(i),snowSize(i),snowAngle(i),snowWidth(i))
end
%画月亮====================================================================
moonPos=[320 300]+[randi([0 50]),randi([0 80])];
moonSize=randi([28 32]);
blankPos=randi([floor(0.3*moonSize) ceil(0.5*moonSize)],[1,2]);
drawMoon(moonPos(1),moonPos(2),moonSize,moonPos(1)-blankPos(1),moonPos(2)+blankPos(2))
%相关函数==================================================================
    function drawMoon(x,y,R,bx,by)
        t=0:pi/50:2*pi;
        X=x+cos(t).*R;
        Y=y+sin(t).*R;
        BX=bx+cos(t).*R;
        BY=by+sin(t).*R;
        fill(X,Y,[255 251 219]./255,'EdgeColor','none')
        fill(BX,BY,[0 59 129]./255,'EdgeColor','none')
    end
    function drawSnow(x,y,len,angle,width)
        for theta=0:pi/3:2*pi-pi/3
            xTail=cos(theta+angle)*len+x;
            yTail=sin(theta+angle)*len+y;
            plot([x,xTail],[y,yTail],'color',[0 134 207]./255,'lineWidth',width)
            for branchRatio=[0.4 0.55 0.7]
                BX=x+cos(theta+angle)*len*branchRatio;
                BY=y+sin(theta+angle)*len*branchRatio;
                LX=BX+cos(theta+angle+pi/3).*len.*0.4;
                LY=BY+sin(theta+angle+pi/3).*len.*0.4;
                RX=BX+cos(theta+angle-pi/3).*len.*0.4;
                RY=BY+sin(theta+angle-pi/3).*len.*0.4;
                plot([BX,LX],[BY,LY],'color',[0 134 207]./255,'lineWidth',width.*0.8)
                plot([BX,RX],[BY,RY],'color',[0 134 207]./255,'lineWidth',width.*0.8)
            end
        end
    end
end

感谢你能够认真阅读完这篇文章,希望小编分享的“如何利用Matlab绘制有趣图像”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI