c++ - 无法生成 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
我知道问题在于颜色和图像值类型,但不知道它应该是什么样子。谢谢!
解决方案
这个:
rows[Index] = color;
每次都将unsigned char *
您分配给同一个数组!vector
换句话说,就像我卖给你十辆汽车并交付钥匙,但它们都是同一辆车的相同钥匙。你不会心烦意乱吗?
更改要使用的变量std::array
:
using Color = std::array<unsigned char, 3>;
Color color;
vector<Color> rows(MaxIndex);
现在你有一个三元组(颜色)向量,而不是一个指针向量,它们都指向同一个三元组。
推荐阅读
- java - 为什么我的 JUnit 测试从 Android Studio 运行但从 CLI 失败?
- python - 运行从中断处开始的脚本 - Python
- java - Java 接口设计:我应该把所有子类都使用的大量重复代码放在哪里?
- python - PANDAS Quantiles 不同的答案取决于我们如何提问
- python - 如何为开始游戏按钮创建一个功能以禁用开始屏幕并加载我的主游戏?
- bash - 如何在给定 HISTFILE 路径的非交互式 shell 中查看历史输出?
- python - matplotlib 不显示轴标题和轴名称
- kendo-ui - 剑道网格复选框获取最后选择的值
- sql - PostgreSQL 数据库将脚本视为 ANSI 而不是 UTF8
- sql-server-2016 - SQL - 选择最后一个和上一个与最后一个不同