【问题标题】:Create a plot graphs with c++ visual studio使用 c++ Visual Studio 创建绘图图
【发布时间】:2017-05-31 06:22:31
【问题描述】:

Y 轴将自动缩放,具体取决于后续函数的值。

void mouseHandleCordinate(double val){
  // include graph function.
}

所以我想根据时间创建图表。 X 轴代表时间,Y 代表函数上面的值。我如何创建上面的图形函数。

始终将数据传递给void mouseHandleCordinate(double val) 函数。 例如:

val >>> 2.1,3,1,6,7,5.5,0,9,5,6,7,3.6,2,5,6,7,8,1,2,3,4 >> represent double val
Time>>> 21,20,19,18,17,......., 4,3,2,1 second

【问题讨论】:

    标签: c++ visual-studio visual-studio-2015


    【解决方案1】:

    不确定我得到了你的要求,但看起来你想创建如下采样点表的连续函数,例如:

    const int N=21; // number of samples
    const double t0=21.0,t1=1.0; // start,end times
    const double val[N]={ 2.1,3,1,6,7,5.5,0,9,5,6,7,3.6,2,5,6,7,8,1,2,3,4 };
    
    double f(double t) // nearest
     {
     t = (t-t0)/(t1-t0); // time scaled to <0,1>
     t*= (N-1); // time scaled to <0,N) .. index in table
     int ix=t; // convert to closest index in val[]
     if ((ix<0)||(ix>=N)) return 0.0; // handle undefined times
     return val[ix]; // return closest point in val[]
     }
    

    这将为您提供最近邻样式的函数值。如果您需要更好的东西,请使用线性或三次或更好的插值,例如:

    double f(double t) // linear
     {
     t = (t-t0)/(t1-t0); // time scaled to <0,1>
     t*= (N-1); // time scaled to <0,N) .. index in table
     int ix=t; // convert to closest index in val[]
     if ((ix<0)||(ix>=N)) return 0.0; // handle undefined times
     if (ix==N-1) return val[ix]; // return closest point in val[] if on edge
     // linear interpolation
     t = t-floor(t); // distance of time between ix and ix+1 points scaled to <0,1>
     return val[ix]+(val[ix+1]-val[ix])*t; // return linear interpolated value
     }
    

    现在解决您的问题:

    void mouseHandleCordinate(double mx) // mouse x coordinate in [pixels] I assume
     {
     double t,x,y,x0,y0
    
     // plot
     x=0;
     y=f(view_start_time)
     for (t=view_start_time;t<=view_end_time;t+=(view_end_time-view_start_time)/view_size_in_pixels,x0=x,x++)
      {
      y0=y; y=f(t); 
      // render line x0,y0,x,y
      }
    
     // mouse highlight
     t = view_start_time+((view_end_time-view_start_time)*mx/view_size_in_pixels);
     x = mx;
     y = f(t);
     // render point x,y ... for example with circle r = 16 pixels
     }
    

    在哪里:
    view_start_time 是绘图视图中最左侧像素的时间
    view_end_time 是绘图视图中最右侧像素的时间
    view_size_in_pixels 是绘图视图的 x 分辨率(以 [像素] 为单位)

    [备注]

    我直接在 SO 编辑器中编码,所以可能有错别字... 希望您在某些 Paint 事件中调用 mouseHandleCordinate,而不是在鼠标移动时调用,这应该只安排重绘顺序...此外,您应该以与我使用的 x 比例类似的方式添加 y 视图比例...

    更多信息见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      相关资源
      最近更新 更多