views:

92

answers:

3

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;

}
A: 

You can get rid of yOffset and change your penultimate line to

float weightYpixel = (theWeight*pixelIncrement)+graphMinY;
mtrw
A: 

You have to fix the last line:

float weightY = (theWeight*pixelIncrement)+yOffset; 
tkr
Thanks for the answer but...Seems like it does not work. I tried plugging in the weight 95kg (as 95.0) for an imaginary weight set ranging from 95kg to 30kg. Tried pluging it into the graph on the Y-axis which is 300pixels high (from pixel (0,100) to pixel (0,400) and I keep getting 538.461 (too high) when it should be 400 since it is the highest weight in the range.
RexOnRoids
yeah, there is still a fault. You have to remove the offset from you actual weight value.float weightY = ((theWeight-lowestWeight)*pixelIncrement)+yOffset;
tkr
Thanks TKR you saved me
RexOnRoids
+1  A: 

Most of what you had was correct. But in order to calculate the pixels for the actual weigh theWeight, you have to subtract the minimal weight that should be plotted. You might have to test theWeight if it is in the range plotted and react if not. Something like this:

-(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
    if (theWeight > highestWeight) { 
       weightYpix = graphMaxY; }
    else if (theWeight < lowestWeight) { 
       weightYpix = graphMinY; }
    else {
       float pixelIncrement = coordDiff/weightDiff; 
       float weightYpix = ((theWeight-lowestWeight)*pixelIncrement)+graphMinY; //The return value
    }

    return weightYpix;

}
Ralph Rickenbach