How to determine Scale of Line Graph based on Pixels/Height?

Posted by Dexter on Stack Overflow See other posts from Stack Overflow or by Dexter
Published on 2010-05-03T02:06:13Z Indexed on 2010/05/03 2:08 UTC
Read the original article Hit count: 278

Filed under:
|
|

I have a problem due to my terrible math abilities, that I cannot figure out how to scale a graph based on the maximum and minimum values so that the whole graph will fit onto the graph-area (400x420) without parts of it being off the screen (based on a given equation by user).

Let's say I have this code, and it automatically draws squares and then the line graph based on these values. What is the formula (what do I multiply) to scale it so that it fits into the small graphing area?

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
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

© Stack Overflow or respective owner

Related posts about graph

Related posts about math