Matlab basic drawing-random snow scene drawing

The functions used by this program are all basic functions such as fill scatter plot in matlab, but the understanding of the code requires a certain degree of proficiency in the use of matrices. The snow scene drawn by this program will adjust itself within a certain range, and there will be problems every time it runs. The same surprise! !

Show results:
Insert picture description here

Insert picture description here
Complete code:

function snowCover
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

Note:

  • At present, it has not been studied how to generate the claw-shaped tree randomly. If there are ideas, the program may be improved.
  • The uneven ground in the figure is generated by a series of randomly generated points through cubic spline interpolation.
  • The moon in the picture is actually obtained by overlapping two circles of different colors, one of which has the same color as the background color.

Guess you like

Origin blog.csdn.net/slandarer/article/details/108873988