I'm not that smart like some of you geniuses. I need some help from a math whiz.
My app draws a graph of the users weight over time. I need a surefire way to always get the right pixel position to draw the weight point at for a given weight.
For example, say I want to plot the weight 80.0(kg) on the graph when the range of weights is 80.0 to 40.0kg. I want to be able to plug in the weight (given I know the highest and lowest weights in the range also) and get the pixel result 400(y) (for the top of the graph). The graph is 300 pixels high (starts at 100 and ends at 400). The highest weight 80kg would be plot at 400 while the lowest weight 40kg would be plot at 100. And the intermediate weights should be plotted appropriately.
I tried this but it does not work:
-(float)weightToPixel:(float)theWeight
{
//Weight Stuff
float highestWeight = 95.0; //The highest weight in the set
float lowestWeight = 60.0; //The lowest weight in the set
float weightDiff = highestWeight-lowestWeight; //The weight diff
//Graph Stuff
float graphMaxY = 400; //The end point of the Y-axis (TOP of graph)
float graphMinY = 100; //The start point of the Y-axis (BOTTOM of graph)
float coordDiff = graphMaxY-graphMinY; //The size in pixels of the graph Y-axis
//Calculations
float pixelIncrement = coordDiff/weightDiff;
float weightY = (theWeight*pixelIncrement)+graphMinY; //The return value
/*
For example, assuming theWeight is of value 95.0 (same as highest weight)
the calculation SHOULD return the value 400 as the Y pixel of theWeight
which should plot theWeight on the TOP of the graph Y-axis.
But it doesn't. I keep getting 538.46
*/
return weightYpixel;
}