相关问题如何用c语言画一个“圣诞树”? - 编程
回复内容:我只是搬运http://codegolf.stackexchange.com上的答案,原作者是silvia (@silvia ),用的是mathematica。
pd = .5;s[t_, f_] := t^.6 - fdt[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]
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];//absolutetiminggraphics[{{darker@green,pointsize@tiny,point@pts},{hue@random[],pointsize@large,point@#}&/@randomchoice[pts,200]},aspectratio->1.5]
去年用r画了圣诞树送给教定量入门的教授,改了一下wiekvoet: merry christmas的代码。
( 今年直接手动涂色送happy spring的卡片了,不是程序媛搞不出酷炫的东西好桑心)wolfram mathematica 算数学软件吧?
还有这个:
算不算抖机灵 = =鸡汁的我
matlab ,名副其实的数学软件。
参考 参考 file exchange
by 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%% setupsnow=800; % number of snow flakes [0 .. 5000]%% draw treeh=0:0.2:25; %vertical grid[x,y,z] = cylinder(tree(h)); %produce a tree formed cylinderz=z*25; %scale to the right heigth%add some diffusion to the surface of the tree to make it look more realtreediffusion=rand(126,21)-0.5;%some horizontal diffusion data%add diffusion to the grid pointsfor cnt1=1:21for cnt2=16:126%starting above the trunk%get the angle to always diffuse in direction of the radiusangle=atan(y(cnt2,cnt1)/x(cnt2,cnt1));%split the diffusion in the two coordinates, depending on the anglex(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 pointz(cnt2,cnt1)=z(cnt2,cnt1)+(rand-0.5)*0.5;endend%draw the treesurfl(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 componentg=(0.2969:(0.4012/50):0.6981)';%green componentb=(0.0625:(0.2696/50):0.3321)';%blue componentmap=[r,g,b];%join in a mapfor cnt=1:6%change the lower part to brown for the trunkmap(cnt,:)=[77,63,5]/265;endcolormap(map)%set the mapview([-37.5,4])%change the view to see a little more of the actual 3d treelighting phong %some nice lightingshading interp %remove grid and smoothen the surface coloraxis equal %takes care of display in the right proportionaxis([-10 10 -10 10 0 30]) %give some more axis space (for the snow later)axis off %but don't show axishold on %to draw the resttitle('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 thenplot3(snowx,snowy,snowz,'w*')%plot coordinates as white snow flakeshold off%doneend % of function%% ============= private functionsfunction r=tree(h)%gives a profile for the treefor cnt=1:length(h)if(h(cnt)==0)%no width at the bottom. ensures a closed trunkr(cnt)=0;end%smaller radius for the trunkif (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 anywayif(h(cnt)>3)r(cnt)=8-(h(cnt)-3)*0.3636;endendend % 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 plotfunction drawpresent(dx,dy,dz,scalex,scaley,scalez)%the standard present coordinatespresentx=[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 handlemyhandle=surf((presentx*scalex+dx),(presenty*scaley+dy), (presentz*scalez+dz));%some random color maprandcolormap(:,:,1)=repmat(rand,[5,5]);%r componentrandcolormap(:,:,2)=repmat(rand,[5,5]);%g componentrandcolormap(:,:,3)=repmat(rand,[5,5]);%b component%assign colormap just to the plot handle object of the present, so the tree%does not change colorset(myhandle,'cdata',randcolormap)shading interp %nice shding + without gridend % of function
christmas tree
r语言能画,但是这种比较有什么意义呢..................................
l c(0.03, 0, 0 , 0.1, 0.85, 0.00, 0.00, 0.85, 0.8, 0.00, 0.00, 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), nrow=4)# ... and each row is a translation vectorb c(0, 0, 0, 1.5, 0, 1.5, 0, 0.85, 0, 0.85, 0, 0.3, 0, 0.4), nrow=2)prob = c(0.02, 0.6,.08, 0.07, 0.07, 0.07, 0.07)# iterate the discrete stochastic map n = 1e5 #5 # number of iterations x = matrix(na,nrow=2,ncol=n)x[,1] = c(0,2) # initial pointk for (i in 2:n) x[,i] = crossprod(matrix(l[,k[i]],nrow=2),x[,i-1]) + b[,k[i]] # iterate # plot the iteration history png('card.png')par(bg='darkblue',mar=rep(0,4)) plot(x=x[1,],y=x[2,], col=grep('green',colors(),value=true), axes=false, cex=.1, xlab='', ylab='' )#,pch='.')bals points(x=x[1,bals],y=x[2,bals]-.1, col=c('red','blue','yellow','orange'), cex=2, pch=19)text(x=-.7,y=8, labels='merry', adj=c(.5,.5), srt=45, vfont=c('script','plain'), cex=3, col='gold')text(x=0.7,y=8, labels='christmas', adj=c(.5,.5), srt=-45, vfont=c('script','plain'), cex=3, col='gold')dev.off()
