資源簡介
運用matlab寫出祝福某人圣誕快樂。
代碼片段和文件信息
function?christmas
%?Anselm?Ivanovas?anselm.ivanovas@student.unisg.ch
%%?setup
snow=450;????%?number?of?snow?flakes?[0?..?5000]
%%?draw?tree
h=0:0.2:25;?%vertical?grid
[XYZ]?=?cylinder(tree(h));?%produce?a?tree?formed?cylinder
Z=Z*25;?%scale?to?the?right?heigth
treeDiffusion=rand(12621)-0.5;%some?horizontal?diffusion?data
for?cnt1=1:21
???
????for?cnt2=16:126%starting?above?the?trunk
???????%get?the?angle?to?always?diffuse?in?direction?of?the?radius
???????angle=atan(Y(cnt2cnt1)/X(cnt2cnt1));
???????%split?the?diffusion?in?the?two?coordinates?depending?on?the?angle
???????X(cnt2cnt1)=X(cnt2cnt1)+cos(angle)*treeDiffusion(cnt2cnt1);
???????Y(cnt2cnt1)=Y(cnt2cnt1)+sin(angle)*treeDiffusion(cnt2cnt1);
???????%some?Vertical?diffusion?for?each?point
???????Z(cnt2cnt1)=Z(cnt2cnt1)+(rand-0.5)*0.5;
????end
???
end
%draw?the?tree
h0??=?figure(‘Units‘‘inches‘);
pos?=?h0.Position;
pos(1)?=?1;?pos(2)?=?1;
pos(3)?=?7;?pos(4)?=?7;
h0.Position?=?pos;
surfl(XYZ‘light‘)
%Use?as?nice?green?color?map?(darker?at?the?bottom?lighter?at?the?top)
r=(0.0430:(0.2061/50):0.2491)‘;%red?component
g=(0.2969:(0.4012/50):0.6981)‘;%green?component
b=(0.0625:(0.2696/50):0.3321)‘;%blue?component
map=[rgb];%join?in?a?map
for?cnt=1:6
????%change?the?lower?part?to?brown?for?the?trunk
????map(cnt:)=[77635]/265;
end
colormap(map)%set?the?map
view([-37.54])%Change?the?view?to?see?a?little?more?of?the?Actual?3D?tree
lighting?phong?%some?nice?lighting
shading?interp?%remove?grid?and?smoothen?the?surface?color
axis?equal?%takes?care?of?display?in?the?right?proportion
axis([-10?10?-10?10?0?30])?%give?some?more?axis?space?(for?the?snow?later)
axis?off?%but?don‘t?show?axis
hold?on?%to?draw?the?rest
title(‘Merry?Christmas?劉成祥‘‘color‘‘w‘...
????‘fontsize‘25...
????‘fontweight‘‘Bold‘)
set(gcf‘color‘[22?32?51]./255)
%%?Presents
%Draw?some?presents?around?the?tree?(each?with?random?color)
drawPresent(2-40332);
drawPresent(-430231.5);
drawPresent(530433);
drawPresent(-14-50631);
drawPresent(-9-100222);
drawPresent(040433);
drawPresent(-6-130333);
%%?Snow
%create?some?random?3D?coordinates?for?the?snow?(amount?as?in?setup?above)
snowX=(rand(snow1)*25-12.5);
snowY=(rand(snow1)*25-12.5);
snowZ=(rand(snow1)*27);
color0?=?jet(length(snowX));
%Note:Some?flakes?will?end?up?IN?the?tree?but?just?can‘t?be?seen?then
for?ii?=?1:length(snowX)
????plot3(snowX(ii)snowY(ii)snowZ(ii)‘*‘‘col
評論
共有 條評論