蕨型叶分形

目录

要点

基本语句

EraseMode

习题

[1 设置颜色](#1 设置颜色)

[2 旋转蕨型叶图](#2 旋转蕨型叶图)

[3 枝干](#3 枝干)

[4 塞平斯基三角形](#4 塞平斯基三角形)


要点


蕨型叶是通过一个点的反复变换产生的,假设x是一个含有两个分量的向量,可以用来表示平面内的一个点,则可以用Ax+b的形式对其进行变换。


基本语句


语句darkgreen=[0 2/3 0]设置深绿色的颜色变量,在matlab中用红绿蓝分量来表示颜色,这里将绿色分量设置为2/3,其余为0。
采用animatedline来绘制蕨型叶,h为蕨型叶图形的句柄。

复制代码
function fern_a


    shg
    clf reset
    set(gcf,'color','white','menubar','none', ...
        'numbertitle','off','name','Fractal Fern')
    x = [.5; .5];
    h = animatedline('Marker', '.', 'LineStyle', 'none', 'Color', [0 2/3 0]);
    axis([-3 3 0 10])
    axis off  %不显示坐标轴,只显示叶子本身
    stop = uicontrol('style','toggle','string','stop', ...
        'background','white');
    drawnow %绘制

    p  = [ .85  .92  .99  1.00];
    A1 = [ .85  .04; -.04  .85];  b1 = [0; 1.6];
    A2 = [ .20 -.26;  .23  .22];  b2 = [0; 1.6];
    A3 = [-.15  .28;  .26  .24];  b3 = [0; .44];
    A4 = [  0    0 ;   0   .16];

    cnt = 1; %将计数器的初始值设置为1
    tic
    while ~get(stop,'value')
        r = rand;
        if r < p(1)
            x = A1*x + b1;
        elseif r < p(2)
            x = A2*x + b2;
        elseif r < p(3)
            x = A3*x + b3;
        else
            x = A4*x;
        end
        addpoints(h, x(1), x(2));
        drawnow %重新绘制图形
        cnt = cnt + 1; %生成新的点后计数器的数值自增1
    end
    t = toc; %读取秒表数据
    s = sprintf('%8.0f points in %6.3f seconds',cnt,t);
    text(-1.5,-0.5,s,'fontweight','bold');
    set(stop,'style','pushbutton','string','close','callback','close(gcf)')
end

EraseMode


EraseMode 属性不再受支持,而且在以后的版本中会出错。

初始语句:

复制代码
h = plot(x(1),x(2),'.');
darkgreen = [0 2/3 0];
set(h,'markersize',1,'color',darkgreen,'erasemode','none');

修改后:

复制代码
h = animatedline('Marker', '.', 'LineStyle', 'none', 'Color', [0 2/3 0]);

习题


1 设置颜色

在MATLAB中,颜色可以使用多种方式来表示,其中一种常用的方式是使用RGB(Red-Green-Blue)表示法。在RGB表示法中,每个颜色的红、绿、蓝三个分量的取值范围为0到1,表示颜色的深浅程度。

复制代码
function fern_a


    shg
    clf reset
    set(gcf,'color','black','menubar','none', ...
        'numbertitle','off','name','Fractal Fern')
    x = [.5; .5];
    h = animatedline('Marker', '.', 'LineStyle', 'none', 'Color', [1 0.75 0.8]);
    axis([-3 3 0 10])
    axis off
    stop = uicontrol('style','toggle','string','stop', ...
        'background','black','ForegroundColor','white');
    drawnow

    p  = [ .85  .92  .99  1.00];
    A1 = [ .85  .04; -.04  .85];  b1 = [0; 1.6];
    A2 = [ .20 -.26;  .23  .22];  b2 = [0; 1.6];
    A3 = [-.15  .28;  .26  .24];  b3 = [0; .44];
    A4 = [  0    0 ;   0   .16];

    cnt = 1;
    tic
    while ~get(stop,'value')
        r = rand;
        if r < p(1)
            x = A1*x + b1;
        elseif r < p(2)
            x = A2*x + b2;
        elseif r < p(3)
            x = A3*x + b3;
        else
            x = A4*x;
        end
        addpoints(h, x(1), x(2));
        drawnow
        cnt = cnt + 1;
    end
    t = toc;
    s = sprintf('%8.0f points in %6.3f seconds',cnt,t);
    text(-1.5,-0.5,s,'fontweight','bold','Color','red');
    set(stop,'style','pushbutton','string','close','callback','close(gcf)')
end

2 旋转蕨型叶图

复制代码
%修改坐标
addpoints(h, x(2), x(1));

%修改标注位置
s = sprintf('%8.0f points in %6.3f seconds',cnt,t);
text(2.5,-2.9,s,'fontweight','bold','Color','red');

3 枝干

复制代码
A4 = [0, 0; 0, 0.16];
%修改为
A4 = [0, 0; 0, 0.08];
%这将使得蕨型叶的枝干更加瘦长

蕨型叶的起始点均为(0,0)。


4 塞平斯基三角形

复制代码
function sierpinski_triangle_a
    shg
    clf reset
    set(gcf, 'color', 'white', 'menubar', 'none', ...
        'numbertitle', 'off', 'name', 'Fractal Sierpinski Triangle')
    x = [.5; sqrt(3)/6];
    h = animatedline('Marker', '.', 'LineStyle', 'none', 'Color', [0 2/3 0]);
    axis([0 1 0 sqrt(3)/2])
    axis off
    stop = uicontrol('style', 'toggle', 'string', 'stop', ...
        'background', 'white');
    hold on
    plot([0, 1, 0.5, 0], [0, 0, sqrt(3)/2, 0], 'k-*'); % 绘制三角形的顶点
    drawnow

    A1 = [0.5, 0; 0, 0.5];  b1 = [0; 0];
    A2 = [0.5, 0; 0, 0.5];  b2 = [0.5; 0];
    A3 = [0.5, 0; 0, 0.5];  b3 = [0.25; sqrt(3)/4];

    cnt = 1;
    tic
    while ~get(stop, 'value')
        r = rand;
        if r <= 1/3
            x = A1 * x + b1;
        elseif r <= 2/3 & r>1/3
            x = A2 * x + b2;
        else
            x = A3 * x + b3;
        end
        addpoints(h, x(1), x(2));
        drawnow
        cnt = cnt + 1;
    end
    t = toc;
    s = sprintf('%8.0f points in %6.3f seconds', cnt, t);
    text(0.25, -0.05, s, 'fontweight', 'bold');
    set(stop, 'style', 'pushbutton', 'string', 'close', 'callback', 'close(gcf)')
end
相关推荐
曹勖之8 小时前
UE 5 和simulink联合仿真,如果先在UE5这一端结束Play,过一段时间以后**Unreal Engine 5** 中会出现显存不足错误
matlab·ue5·机器人
曹勖之11 小时前
simulink有无现成模块可以实现将三个分开的输入合并为一个[1*3]的行向量输出?
matlab
机器学习之心19 小时前
Transformer-BiGRU多变量时序预测(Matlab完整源码和数据)
深度学习·matlab·transformer·bigru
slandarer1 天前
MATLAB | 绘图复刻(十九)| 轻松拿捏 Nature Communications 绘图
开发语言·matlab
【杨(_> <_)】1 天前
信号处理分析工具——时频分析(一)
算法·matlab·信号处理
曹勖之2 天前
在MATLAB中使用自定义的ROS2消息
开发语言·matlab·机器人·ros·simulink·ros2
我爱C编程2 天前
基于QPSK调制解调+Polar编译码(SCL译码)的matlab性能仿真,并对比BPSK
matlab·qpsk·polar编译码·scl译码
bubiyoushang8882 天前
matlab实现高斯烟羽模型算法
开发语言·算法·matlab
tyatyatya3 天前
MATLAB 中调整超参数的系统性方法
开发语言·matlab
Expecto03 天前
Matlab数值计算
matlab·数值计算