我有一個問題,由於我的可怕的數學能力,我無法弄清楚如何根據最大值和最小值對圖進行縮放,以便整個圖可以適合圖表區(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;
我正在做一個簡單的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
您可能希望添加此信息作爲您的問題的更新,它可能會非常有用。我正在準備工作,如果今天沒有更好的答案,我將在今晚看到關於回答。 – 2010-05-03 10:17:10