该程序使用的 函数都是matlab中基础的fill scatter plot等函数,但代码的理解需要对矩阵使用有一定熟练度,该程序所绘制的雪景会在一定范围内自行调整,每次运行都会有不一样的惊喜!!
效果展示:
完整代码:
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
注:
- 目前还没研究出那种爪状的树怎样随机生成较好,若有了思路可能会对程序进行完善。
- 图中的凹凸不平的地面是由一系列随机生成点经过三次样条插值生成的。
- 图中的月亮其实为两个不同颜色的圆形重叠进行得到的,其中一个月亮颜色与背景色相同。