尝试使用CImg库(C ++)创建实时数据grapher

时间:2016-02-18 04:10:10

标签: c++ cimg

我是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;
}

问题 画家从右到左慢慢地穿过......而且我不确定如何制作一条平滑的曲线,因此我选择了点...你如何制作一条平滑的曲线?

1 个答案:

答案 0 :(得分:0)

库中已有一些东西,方法CImg<T>::draw_graph(),正如(brielfy)解释的那样: http://cimg.eu/reference/structcimg__library_1_1CImg.html#a2e629aadedc4518001f00333f25bfec8

使用此方法的库提供的示例很少,请参阅文件examples/tutorial.cppexamples/plotter1d.cpp