GNUPlot http://www.gnuplot.info/,是一个非常强大的开源绘图工具,借助一个名为Gnuplot-iostream 接口 http://stahlke.org/dan/gnuplot-iostream/,从 C++ 调用 gnuplot 命令是非常简单的过程。如果有人已经有 gnuplot 绘图经验并且必须使用 C++ 进行编程,那么这个接口非常有用。或者,如果您想创建自己的界面,请参阅中提供的信息here https://www.youtube.com/watch?v=UcYankkrIpw将会非常有用。链接这个接口的过程非常简单,只需在你的系统中安装gnuplot,然后将gnuplot的include目录和lib目录链接到C++ IDE,然后就可以开始了。给出了如何使用 gnuplot-iostream 接口从 C++ 使用 Gnuplot 的示例here http://stahlke.org/dan/gnuplot-iostream/,下面发布了示例示例的摘录。
#include <vector>
#include <cmath>
#include <boost/tuple/tuple.hpp>
#include "gnuplot-iostream.h"
int main() {
Gnuplot gp;
std::vector<boost::tuple<double, double, double, double> > pts_A;
std::vector<double> pts_B_x;
std::vector<double> pts_B_y;
std::vector<double> pts_B_dx;
std::vector<double> pts_B_dy;
for(double alpha=0; alpha<1; alpha+=1.0/24.0) {
double theta = alpha*2.0*3.14159;
pts_A.push_back(boost::make_tuple(
cos(theta),
sin(theta),
-cos(theta)*0.1,
-sin(theta)*0.1
));
pts_B_x .push_back( cos(theta)*0.8);
pts_B_y .push_back( sin(theta)*0.8);
pts_B_dx.push_back( sin(theta)*0.1);
pts_B_dy.push_back(-cos(theta)*0.1);
}
gp << "set xrange [-2:2]\nset yrange [-2:2]\n";
gp << "plot '-' with vectors title 'pts_A', '-' with vectors title 'pts_B'\n";
gp.send1d(pts_A);
gp.send1d(boost::make_tuple(pts_B_x, pts_B_y, pts_B_dx, pts_B_dy));
} // very simple tool right???
MATLAB https://in.mathworks.com/help/matlab/cpp-mex-file-applications.html (Yes, I am not kidding MATLAB can be called from C++) If you are familiar with MATLAB, you can get the same functionality in C++ by calling, functions/toolboxes from MATLAB and vice versa. Since MATLAB is commercial software, first you have to acquire license (this is very costly). If you have an installed MATLAB software, then use the engine.h
file and link the necessary MATLAB library files to C++ IDE, then the process is outright simple. A detailed step-by-step process of linking matlab to visual studio c++ is provided here https://stackoverflow.com/questions/16258815/call-a-matlab-code-from-visual-studio-c and here https://www.youtube.com/watch?v=5pz1fpDE0nw. An example code is given here, an excerpt of an example is given below (source https://www.researchgate.net/profile/Hazim_Tahir/post/How_can_I_convert_MATLAB_code_to_c_c_code/attachment/59d64455c49f478072eacce0/AS%3A273746584113162%401442277679173/download/cmex.pdf)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define BUFSIZE 256
int main()
{
Engine *ep;
mxArray *T = NULL, *result = NULL;
char buffer[BUFSIZE+1];
double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
if (!(ep = engOpen(""))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
T = mxCreateDoubleMatrix(1, 10, mxREAL);
memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
engPutVariable(ep, "T", T);
engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
engEvalString(ep, "plot(T,D);");
engEvalString(ep, "title('Position vs. Time for a falling object');");
engEvalString(ep, "xlabel('Time (seconds)');");
engEvalString(ep, "ylabel('Position (meters)');");
printf("Hit return to continue\n\n");
fgetc(stdin);
printf("Done for Part I.\n");
mxDestroyArray(T);
engEvalString(ep, "close;");
buffer[BUFSIZE] = '\0';
engOutputBuffer(ep, buffer, BUFSIZE);
while (result == NULL) {
char str[BUFSIZE+1];
printf("Enter a MATLAB command to evaluate. This command should\n");
printf("create a variable X. This program will then determine\n");
printf("what kind of variable you created.\n");
printf("For example: X = 1:5\n");
printf(">> ");
fgets(str, BUFSIZE, stdin);
engEvalString(ep, str);
printf("%s", buffer);
printf("\nRetrieving X...\n");
if ((result = engGetVariable(ep,"X")) == NULL)
printf("Oops! You didn't create a variable X.\n\n");
else {
printf("X is class %s\t\n", mxGetClassName(result));
}
}
printf("Done!\n");
mxDestroyArray(result);
engClose(ep);
return EXIT_SUCCESS;
}