首页 > 解决方案 > 无法生成 Mandelbrot 矩阵以将其保存到 C++ 线程中的文件

问题描述

我从我的 c++ 线程开始,不了解一些基本的东西。这是 Mandelbrot 示例,它生成分形图像。这不是我的代码,我只是做了一些更改(这里是原文:https://rosettacode.org/wiki/Mandelbrot_set#PPM_non_interactive

我有这个函数可以生成带有颜色的矩阵以保存到文件:

vector<unsigned char *> drawMandelbrot()
{
   /* screen ( integer) coordinate */
   int iX, iY;

   double Cx, Cy;
   const double CxMin = -2.5;
   const double CxMax = 1.5;
   const double CyMin = -2.0;
   const double CyMax = 2.0;
   double PixelWidth = (CxMax - CxMin) / iXmax;
   double PixelHeight = (CyMax - CyMin) / iYmax;

   int Index = 0;

   const int IterationMax = 200;
   unsigned char color[3];
   vector<unsigned char *> rows(MaxIndex);
   double Zx, Zy;
   double Zx2, Zy2;
   int Iteration;
   const double EscapeRadius = 2;
   double ER2 = EscapeRadius * EscapeRadius;

   for (iY = 0; iY < iYmax; iY++)
   {
      Cy = CyMin + iY * PixelHeight;
      if (fabs(Cy) < PixelHeight / 2)
         Cy = 0.0; /* Main antenna */
      for (iX = 0; iX < iXmax; iX++)
      {
         Cx = CxMin + iX * PixelWidth;
         /* initial value of orbit = critical point Z= 0 */
         Zx = 0.0;
         Zy = 0.0;
         Zx2 = Zx * Zx;
         Zy2 = Zy * Zy;
         /* */
         for (Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
         {
            Zy = 2 * Zx * Zy + Cy;
            Zx = Zx2 - Zy2 + Cx;
            Zx2 = Zx * Zx;
            Zy2 = Zy * Zy;
         };
         /* compute  pixel color (24 bit = 3 bytes) */
         if (Iteration == IterationMax)
         { /*  interior of Mandelbrot set = black */
            color[0] = 0;
            color[1] = 0;
            color[2] = 0;
         }
         else
         {                  /* exterior of Mandelbrot set = white */
            color[0] = 255; /* Red*/
            color[1] = 255; /* Green */
            color[2] = 255; /* Blue */
         };

         rows[Index] = color;
         Index++;
      }
   }
   return rows;
}

这是将其保存到文件的功能:

void saveToFile(vector<unsigned char *> matrix, char *filename)
{
   char *comment = (char *)"# "; /* comment should start with # */
   FILE *file;
   file = fopen(filename, "wb"); /* b -  binary mode */
   fprintf(file, "P6\n %s\n %d\n %d\n %d\n", comment, iXmax, iYmax, MaxColorComponentValue);

   for (int Index = 0; Index < MaxIndex; Index++)
   {
      fwrite(matrix[Index], 1, 3, file);
   }

   fclose(file);
}

一些全局值和主循环:

const int iXmax = 1000;
const int iYmax = 1000;
const int MaxColorComponentValue = 255;
int const MaxIndex = (iXmax * iYmax) - 1;

int main()
{
   clock_t start = clock();
   vector<unsigned char *> image = drawMandelbrot();
   clock_t stop = clock();
   cout << (double(stop - start) / CLOCKS_PER_SEC) << " seconds\n";

   char *filename = (char *)"new2.ppm";
   saveToFile(image,filename);
   return 0;
}

问题是 generateMandelbrot() 返回这样的矩阵: 图像矩阵

但它应该是元素的向量,看起来像这样,实际上是颜色值: color char

我知道问题在于颜色和图像值类型,但不知道它应该是什么样子。谢谢!

标签: c++arrays

解决方案


这个:

     rows[Index] = color;

每次都将unsigned char *您分配给同一个数组!vector

换句话说,就像我卖给你十辆汽车并交付钥匙,但它们都是同一辆车的相同钥匙。你不会心烦意乱吗?

更改要使用的变量std::array

using Color = std::array<unsigned char, 3>;
Color color;
vector<Color> rows(MaxIndex);

现在你有一个三元组(颜色)向量,而不是一个指针向量,它们都指向同一个三元组。


推荐阅读