How to calculate the y-pixels of someones weight on a graph? (math+programming question)
- by RexOnRoids
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
{
float graphMaxY = 400; //The TOP of the graph
float graphMinY = 100; //The BOTTOM of the graph
float yOffset = 100; //Graph itself is offset 100 pixels in the Y direction
float coordDiff = graphMaxY-graphMinY; //The size in pixels of the graph
float weightDiff = self.highestWeight-self.lowestWeight; //The weight gap
float pixelIncrement = coordDiff/weightDiff;
float weightY = (theWeight*pixelIncrement)-(coordDiff-yOffset); //The return value
return weightYpixel;
}