2010-05-03 35 views
1

我有一個問題,由於我的可怕的數學能力,我無法弄清楚如何根據最大值和最小值對圖進行縮放,以便整個圖可以適合圖表區(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; 

回答

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;

0

我所做的是確定我有多少分,在X和Y方向去,然後除以x和y的尺寸,然後通過3分,因爲我想每個最小點是三個像素,所以可以看到。

接下來的訣竅就是你必須對數據進行聚合,以便用一個點顯示幾個點,所以它可能是它們的平均值,但這取決於你顯示的內容。

不知道更多關於你在做什麼很難提出建議。

對於這部分,減去,不要加上: best_max_y+best_min_y你想要的差異。

唯一的另一件事情就是將y_magnitudex_magnitude除以3.這是一個我想出的任意數字,爲了讓用戶能夠看到點數,您可能會發現其他一些數字可以更好地工作。

+0

我正在做一個簡單的a *的x^2 + b * X + C方程曲線圖。用戶只能更改a,b,c。我將x設置爲從-10到10的20個數據點。 我還允許用戶輸入手動數據點。 對我來說,y_magnitude決定了行之間的像素數量。 (int r = 1; r <= colrow; r ++){//繪製行 \t \t pt.x = left_side; \t \t pt.y =(ymagnitude)* r + top_side; \t \t dc.MoveTo(pt); \t \t pt.x = right_side; \t \t dc.LineTo(pt); (((「colrow」剛好等於m_col和m_row。而你的#點/高/ 3不工作的建議,它出來的分數數量,20/420/3))) – Dexter 2010-05-03 02:22:52

+0

您可能希望添加此信息作爲您的問題的更新,它可能會非常有用。我正在準備工作,如果今天沒有更好的答案,我將在今晚看到關於回答。 – 2010-05-03 10:17:10

相關問題