matlab画心,画玫瑰花,表白合集

1.画心
直接在command窗口输入一下代码即可

close all
[x,y]=meshgrid(-10:0.01:10);
z=-(17*x.^2-16*y.*abs(x)+17.*y.^2);
[c,h]=contourf(z,100);
set(h,'linestyle','none')

得下图
matlab画心,画玫瑰花,表白合集_第1张图片
2.画一个跳动的心(动图)

gcp=figure;
M=moviein(100,gcp);
heartSpeed=15;
radiusRate=3;

for i = 1:100
    
    axis([-20 20 -20 20]);
    cla(gcp);
    x=[];
    y=[];
    
    for t = 0:0.01:2*pi
        r=(sin(t)*sqrt(abs(cos(t))))/(sin(t)+7/5)-2*sin(t)+2;
        r = radiusRate * r;
        x=[x,r*cos(t)*(0.5+0.5*HeartRate(i,heartSpeed))];
        y=[y,r*sin(t)*(0.5+0.5*HeartRate(i,heartSpeed))];
    end
    hold on;
    fill(x,y,'r');
    
    M(:,i)=getframe;
end
movie(M,1);

function ratio=HeartRate(i,heartSpeed)
i=mod(i,heartSpeed);
if i < heartSpeed / 2
    ratio=sin( i/heartSpeed*pi);
elseif i >= heartSpeed / 2 && i < heartSpeed * 3/4
    ratio=2*cos(i/heartSpeed*2/3*pi);
else
    ratio = 0;
end
end

得下图
matlab画心,画玫瑰花,表白合集_第2张图片
3.画玫瑰花
步骤一:先新建文件并命名为hb.m
在改文件下输入以下代码

function y=hb(x)

[m n]=size(x);

for i=1:m
    
    for j=1:n
        
        if rem(x(i,j),2*pi)>=0&&rem(x(i,j),2*pi)<0.5*pi
            
            y(i,j)=sin(2*x(i,j)-0.5*pi)+1
            
        elseif rem(x(i,j),2*pi)>=0.5*pi&&rem(x(i,j),2*pi)<1.5*pi
            
            y(i,j)=2
            
        elseif rem(x(i,j),2*pi)>=1.5*pi&&rem(x(i,j),2*pi)<2*pi
            
            y(i,j)=-cos(2*x(i,j))+1
            
        end
        
    end
    
end

end

步骤二:在command窗口输入以下代码即可(或者新建脚本也一样)

t=0:0.01*pi:2*pi

for i=1:5

r(i,:)=i-0.9:0.1:i+0.1;

end

%绘制花瓣

for i=1:5

a=r(i,:);

b=t;

[a,b]=meshgrid(a,b)

z=(log(a)+5).*hb(5*b+pi*i).*abs(sin(pi*a))

[x,y,z]=pol2cart(b,a,z)

surf(x,y,z,(40+i)*ones(size(z)))

hold on

end

%绘制花托

[xx,yy]=meshgrid(-5:0.1:5);

h=2*cos((xx.^2+yy.^2).^0.5)-0.64;

surf(xx,yy,h,27*ones(size(h)));

hold on

%绘制补色

xxx=[-0.1 0.1];

yyy=xxx;

[xxx,yyy]=meshgrid(xxx,yyy);

c=zeros(size(xxx))

for i=1:50

surf(xxx,yyy,c,i*ones(size(c)))

hold on

end

运行后如下图:
matlab画心,画玫瑰花,表白合集_第3张图片

你可能感兴趣的:(matlab画心,画玫瑰花,表白合集)