为什么我要学计算机啊 2021-12-20 18:29 采纳率: 50%
浏览 678
已结题

怎么用matlab弄圣诞树啊!

% setupsnow=450; % 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: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 treeh0 = figure('Units','inches');pos = h0.Position;pos(1) = 1; pos(2) = 1;pos(3) = 7; pos(4) = 7;h0.Position = pos;surfl(X,Y,Z,'light')
这是我复制大神的代码,但是我用16版的matlab有错误,然后我把(tree(h))改成(h)就可以运行,但是树是倒过来的,我在想是不是因为版本不同的问题啊!(我真的在计算机方面很菜啊)

  • 写回答

1条回答 默认 最新

  • 技术专家团-Joel 2021-12-20 20:00
    关注

    你好同学,那是因为tree这个函数是作者自己定的,没在博文中展示出来,你可以去这里下载完整的圣诞树代码
    链接:https://pan.baidu.com/s/1KNE9sUM_tzNdSZa81b8gmg 提取码:qmvk
    (有帮助记得给个采纳支持一下呢)

    其实matlab画圣诞树还有很多方法呢,比如
    方法一:旋转的圣诞树

    clear, clc, close all
    figure, hold on, N = 6^5; c = 50; k = randi(6,c,1); l = randperm(N,c);
    q = @(x) rand(N,1); a = q()*2*pi; z = q(); r = .4*(1-z); t = q();
    x = r.*cos(a); y = r.*sin(a); P = {'ro','ys','md','b^','kh','c*'};
    scatter3(x.*t,y.*t,z,[],[zeros(N,1) (t.*r).^.6 zeros(N,1)],'*')
    plot3(0,0,1.05,'rp','markers',12,'markerf','r')
    for i = 1:6
        L = l(k==i);
        plot3(x(L),y(L),z(L),P{i},'markers',8,'linew',2);
    end
    [X,Y,Z] = cylinder(.025,30);
    surf(X,Y,-Z*.1)
    view(3, 9), axis equal off
    for i = 1:9:c*9, set(gca,'vie',[i, 9]); drawnow, end
    

    方法二:静态圣诞树

    % Christmas Tree - static version
    % author: Hanchu Wang
    % This code is for plotting 2 chirstmas trees based on Conical Spiral
    % Function.
    % =================================================
    % =================================================
    close all; clear all; clc
    % =================================================
    % Color RGB
    % =================================================
    bl = [ 22,  42, 114]/255;
    dg = [ 22,  91,  51]/255;
    gr = [ 20, 107,  58]/255;
    oy = [248, 178,  41]/255;
    or = [234,  70,  48]/255;
    rd = [187,  37,  40]/255;
    sg = [ 86, 125,  48]/255;
    mg = [143, 177,  78]/255;
    dy = [249, 243,  85]/255;
    % =================================================
    % Conical Spiral - Parameters and function
    % Equation reference: Weisstein, Eric W. "Conical Spiral." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/ConicalSpiral.html
    % =================================================
    h = 10;
    r = h/1.618/2;
    a = 100;
    fx = @(h,r,a,z) (h-z)./h.*r.*cos(a.*z);
    fy = @(h,r,a,z) (h-z)./h.*r.*sin(a.*z);
    % =================================================
    % Conical Spiral - Parameters and function
    % =================================================
    mainbody_z = 0:0.01:10;
    mainbody_x = fx(h,r,a,mainbody_z);
    mainbody_y = fy(h,r,a,mainbody_z);
    ball_z=0.1:0.2:10;
    ball_x=fx(h,r,a,ball_z);
    ball_y=fy(h,r,a,ball_z);
    leaf_z1=0.1:0.01:5;
    leaf_x1=fx(h,r,a*2,leaf_z1)+0.1;
    leaf_y1=fy(h,r,a*2,leaf_z1)+0.1;
    stringLight_z=0.1:0.01:10;
    stringLight_x=fx(h,r+0.1,a*4,stringLight_z+0.01);
    stringLight_y=fy(h,r+0.1,a*4,stringLight_z+0.01);
    stringLight2_z=0:0.12:10;
    stringLight2_x=fx(h,r,a,stringLight2_z);
    stringLight2_y=fy(h,r,a,stringLight2_z);
    leaf_z2=0.1:0.015:10;
    leaf_x2=fx(h,r,a,leaf_z2+0.01);
    leaf_y2=fy(h,r,a,leaf_z2+0.01);
    snow=rand(50,3);
    snow(:,1:2)=snow(:,1:2).*((4-(-4))+1)+(-4.5);
    snow(:,3)=snow(:,3).*(10+1);
    snow2=rand(105,3);
    snow2(:,1:2)=snow2(:,1:2).*((4-(-4))+1)+(-4.5);
    snow2(:,3)=snow2(:,3).*(10+1);
    % =================================================
    % Plotting
    % =================================================
    figure (1)
    % tree
    plot3(mainbody_x,mainbody_y,mainbody_z, '*', 'Color',gr,'LineWidth', 1.5)
    hold on
    % star
    plot3(0,0,h+0.2,'p', 'MarkerSize',28,'MarkerFaceColor',oy, 'MarkerEdgeColor',oy.*0.8, 'LineWidth', 2)
    scatter3(0,0,h+0.2,20000, 'w', 'MarkerFaceColor', oy, 'MarkerEdgeAlpha', 0, 'MarkerFaceAlpha', 0.1);
    %leaf
    plot3(leaf_x1,leaf_y1,leaf_z1,'*','Color', gr);
    plot3(leaf_x2,leaf_y2,leaf_z2,'*','Color', sg);
    %string lights
    plot3(stringLight_x,stringLight_y,stringLight_z,'.', 'Color', dy);
    scatter3(stringLight_x,stringLight_y,stringLight_z,900, '.', 'MarkerFaceColor', dy, 'MarkerEdgeColor', dy, 'MarkerEdgeAlpha', 0, 'MarkerFaceAlpha', 0.1);
    %balls
    plot3(ball_x,ball_y,ball_z, 'o','MarkerSize',10, 'Color',or, 'MarkerFaceColor', or, 'MarkerEdgeColor',rd)
    % snow
    plot3(snow(:,1),snow(:,2),snow(:,3),'*','Color',[1 1 1])
    plot3(snow2(:,1),snow2(:,2),snow2(:,3),'.','Color',[1 1 1], 'MarkerSize',5)
    % text
    text(2,2,8,{['Merry Christmas']},'HorizontalAlignment','left','FontSize',20,'Color', [1 1 1],'fontname','Britannic Bold');
    % =================================================
    % Formatting
    % =================================================
    pbaspect([1 1 1])
    xlim([-4.5 4.5])
    ylim([-4.5 4.5])
    zlim([0 11])
    set(gca,'Color',bl*0.2)
    set(gcf,'Color','w')
    set(gcf, 'Position',  [100, 100, 1000, 1000])
    hold off
    figure (2)
    % tree
    plot3(mainbody_x,mainbody_y,mainbody_z, '-', 'Color',gr,'LineWidth', 1.5)
    hold on
    % star
    plot3(0,0,h+0.2,'p', 'MarkerSize',28,'MarkerFaceColor',oy, 'MarkerEdgeColor',oy.*0.8, 'LineWidth', 2)
    scatter3(0,0,h+0.2,20000, 'w', 'MarkerFaceColor', oy, 'MarkerEdgeAlpha', 0, 'MarkerFaceAlpha', 0.1);
    %leaf
    plot3(leaf_x1,leaf_y1,leaf_z1,'-','Color', gr);
    plot3(leaf_x2,leaf_y2,leaf_z2,'-','Color', sg);
    %string lights
    plot3(stringLight2_x,stringLight2_y,stringLight2_z,':', 'Color', dy,  'LineWidth', 2);
    %balls
    plot3(ball_x,ball_y,ball_z, 'o','MarkerSize',10, 'Color',or, 'MarkerFaceColor', or, 'MarkerEdgeColor',rd)
    % snow
    plot3(snow(:,1),snow(:,2),snow(:,3),'*','Color',[1 1 1])
    plot3(snow2(:,1),snow2(:,2),snow2(:,3),'.','Color',[1 1 1], 'MarkerSize',5)
    % text
    text(2,2,8,{['Merry Christmas']},'HorizontalAlignment','left','FontSize',20,'Color', [1 1 1],'fontname','Britannic Bold');
    % =================================================
    % Formatting
    % =================================================
    pbaspect([1 1 1])
    xlim([-4.5 4.5])
    ylim([-4.5 4.5])
    zlim([0 11])
    % -------------------------------------------------
    % polygon frame
    set(gca,'Color',bl*0.2)
    set(gcf,'Color','w')
    % if wants no frame, use following lines instead
    % set(gcf,'Color',bl*0.2)
    % -------------------------------------------------
    %optional
    set(gcf, 'Position',  [1000, 100, 1000, 1000])
    hold off
    % End--------------------------------------------------------------------
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月28日
  • 已采纳回答 12月20日
  • 创建了问题 12月20日

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能