如何用數學軟體畫一個「聖誕樹」?
可以是字元的,也可以是圖形的
相關問題如何用C語言畫一個「聖誕樹」? - 編程
我只是搬運http://codegolf.stackexchange.com上的答案,原作者是Silvia (@Silvia ),用的是Mathematica。
PD = .5;
s[t_, f_] := t^.6 - f
dt[cl_, ps_, sg_, hf_, dp_, f_, flag_] :=
Module[{sv, basePt},
{PointSize[ps],
sv = s[t, f];
Hue[cl (1 + Sin[.02 t])/2, 1, .3 + sg .3 Sin[hf sv]],
basePt = {-sg s[t, f] Sin[sv], -sg s[t, f] Cos[sv], dp + sv};
Point[basePt],
If[flag,
{Hue[cl (1 + Sin[.1 t])/2, 1, .6 + sg .4 Sin[hf sv]], PointSize[RandomReal[.01]],
Point[basePt + 1/2 RotationTransform[20 sv, {-Cos[sv], Sin[sv], 0}][{Sin[sv], Cos[sv], 0}]]},
{}]
}]
frames = ParallelTable[
Graphics3D[Table[{
dt[1, .01, -1, 1, 0, f, True], dt[.45, .01, 1, 1, 0, f, True],
dt[1, .005, -1, 4, .2, f, False], dt[.45, .005, 1, 4, .2, f, False]},
{t, 0, 200, PD}],
ViewPoint -&> Left, BoxRatios -&> {1, 1, 1.3},
ViewVertical -&> {0, 0, -1},
ViewCenter -&> {{0.5, 0.5, 0.5}, {0.5, 0.55}}, Boxed -&> False,
PlotRange -&> {{-20, 20}, {-20, 20}, {0, 20}}, Background -&> Black],
{f, 0, 1, .01}];
Export["tree.gif", frames]
Wolfram Mathematica 算數學軟體吧?
還有這個:算不算抖機靈 = =
Mathematica版本:打開一個notebook,然後長按CTRL+/,效果如下:像不像一棵聖誕樹呢?
這裡有用SAS畫的聖誕樹,翻譯成了Mathematica版的
Clear["`*"];
ifs[prob_,A_,init_,max_]:=FoldList[#2.{#[[1]],#[[2]],1},init,RandomChoice[prob-&>A,max]];
L={{{0.03,0},{0,0.1}},{{0.85,0},{0,0.85}},{{0.8,0},{0,0.8}},{{0.2,-0.08},{0.15,0.22}},{{-0.2,0.08},{0.15,0.22}},{{0.25,-0.1},{0.12,0.25}},{{-0.2,0.1},{0.12,0.2}}};
B=Map[List,{{0,0},{0,1.5},{0,1.5},{0,0.85},{0,0.85},{0,0.3},{0,0.4}},{2}];
{A,prob,init,max}={N@Join[L,B,3],{2,60,10,7,7,7,7}/100.,{0.,2.},10^5};
pts=ifs[prob,A,init,max];//AbsoluteTiming
Graphics[{{Darker@Green,PointSize@Tiny,Point@pts},{Hue@Random[],PointSize@Large,Point@#}/@RandomChoice[pts,200]},AspectRatio-&>1.5]
能畫。
去年用R畫了聖誕樹送給教定量入門的教授,改了一下Wiekvoet: Merry Christmas的代碼。( 今年直接手動塗色送happy spring的卡片了,不是程序媛搞不出酷炫的東西好桑心)
MATLAB ,名副其實的數學軟體。參考 File Exchangeby Anselm Ivanovas====================================
%
function christmas
% Anselm Ivanovas, anselm.ivanovas@student.unisg.ch
%Basically just a nice plot for some christmas fun.
%3D Plot of a hhristmas tree with some presents and snow
%% setup
snow=800; % number of snow flakes [0 .. 5000]
%% draw tree
h=0:0.2:25; %vertical grid
[X,Y,Z] = cylinder(tree(h)); %produce a tree formed cylinder
Z=Z*25; %scale to the right heigth
%add some diffusion to the surface of the tree to make it look more real
treeDiffusion=rand(126,21)-0.5;%some horizontal diffusion data
%add diffusion to the grid points
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(cnt2,cnt1)/X(cnt2,cnt1));
%split the diffusion in the two coordinates, depending on the angle
X(cnt2,cnt1)=X(cnt2,cnt1)+cos(angle)*treeDiffusion(cnt2,cnt1);
Y(cnt2,cnt1)=Y(cnt2,cnt1)+sin(angle)*treeDiffusion(cnt2,cnt1);
%some Vertical diffusion for each point
Z(cnt2,cnt1)=Z(cnt2,cnt1)+(rand-0.5)*0.5;
end
end
%draw the tree
surfl(X,Y,Z,"light")
%% View and format
%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=[r,g,b];%join in a map
for cnt=1:6
%change the lower part to brown for the trunk
map(cnt,:)=[77,63,5]/265;
end
colormap(map)%set the map
view([-37.5,4])%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 知乎er")%self explaining
%% Presents
%Draw some presents around the tree (each with random color)
drawPresent(2,-4,0,3,3,2);
drawPresent(-4,3,0,2,3,1.5);
drawPresent(5,3,0,4,3,3);
drawPresent(-14,-5,0,6,3,1);
drawPresent(-9,-10,0,2,2,2);
drawPresent(0,4,0,4,3,3);
drawPresent(-6,-13,0,3,3,3);
%% Snow
%create some random 3D coordinates for the snow (amount as in setup above)
snowX=(rand(snow,1)*25-12.5);
snowY=(rand(snow,1)*25-12.5);
snowZ=(rand(snow,1)*27);
%Note:Some flakes will end up IN the tree but just can"t be seen then
plot3(snowX,snowY,snowZ,"w*")%plot coordinates as white snow flakes
hold off%Done
end % of function
%% ============= private functions
function r=tree(h)%Gives a profile for the tree
for cnt=1:length(h)
if(h(cnt)==0)%no Width at the bottom. Ensures a "closed" trunk
r(cnt)=0;
end
%smaller radius for the trunk
if (h(cnt)&>0 h(cnt)&<=3)
r(cnt)=1.5;
end
%reduce radius gradually from 8 to 0. Note: will only work with a trunk heigth
%of 3 and a whole tree heigth of 25. Scale the height of the tree in
%the "draw tree" section, since the cylinder command will return a 1
%unit high cylinder anyway
if(h(cnt)&>3)
r(cnt)=8-(h(cnt)-3)*0.3636;
end
end
end % of function
%Draws a present with the given coordinate + size in a random color
%Note:Given coordinates apply to the lower front + left corner of the
%present (the one closest to the viewer) as seen in the plot
function drawPresent(dx,dy,dz,scalex,scaley,scalez)
%the standard present coordinates
presentX=[0.5 0.5 0.5 0.5 0.5; 0 1 1 0 0; 0 1 1 0 0; 0 1 1 0 0; 0.5 0.5 0.5 0.5 0.5];
presentY=[0.5 0.5 0.5 0.5 0.5; 0 0 1 1 0; 0 0 1 1 0; 0 0 1 1 0; 0.5 0.5 0.5 0.5 0.5];
presentZ=[0 0 0 0 0; 0 0 0 0 0; 0.5 0.5 0.5 0.5 0.5; 1 1 1 1 1; 1 1 1 1 1];
%draw some presents with random colors
%scale present and move it to the right place and get the plot handle
myHandle=surf((presentX*scalex+dx),(presentY*scaley+dy), (presentZ*scalez+dz));
%some random color map
randColorMap(:,:,1)=repmat(rand,[5,5]);%r component
randColorMap(:,:,2)=repmat(rand,[5,5]);%g component
randColorMap(:,:,3)=repmat(rand,[5,5]);%b component
%Assign colormap just to the plot handle object of the present, so the tree
%does not change color
set(myHandle,"CData",randColorMap)
shading interp %Nice shding + without grid
end % of function
雞汁的我
Making a Christmas Animation with the Wolfram Mathematica
Christmas Tree
R語言
推薦閱讀:
※python多進程為什麼一定要__name__="__main__"?
※為啥進程池封裝在裝飾器中不能生效,而多進程可以?
※進程間通信和線程間通信的區別?
※如何利用Python抓取PDF中的某些內容?
※pandas 怎麼根據一列的數據的值的情況判斷來生成另外一列的數值?
TAG:Python | 數學軟體 | MATLAB | WolframMathematica | R編程語言 |