欢迎访问 外汇EA下载与MT4/MT5自动交易资源 - 聚合外汇EA、黄金EA、量化交易工具与自动化交易实战内容。
登录 注册

使用迭代函数系统 (IFS) 在 MQL5 中创建分形 - MetaTrader 5 专家

author emer | 108 人阅读 | 0 人评论 |

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

有许多程序允许创建自相似集,定义为迭代函数系统(IFS)。例如,参见弗拉克廷特,分形设计师或者IFS Matlab 生成器。得益于速度MQL5语言和使用图形对象的可能性,这些美丽的集合可以在元交易者 5客户端。

骨形态发生蛋白库,开发者德米特里(整数)提供了新的图形机会并极大地简化了图形图像的创建。这个图书馆是授予并获得特别奖MetaQuotes 软件公司

在本出版物中,我们将考虑与以下人员合作的示例:骨形态发生蛋白图书馆。此外,我们还将介绍使用迭代函数系统创建分形集的算法。

1. 平面的仿射变换

平面的仿射变换是一种映射Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5。一般来说,仿射二维变换可以用一些定义 Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5矩阵和 Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5, 向量。坐标为 (x,y) 的点变换到其他点Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5使用线性变换:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

变换必须是非奇异的,即Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5。  仿射变换改变大小Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5次。

仿射变换不会改变几何对象的结构(线变换为线),AT 允许描述对象的简单“变形”,例如旋转、缩放和平移。

仿射平面变换的示例:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

2)“平面的缩放”Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5系数(X 和 Y 轴):

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

3)翻译飞机的 Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5向量:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

收缩映射是关键(参见哈钦森结果)。

如果Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5有坐标Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5是一个度量(例如,欧几里得度量:Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5)。仿射变换称为收缩如果Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5, 在哪里Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

这是仿射变换的示例:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

2. 相似度变换

分形的构造方式如下:一些(简单的)几何对象(截面、三角形、正方形)分为 N 块,其中 M 个用于进一步“构造”该集合(如果 N=M,我们将得到结果集合的整数维度)。此外,这个过程对每件作品都一次又一次地重复。

经典分形:

部分:

分形具有自相似结构,有些分形可以通过多种相似变换来定义。仿射变换的结构取决于分形构造的方式。

正如您将进一步看到的,它非常简单,我们必须解决的唯一问题是仅描述分形构造的第一次迭代并找到相应的仿射变换集。

假设我们有一些集合。根据分形创建算法,我们需要将其缩小、旋转并“放在某个地方”。  问题是使用仿射变换来描述这个过程,即我们需要找到矩阵和向量。

很容易证明,只需取初始集合(非平凡)的 3 个点并将它们转换为“缩减”集合的 3 个对应点就足够了。这个变换将产生 6 个线性方程,使我们能够找到 a、b、c、d、e、f 作为解。

让我们展示一下。认为Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5三角形变换为Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5三角形。

通过求解线性方程组,我们将能够得到 a、b、c、d、e 和 f 系数:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

示例:谢尔宾斯基垫片:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

点的坐标是:

我们有3个转变:

线性方程组如下所示:

解决方案是:Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5,Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5,Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

我们已经找到了三个仿射变换的系数。此外,我们将使用它们来创建自相似集。

3. 使用迭代函数系统创建分形

迭代函数系统 (IFS)是一组仿射收缩Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5在哪里Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5- 是“权重”。每个 IFS 函数由 7 个数字定义:Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5, 在哪里Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5迭代过程中使用权重作为第n次变换的概率。 It's better to define their values, proportional to contraction:Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

让我们考虑使用迭代函数系统的分形构造算法(另请参阅混沌游戏)。

我们首先需要获取一些带有坐标的初始点Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5。接下来,我们随机选择一些收缩并绘制点Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5。再次,让我们随机选择一个缩写Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5和情节Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5。最后我们将拥有Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5作为一组点。

收缩的选择取决于它的“概率”。如果我们重复这个过程(例如,大约 30000 个点)并绘制结果集,我们将看到它的结构,尽管是随机过程。

以下是谢尔宾斯基垫片的示例:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 1. 使用第 2 章中计算的 IFS 系数生成的 Sierpinski 垫片

//+------------------------------------------------------------------+
//|                                        IFS_Sierpinski_Gasket.mq5 |
//|                        版权所有 2011,MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#财产版权“版权所有 2011,MetaQuotes Software Corp.”
#属性链接      “https://www.mql5.com”
#属性版本  “1.00”

//-- 包含 cIntBMP 类的文件
#include 

//-- Sierpinski 垫片 IFS 系数
//-- (a,b,c,d) 矩阵
双倍的IFS_a[3] = {0.50,  0.50,  0.50};双倍的IFS_b[3] = {0.00,  0.00,  0.00};双倍的IFS_c[3] = {0.00,  0.00,  0.00};双倍的IFS_d[3] = {0.50,  0.50,  0.50};//-- (e,f) 向量
双倍的IFS_e[3] = {0.00,  0.00,  0.50};双倍的IFS_f[3] = {0.00,  0.50,  0.50};//-- 变换的“概率”,乘以 1000
双倍的IFS_p[3]={第333章,第333章,第333章};双倍的问题[3];// Probs 数组 - 用于选择 IFS 变换cIntBMP bmp;    // cIntBMP 类实例
整数规模=350;  // 尺度系数
//+------------------------------------------------------------------+
//|专家初始化函数                                   |
//+------------------------------------------------------------------+
整数 初始化时()
  {//-- 准备 Probs 数组  双倍的米=0;
  为了整数我=0;我<数组大小(IFS_p);我++)
     {
      Probs[i]=IFS_p[i]+m;
      m=m+IFS_p[i];
     }//-- BMP图像的大小  整数X尺寸=500;
  整数Y尺寸=400;//-- 使用 clrSeashell 背景颜色创建 BMP 图像 XSizexYSize   bmp.Create(XSize,YSize,clrSeashell);//-- 图像矩形   bmp.DrawRectangle(0,0,X尺寸-1,Y尺寸-1,clrBlack);//-- 点坐标(将用于构造集合)  双倍的x0=0;
  双倍的y0=0;
  双倍的x,y;//-- 要计算的点数(更多点 - 详细图像)  整数点=1500000;//-- 计算集合  为了整数我=0;我<点;我++)
     {
      // 选择具有概率的 IFS 变换,与定义的比例成比例      双倍的prb=1000*(兰特()/32767.0);
      为了整数k=0; k<数组大小(IFS_p); k++)
        {
        如果(prb<=概率[k])
           {
            // 仿射变换            x = IFS_a[k] * x0 + IFS_b[k] * y0 + IFS_e[k];
            y = IFS_c[k] * x0 + IFS_d[k] * y0 + IFS_f[k];
            // 更新之前的坐标            x0 = x;
            y0 = y;
            // 转换为BMP图像坐标            //(注意cIntBMP中的Y轴)            整数scX =整数数学轮(X尺寸/2+ (x-0.5)*规模));
            整数SCY =整数数学轮(Y尺寸/2+ (y-0.5)*规模));
            // 如果点坐标在图像内部,则绘制点            如果(scX>=0&& scX=0&& scY休息;
           }
        }
     }//-- 将图像保存到文件   bmp.保存(“bmpimg”,真的);//-- 在图表上绘制图像   bmp.显示(0,0,“bmpimg”,“国际金融服务协会”);//---  返回0);
  }//+------------------------------------------------------------------+
//|专家去初始化函数                                 |
//+------------------------------------------------------------------+
空白 解初始化常量 整数原因)
  {//--- 从图表中删除图像  对象删除0,“国际金融服务协会”);//--- 删除文件   bmp.删除(“bmpimg”,真的);
  }//+------------------------------------------------------------------+

如果我们将scale设置为1350,将迭代次数增加到15000000,并更改初始点的偏移:

整数scX =数学轮(X尺寸/2+ (x-0.75)*规模);整数SCY =数学轮(Y尺寸/2+ (y-0.75)*规模);

我们将能够看到集合的缩放区域。可以看到(图2),它具有一种自相似的结构:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 2. Sierpinski 垫片的放大区域

我们来考虑一下著名的巴恩斯利的蕨类植物,提出者迈克尔·巴恩斯利。情况更复杂。

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 3. 巴恩斯利的蕨类植物

代码类似,但在本例中我们有 4 个具有不同权重的 IFS 收缩。

//+------------------------------------------------------------------+
//|                                                     IFS_fern.mq5 |
//|                        版权所有 2011,MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#财产版权“版权所有 2011,MetaQuotes Software Corp.”
#属性链接      “https://www.mql5.com”
#属性版本  “1.00”
#include 
//-- Barnsley Fern IFS 系数
//-- (a,b,c,d) 矩阵
双倍的IFS_a[4] = {0.00,  0.85,  0.20,  -0.15};双倍的IFS_b[4] = {0.00,  0.04, -0.26,  0.28};双倍的IFS_c[4] = {0.00, -0.04,  0.23,  0.26};双倍的IFS_d[4] = {0.16,  0.85,  0.22,  0.24};//-- (e,f) 向量
双倍的IFS_e[4] = {0.00,  0.00,  0.00,  0.00};双倍的IFS_f[4] = {0.00,  1.60,  1.60,  0.00};//-- 变换的“概率”,乘以 1000
双倍的IFS_p[4] = {10,    850,    70,    70};双倍的问题[4];
cIntBMP bmp;整数规模=50;//+------------------------------------------------------------------+
//|专家初始化函数                                   |
//+------------------------------------------------------------------+
整数 初始化时()
  {
  双倍的米=0;
  为了整数我=0;我<数组大小(IFS_p);我++)
     {
      Probs[i]=IFS_p[i]+m;
      m=m+IFS_p[i];
     }

  整数X尺寸=600;
  整数Y尺寸=600;

   bmp.Create(XSize,YSize,clrSeashell);

   bmp.DrawRectangle(0,0,X尺寸-1,Y尺寸-1,clrBlack);

  双倍的x0=0;
  双倍的y0=0;
  双倍的x,y;

  整数点=250000;

  为了整数我=0;我<点;我++)
     {
      双倍的prb=1000*(兰特()/32767.0);
      为了整数k=0; k<数组大小(IFS_p); k++)
        {
        如果(prb<=概率[k])
           {
            x = IFS_a[k] * x0 + IFS_b[k] * y0 + IFS_e[k];
            y = IFS_c[k] * x0 + IFS_d[k] * y0 + IFS_f[k];
            x0 = x;
            y0 = y;
            整数scX =整数数学轮(X尺寸/2+ (x)*比例));
            整数SCY =整数数学轮(Y尺寸/2+ (y-5)*规模));
            如果(scX>=0&& scX=0&& scY休息;
           }
        }
     }
   bmp.保存(“bmpimg”,真的);
   bmp.显示(0,0,“bmpimg”,“国际金融服务协会”);//---  返回0);
  }//+------------------------------------------------------------------+
//|专家去初始化函数                                 |
//+------------------------------------------------------------------+
空白 解初始化常量 整数原因)
  {
  对象删除0,“国际金融服务协会”);
   bmp.删除(“bmpimg”,真的);
  }//+------------------------------------------------------------------+

值得注意的是,如此复杂的结构仅用 28 个数字就能定义。

如果我们将比例增加到 150,并将迭代次数设置为 1250000,我们将看到缩放的片段:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 4. 巴恩斯利蕨的片段

正如您所看到的,该算法是通用的,它允许您生成不同的分形集。

下一个示例是 Sierpinski Carpet,由以下 IFS 系数定义:

//-- Sierpinski 垫片 IFS 系数
双倍的IFS_a[8] = {0.333,0.333,0.333,0.333,0.333,0.333,0.333,0.333};双倍的IFS_b[8] = {0.00,  0.00,  0.00,  0.00,0.00,  0.00,  0.00,  0.00};双倍的IFS_c[8] = {0.00,  0.00,  0.00,  0.00,0.00,  0.00,  0.00,  0.00};双倍的IFS_d[8] = {0.333,0.333,  0.333,  0.333,0.333,  0.333,  0.333,0.333};双倍的IFS_e[8] = {-0.125, -3.375, -3.375,  3.125,3.125, -3.375, -0.125,3.125};双倍的IFS_f[8] = {6.75,0.25,6.75,  0.25,6.75,3.5,0.25,3.50};//-- “概率”,乘以 1000
双倍的IFS_p[8]={125,125,125,125,125,125,125,125};

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 5.谢尔宾斯基地毯

在第二章中我们考虑了IFS收缩系数的计算算法。

让我们考虑如何创建“分形词”。在俄语中,“分形”这个词看起来像:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 6. 俄语中的“分形”一词

为了找到 IFS 系数,我们需要求解相应的线性系统。解决方案是:

//-- 俄语中“分形”一词的 IFS 系数
双倍的IFS_a[28]=
  {
  0.00,0.03,  0.00,0.09,0.00,0.03, -0.00,0.07,0.00,0.07,0.03,  0.03,  0.03,  0.00,
  0.04,0.04, -0.00,0.09,0.03,0.03,  0.03,0.03,0.03,0.00,0.05, -0.00,  0.05,  0.00  };双倍的IFS_b[28]=
  {
   -0.11,0.00,0.07,0.00, -0.07,0.00, -0.11,  0.00, -0.07,  0.00, -0.11,  0.11,0.00, -0.14,
   -0.12,0.12,-0.11,0.00, -0.11,0.11,  0.00, -0.11,  0.11, -0.11,  0.00, -0.07,0.00, -0.07  };双倍的IFS_c[28]=
  {
  0.12,  0.00,  0.08,  -0.00,  0.08,  0.00,  0.12,  0.00,  0.04,  0.00,  0.12,  -0.12,0.00,  0.12,
  0.06,  -0.06,  0.10,  0.00,  0.12,  -0.12,  0.00,  0.12,  -0.12,  0.12,0.00,  0.04,  0.00,  0.12  };双倍的IFS_d[28]=
  {
  0.00,  0.05,  0.00,  0.07,  0.00,  0.05,  0.00,  0.07,  0.00,  0.07,  0.00,  0.00,  0.07,  0.00,
  0.00,  0.00,  0.00,  0.07,  0.00,  0.00,  0.07,  0.00,  0.00,  0.00,  0.07,  0.00,  0.07,  0.00  };双倍的IFS_e[28]=
  {
   -4.58,  -5.06, -5.16, -4.70, -4.09, -4.35, -3.73, -3.26, -2.76,  -3.26, -2.22, -1.86, -2.04, -0.98,
   -0.46,  -0.76,  0.76,  0.63,  1.78,  2.14,  1.96,  3.11,  3.47,  4.27,  4.60,  4.98,  4.60,5.24  };双倍的IFS_f[28]=
  {
  1.26,  0.89,  1.52,  2.00,  1.52,  0.89,  1.43,  1.96,  1.69,  1.24,  1.43,  1.41,  1.11,  1.43,
  1.79,  1.05,  1.32,  1.96,  1.43,  1.41,  1.11,  1.43,  1.41,  1.43,  1.42,  1.16,  0.71,  1.43  };//-- “概率”,乘以 1000
双倍的IFS_p[28]=
  {
  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  
  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35,  35  };

结果,我们将得到以下图像:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 7. 自相似词

完整的源代码可以在 ifs_fractals.mq5 中找到。

如果我们放大集合,我们会看到自相似结构:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 8. 集合的缩放区域

基于 IFS 的自相似集可以使用分形设计师

我们已经讨论了使用迭代函数系统创建分形集的主题。感谢骨形态发生蛋白库,过程非常简单。现在是时候创建一个类并添加一些功能以使图像更好。

您可能会注意到,集合的正确构造是由概率驱动的。概率的差异意味着该集合具有不规则的结构(参见 Barnsley Fern IFS 的权重)。这一事实可用于创建美丽的图像。我们需要设置与某个邻域中点的频率成比例的颜色。

如果像素颜色取决于先前的值,则可以使用虚拟屏幕(只是一个数组)来完成。最后,虚拟屏幕将使用调色板渲染为 bmp。 bmp图像本身可以绘制为图表的背景图片。

以下是基于 CIFS 类的 EA 交易的代码:

//+------------------------------------------------------------------+
//|                                               IFS_Fern_color.mq5 |
//|                        版权所有 2011,MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#include 
//-- Barnsley Fern IFS 系数
双倍的IFS_a[4] = {0.00,  0.85,  0.20,  -0.15};双倍的IFS_b[4] = {0.00,  0.04, -0.26,  0.28};双倍的IFS_c[4] = {0.00, -0.04,  0.23,  0.26};双倍的IFS_d[4] = {0.16,  0.85,  0.22,  0.24};双倍的IFS_e[4] = {0.00,  0.00,  0.00,  0.00};双倍的IFS_f[4] = {0.00,  1.60,  1.60,  0.00};双倍的IFS_p[4] = {10,    850,    70,    70};//-- 调色板
乌查尔调色板[23*3]=
  {
  0x00,0x00,0x00,0x02,0x0A,0x06,0x03,0x11,0x0A,0x0B,0x1E,0x0F,0x0C,0x4C,0x2C,0x1C,0x50,0x28,
  0x2C,0x54,0x24,0x3C,0x58,0x20,0x4C,0x5C,0x1C,0x70,0x98,0x6C,0x38,0xBC,0xB0,0x28,0xCC,0xC8,
  0x4C,0xB0,0x98,0x5C,0xA4,0x84,0xBC,0x68,0x14,0xA8,0x74,0x28,0x84,0x8C,0x54,0x94,0x80,0x40,
  0x87,0x87,0x87,0x9F,0x9F,0x9F,0xC7,0xC7,0xC7,0xDF,0xDF,0xDF,0xFC,0xFC,0xFC  };//+------------------------------------------------------------------+
//| CIFS 类                                                       |
//+------------------------------------------------------------------+
班级CIFS
  {受保护的:
   cIntBMP           m_bmp;
  整数               m_x大小;
  整数               m_y大小;
  乌查尔             m_virtual_screen[];
  双倍的            m_scale;
  双倍的            m_probs[8];民众:
                    ~CIFS()                          { m_bmp.Delete(“bmpimg”,真的); };
  空白              创造(整数x_尺寸,整数y_尺寸,乌查尔山口);
  空白              使成为(双倍的规模,布尔值后退);
  空白              显示BMP(布尔值后退);受保护的:
  空白              VS_准备(整数x_尺寸,整数y_尺寸,乌查尔山口);
  空白              VS_填充(乌查尔山口);
  空白              VS_PutPixel(整数像素,整数py,乌查尔山口);
  乌查尔             VS_GetPixel(整数像素,整数py);
  整数               获取好友颜色(乌查尔指数);
  整数               RGB256(整数,整数克,整数b)常量{返回(r+256*克+65536*b);      }
  空白              准备概率();
  空白              RenderIFSToVirtualScreen();
  空白              VirtualScreenToBMP();
  };//+------------------------------------------------------------------+
//|创建方法                                                   |
//+------------------------------------------------------------------+
空白CIFS::创建(整数x_尺寸,整数y_尺寸,乌查尔上)
  {
   m_bmp.Create(x_size,y_size,col);
   VS_Prepare(x_size,y_size,col);
   准备概率();
  }//+------------------------------------------------------------------+
//|准备虚拟屏幕                                         |
//+------------------------------------------------------------------+
空白CIFS::VS_Prepare(整数x_尺寸,整数y_尺寸,乌查尔上)
  {
   m_x大小=x_大小;
   m_y大小=y_大小;
  数组调整大小(m_虚拟屏幕,m_x尺寸*m_y尺寸);
   VS_Fill(列);
  }//+------------------------------------------------------------------+
//|用指定的颜色填充虚拟屏幕                    |
//+------------------------------------------------------------------+
空白CIFS::VS_Fill(乌查尔上)
  {
  为了整数我=0;我//+------------------------------------------------------------------+
//|返回调色板中的颜色                                   |
//+------------------------------------------------------------------+
整数CIFS::GetPalColor(乌查尔指数)
  {
  整数ind=索引;
  如果(索引<=0) {索引=0;}
  如果(行业>22 号) {索引=22 号;}
  乌查尔r=调色板[3*(索引)];
  乌查尔g=调色板[3*(索引)+1];
  乌查尔b=调色板[3*(索引)+2];
  返回(RGB256(r,g,b));
  }//+------------------------------------------------------------------+
//|在虚拟屏幕上绘制一个像素                              |
//+------------------------------------------------------------------+
空白CIFS::VS_PutPixel(整数像素,整数py,乌查尔上)
  {
  如果(像素<0返回;
  如果(py<0返回;
  如果(px>m_x尺寸)返回;
  如果(py>m_ysize)返回;
    整数pos=m_xsize*py+px;
  如果(位置>=数组大小(m_虚拟_屏幕))返回;
   m_virtual_screen[pos]=col;
  }//+------------------------------------------------------------------+
//|从虚拟屏幕获取像素“颜色”                   |
//+------------------------------------------------------------------+
乌查尔CIFS::VS_GetPixel(整数像素,整数py)
  {
  如果(像素<0返回0);
  如果(py<0返回0);
  如果(px>m_x尺寸)返回0);
  如果(py>m_ysize)返回0);
    整数pos=m_xsize*py+px;
  如果(位置>=数组大小(m_虚拟_屏幕))返回0);
  返回(m_virtual_screen[位置]);
  }//+------------------------------------------------------------------+
//|准备累积概率数组                       |
//+------------------------------------------------------------------+
空白CIFS::准备概率()
  {
  双倍的米=0;
  为了整数我=0;我<数组大小(IFS_p);我++)
     {
      m_probs[i]=IFS_p[i]+m;
      m=m+IFS_p[i];
     }
  }//+------------------------------------------------------------------+
//|将 IFS 设置渲染到虚拟屏幕                        |
//+------------------------------------------------------------------+
空白CIFS::RenderIFSToVirtualScreen()
  {
  双倍的x=0,y=0;
  双倍的x0=0;
  双倍的y0=0;
  单位迭代次数=单位数学轮100000+100*数学包(米尺度,2)));

  为了单位我=0; i<迭代次数;我++)
     {
      双倍的prb=1000*(兰特()/32767.0);

      为了整数k=0; k<数组大小(IFS_p); k++)
        {
        如果(prb<=m_probs[k])
           {
            x = IFS_a[k] * x0 + IFS_b[k] * y0 + IFS_e[k];
            y = IFS_c[k] * x0 + IFS_d[k] * y0 + IFS_f[k];

            整数scX =整数数学轮(m_x尺寸/2+ (x-0)*m_scale));
            整数SCY =整数数学轮(m_y尺寸/2+ (y-5)*m_scale));

            如果(scX>=0&& scX=0&& scY乌查尔c=VS_GetPixel(scX,scY);
              如果(c<255) c=c+1;
               VS_PutPixel(scX,scY,c);
              }
            休息;
           }
         x0 = x;
         y0 = y;
        }
     }
  }//+------------------------------------------------------------------+
//|将虚拟屏幕复制到 BMP                                     |
//+------------------------------------------------------------------+
空白CIFS::VirtualScreenToBMP()
  {
  为了整数我=0;我为了(整数j=0; j乌查尔colind=VS_GetPixel(i,j);
        整数xcol=GetPalColor(colind);
        如果(科林德==0) xcol=0x00;
        //如果(colind==0) xcol=0xFFFFFF;         m_bmp.DrawDot(i,j,xcol);
        }
     }
  }//+------------------------------------------------------------------+
//|在图表上显示 BMP 图像                                     |
//+------------------------------------------------------------------+
空白CIFS::显示BMP(布尔值后)
  {
   m_bmp.保存(“bmpimg”,真的);
   m_bmp.显示(0,0,“bmpimg”,“蕨类”);
  对象集整数0,“蕨类”,OBJPROP_BACK,后退);
  }//+------------------------------------------------------------------+
//|渲染方法                                                   |    
//+------------------------------------------------------------------+
空白CIFS::渲染(双倍的规模,布尔值后)
  {
   m_scale=比例;
   VS_填充(0);
   RenderIFSToVirtualScreen();
   VirtualScreenToBMP();
   显示BMP(后退);
  }静止的 整数网格模式;
CIFS 蕨类植物;整数当前刻度=50;//+------------------------------------------------------------------+
//|专家初始化函数                                   |
//+------------------------------------------------------------------+
空白 初始化时()
  {//-- 获取网格模式   网格模式=整数图表获取整数0,CHART_SHOW_GRID,0));//-- 禁用网格  图表集整数0,CHART_SHOW_GRID,0);//-- 创建bmp   蕨类植物.创建(800,800,0x00);//-- 显示为背景图像   蕨类植物.渲染(当前比例,真的);
  }//+------------------------------------------------------------------+
//|专家去初始化函数                                 |
//+------------------------------------------------------------------+
空白 解初始化常量 整数r)
  {//-- 恢复网格模式  图表集整数0,CHART_SHOW_GRID,网格模式);//-- 删除 Fern 对象  对象删除0,“蕨类”);
 }//+------------------------------------------------------------------+
//|专家 OnChart 事件处理程序                                      |
//+------------------------------------------------------------------+
空白 图表事件常量 整数ID,          // 事件标识符                 常量 长的&l参数,  // long类型的事件参数               常量 双倍的&d参数,// double类型的事件参数               常量 细绳& 垃圾邮件  // string类型的事件参数                )
  {//--- 单击图形对象  如果(id==CHARTEVENT_OBJECT_CLICK)
     {
      打印“单击名称为‘的图形对象上的事件”+sparam+“'”);
      如果(垃圾参数==“蕨类”)
        {
        // 增加比例系数(缩放)         当前刻度=整数(当前规模*1.1);
         蕨类植物.渲染(当前比例,真的);
        }
     }
  }//+------------------------------------------------------------------+

结果是:

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 9. 使用 CIFS 类创建的 Barnsley 蕨类图像

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 10. Barnsley's Fern 的放大区域

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 11. Barnsley's Fern 的放大区域

Creating fractals in MQL5 using the Iterated Function Systems (IFS) - expert for MetaTrader 5

图 12. Barnsley's Fern 的放大区域

1. 存在大量的 IFS 分形弗拉克廷特, 例如:

// 二进制
双倍的IFS_a[3] = {0.5,  0.5,  0.0};双倍的IFS_b[3] = {0.0,  0.0, -0.5};双倍的IFS_c[4] = {0.0,  0.0,  0.5};双倍的IFS_d[4] = {0.5,  0.5,  0.5};双倍的IFS_e[4] = {-2.563477,  2.436544,4.873085};双倍的IFS_f[4] = {-0.000000, -0.000003,7.563492};双倍的IFS_p[4] = {第333章,第333章,第333章};// 珊瑚
双倍的IFS_a[3] = {0.307692,  0.307692,  0.000000};双倍的IFS_b[3] = {-0.531469, -0.076923,  0.54545};双倍的IFS_c[3] = {-0.461538,  0.153846,  0.692308};双倍的IFS_d[3] = {-0.293706, -0.447552, -0.195804};双倍的IFS_e[3] = {5.4019537, -1.295248, -4.893637};双倍的IFS_f[3] = {8.6551754.152990,  7.269794};双倍的IFS_p[3] = {400,150,450};// 水晶
双倍的IFS_a[2] = {0.696970,0.090909};双倍的IFS_b[2] = {-0.481061, -0.443182};双倍的IFS_c[2] = {-0.393939,  0.515152};双倍的IFS_d[2] = {-0.662879, -0.094697};双倍的IFS_e[2] = {2.147003,  4.286558};双倍的IFS_f[2] = {10.310288,  2.925762};双倍的IFS_p[2] = {750,250};// 龙
双倍的IFS_a[2] = {0.824074,  0.088272};双倍的IFS_b[2] = {0.281482,  0.520988};双倍的IFS_c[2] = {-0.212346, -0.463889};双倍的IFS_d[2] = {0.864198, -0.377778};双倍的IFS_e[2] = {-1.882290,  0.785360};双倍的IFS_f[2] = {-0.110607,  8.095795};双倍的IFS_p[2] = {780,220};// 地面
双倍的IFS_a[3] = {0,  0.52,  0};双倍的IFS_b[3] = {-0.5,  0,0.5};双倍的IFS_c[3] = {0.5,   0, -0.5};双倍的IFS_d[3] = {0,  0.5,  0};双倍的IFS_e[3] = {-1.732366, -0.027891,  1.620804};双倍的IFS_f[3] = {3.366182,5.014877,  3.310401};双倍的IFS_p[3] = {第333章,第333章,第333章};// 科赫3
双倍的IFS_a[5] = {0.307692,0.192308,  0.192308,0.307692,0.384615};双倍的IFS_b[5] = {     0,-0.205882,0.205882,        0,0};双倍的IFS_c[5] = {     0,0.653846, -0.653846,        0,        0};双倍的IFS_d[5] = {0.294118,0.088235,0.088235,0.294118, -0.294118};双倍的IFS_e[5] = {4.119164,-0.688840,  0.688840, -4.136530, -0.007718};双倍的IFS_f[5] = {1.604278,5.978916,  5.962514,1.604278,2.941176};双倍的IFS_p[5] = {151,第254章,第254章,151,190};//螺旋
双倍的IFS_a[3] = {0.787879, -0.121212,  0.181818};双倍的IFS_b[3] = {-0.424242,  0.257576, -0.136364};双倍的IFS_c[3] = {0.242424,  0.151515,  0.090909};双倍的IFS_d[3] = {0.859848,  0.053030,  0.181818};双倍的IFS_e[3] = {1.758647,  -6.721654,  6.086107};双倍的IFS_f[3] = {1.408065,   1.377236,  1.568035};双倍的IFS_p[3] = {896,52,52};//漩涡5
双倍的IFS_a[2] = {0.74545, -0.424242};双倍的IFS_b[2] = {-0.459091, -0.065152};双倍的IFS_c[2] = {0.406061, -0.175758};双倍的IFS_d[2] = {0.887121, -0.218182};双倍的IFS_e[2] = {1.460279,  3.809567};双倍的IFS_f[2] = {0.691072,  6.741476};双倍的IFS_p[2] = {920,80};//之字形2
双倍的IFS_a[2] = {-0.632407, -0.036111};双倍的IFS_b[2] = {-0.614815,0.444444};双倍的IFS_c[2] = {-0.545370,0.210185};双倍的IFS_d[2] = {0.659259,0.037037};双倍的IFS_e[2] = {3.840822,2.071081};双倍的IFS_f[2] = {1.282321,8.330552};双倍的IFS_p[2] = {第888章,112};

绘制这些集合。如何使用 IFS 系数找到初始相似变换?

2. 创建您自己的分形集并计算它们的系数(第 2 章)。

3.尝试使用调色板颜色(uchar Palette array),扩展调色板并添加渐变颜色。

4. Barnsley's Fern 的分形(Hausdorf-Bezikovitch)维数如何?是否有使用 IFS 系数计算分形维数的公式*。

5.添加某个区域的缩放,利用鼠标点击坐标信息图表事件:

空白 图表事件常量 整数ID,        // 事件标识符                 常量 长的&l参数,  // long类型的事件参数               常量 双倍的&d参数,// double类型的事件参数               常量 细绳& 垃圾邮件  // string类型的事件参数                )
  {//--- 左键单击  如果(id==CHARTEVENT_CLICK)
     {
      打印“坐标:x=”,l参数,“  y=”,d参数);
     }
  }


结论

我们考虑了使用迭代函数系统创建自相似集的算法。

使用骨形态发生蛋白库极大地简化了图形图像的工作。除了我们使用的 DrawDot(x,y,color) 方法之外,cIntBMP 类还包含许多其他有用的方法。但这是另一个故事了。


附件下载

📎 ifs_sierpinski_gasket.mq5 (3.45 KB)

📎 ifs_fern.mq5 (2.67 KB)

📎 ifs_sierpinski_carpet.mq5 (2.8 KB)

📎 ifs_fractals.mq5 (3.82 KB)

📎 ifs_fern_color.mq5 (9.41 KB)

📎 cintbmp.mqh (78.58 KB)

Source: MQL5 #328

🔐
请登录后参与评论
注册满12小时后评论,即可解锁附件下载
立即登录