如何根据像素/高度确定线图的比例?

时间:2010-05-03 02:06:13

标签: c++ math graph

由于我糟糕的数学能力,我遇到了一个问题,我无法弄清楚如何根据最大值和最小值来缩放图形,以便整个图形适合图形区域(400x420)而不需要它的一部分离开屏幕(根据用户给定的等式)。

假设我有这个代码,它会自动绘制正方形,然后根据这些值绘制折线图。什么是公式(我可以乘以什么)来缩放它以使其适合小图形区域?

vector<int> m_x;
vector<int> m_y; // gets automatically filled by user equation or values

int HeightInPixels = 420;// Graphing area size!!
int WidthInPixels = 400;
int best_max_y = GetMaxOfVector(m_y);
int best_min_y = GetMinOfVector(m_y);
m_row = 0;
m_col = 0;

y_magnitude = (HeightInPixels/(best_max_y+best_min_y)); // probably won't work
x_magnitude = (WidthInPixels/(int)m_x.size());
m_col = m_row = best_max_y; // number of vertical/horizontal lines to draw

////x_magnitude = (WidthInPixels/(int)m_x.size())/2; Doesn't work well
////y_magnitude = (HeightInPixels/(int)m_y.size())/2; Doesn't work well

ready = true; // we have values, graph it
Invalidate(); // uses WM_PAINT

////////////////////////////////////////////
/// Construction of Graph layout on WM_PAINT, before painting line graph
///////////////////////////////////////////
CPen pSilver(PS_SOLID, 1, RGB(150, 150, 150) ); // silver
    CPen pDarkSilver(PS_SOLID, 2, RGB(120, 120, 120) ); // dark silver
    dc.SelectObject( pSilver ); // silver color
    CPoint pt( 620, 620 ); // origin
    int left_side = 310;
    int top_side = 30;
    int bottom_side = 450;
    int right_side = 710; // create a rectangle border
    dc.Rectangle(left_side,top_side,right_side,bottom_side);
    int origin = 310;
    int xshift = 30;
    int yshift = 30;
    // draw scaled rows and columns
    for(int r = 1; r <= colrow; r++){ // draw rows
        pt.x = left_side;
        pt.y = (ymagnitude)*r+top_side;
        dc.MoveTo( pt );
        pt.x = right_side;
        dc.LineTo( pt );
        for(int c = 1; c <= colrow; c++){
            pt.x = left_side+c*(magnitude);
            pt.y = top_side;
            dc.MoveTo(pt);
            pt.y = bottom_side;
            dc.LineTo(pt);
        } // draw columns
    }
    // grab the center of the graph on x and y dimension
    int top_center = ((right_side-left_side)/2)+left_side;
    int bottom_center = ((bottom_side-top_side)/2)+top_side;

2 个答案:

答案 0 :(得分:0)

我所做的是确定我在x和y方向上有多少点,然后将其除以x和y维度,然后将其除以3,因为我希望每个最小点为三个像素,所以可以看出来。

然后诀窍是你必须聚合数据,以便用一个点显示几个点,所以它可能是它们的平均值,但这取决于你显示的是什么。

如果不了解您正在做的事情,很难提出建议。

对于这部分,减去,不要添加: best_max_y+best_min_y因为你想要区别。

唯一的另一件事就是将y_magnitudex_magnitude除以3.这是我提出的任意数字,只是因此用户可以看到积分,你可能会找到其他一些数字更好地工作。

答案 1 :(得分:0)

您使用的是 x ^ 2 + b x + c(quadratic equation)。您将获得用户插入的(X,Y)值列表 让我们说你得到的5分是 (1,1) (2,4) (4,1) (5,6) (6,7)

所以,这里你的best_max_y将是7,best_min_y将是1.
现在您的总图表区域为
Dx = right_side - left_side //here, 400 (710 - 310)
Dy = bottom_side - top_side //here, 420 (450 - 30)

因此,您可以使用以下等式计算x_magnitude和y_magnitude:

x_magnitude = WidthInPixels / Dx;
y_magnitude = HeightInPixels / Dy;