gpt4 book ai didi

c++ - 使用 C++ 中的 gnuplot 绘制几何图形

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:44:50 24 4
gpt4 key购买 nike

我用 C++ 编写了一段代码,它根据给定的节点坐标(彼此足够接近的点相连)找到几何图形的边。我现在想绘制图形,将链接的坐标传递给 Gnuplot。这看起来像这样:

FILE *pipe = popen("gnuplot -persist", "w");
fprintf(pipe, "\n");
fprintf(pipe, "set polar\n");
fprintf(pipe, "plot '-' with linespoints \n");

现在每次找到链接时(使用广度优先搜索算法),我都会这样做

fprintf(pipe, "%g %g\n", RC[j].angle,RC[j].radius);
fprintf(pipe, "%g %g\n", RC[start[ptr]].angle,RC[start[ptr]].radius);
fprintf(pipe, "%g %g\n");

当搜索算法完成时,我以

结束
fprintf(pipe, "e\n");
fflush(pipe);
fclose(pipe);

然而,这并没有打印出我期望的几何图形(远离的点原来是相连的)。如果我使用此过程将数据写入文件

outfile << RC[j].angle << ", " << RC[j].radius << endl;
outfile << RC[start[ptr]].angle << ", " << RC[start[ptr]].radius << endl;
outfile << " " << endl;

我确实得到了我想要的图表。管道有什么问题?

最佳答案

从你的 std::ostreamfprintf 版本,我可以看出区别很明显:你忘记了 中的逗号 (,) + 空行fprintf 版本:

fprintf(pipe, "%g, %g\n", RC[j].angle, RC[j].radius);
fprintf(pipe, "%g, %g\n", RC[start[ptr]].angle, RC[start[ptr]].radius);
fprintf(pipe, " \n");

作为替代方案,我建议使用 C++ 类而不是旧的 C 函数(未经测试):

class Pipe : public std::ofstream // in most cases this is bad, but ok here
{
public:
Pipe( FILE* pf ) : _pg(pf) {}
~Pipe() { pclose(pf); }
// copy-ctr(), op=(), ...
private:
FILE _pf;
};

Pipe gnuplot(popen("gnuplot -persist", "w"));
gnuplot << "\n"
<< "set polar\n"
<< "plot '-' with linespoints\n";

for (...)
{
gnuplot << RC[j].angle << ", " << RC[j].radius << "\n";
gnuplot << RC[start[ptr]].angle << ", " << RC[start[ptr]].radius << "\n";
gnuplot << " " << std::endl;
}

关于c++ - 使用 C++ 中的 gnuplot 绘制几何图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41015327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com