我是CImg的新手。不确定库中是否已经有一个实时数据绘图仪,但我想我会自己制作一个。如果我正在寻找的内容已经存在于库中,请指出我该函数。否则,这是我的超级低效代码,希望你能帮到我~
#include <iostream>
#include "CImg.h"
#include <ctime>
#include <cmath>
using namespace cimg_library;
int main()
{
CImg<unsigned char> plot(400, 320, 1, 3, 0);
CImgDisplay graph(plot, "f(x)");
clock();
const unsigned char red[] = {255, 0, 0};
float* G = new float[plot.width()]; //define an array holding the values that are to be displayed on the graph
while (1){
G[0] = ((plot.height()/4) * sin(clock() / 1000.0)) + plot.height()/2; // new f(t) value
for (int i = 1; i <= plot.width() - 1; i++){
G[plot.width() - i] = G[plot.width() - i - 1]; //basically shift all the array values to current address+1
plot.draw_point(plot.width() - 3*i, G[i-1], red, 1).display(graph);
}
plot.fill(0);
}
return 0;
}
问题绘图员从右到左移动的速度如此缓慢。而且我不确定如何制作平滑曲线,因此我选择了点。你如何制作平滑的曲线?
库中已经有一些适合您的方法,方法 CImg<T>::draw_graph()
,正如 (brielfy) 在这里解释的那样:http://cimg.eu/reference/structcimg__library_1_1CImg.html#a2e629aadedc4518001f00333f25bfec8
使用此方法的库提供了几个示例,请参阅文件examples/tutorial.cpp
和examples/plotter1d.cpp
。