2017-05-31 168 views
-2

Y轴的曲线图将被自动缩放,这取决于未来休耕函数的值。创建C++视觉工作室

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 

回答

0

不知道我得到了你问什么,但看起来像你想创建以下就像个采样点的表连续函数:

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视图比例...

欲了解更多信息,请参阅: