gpt4 book ai didi

iphone - 如何计算图表上某人体重的 y 像素? (数学+编程题)

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:34:56 26 4
gpt4 key购买 nike

我不像你们中的一些天才那么聪明。我需要数学高手的帮助。

我的应用绘制了用户体重随时间变化的图表。我需要一种万无一失的方法来始终获得正确的像素位置来绘制给定重量的重量点。

例如,当体重范围为 80.0 到 40.0 公斤时,假设我想在图表上绘制体重 80.0(公斤)。我希望能够插入权重(假设我也知道该范围内的最高和最低权重)并获得像素结果 400(y)(对于图表的顶部)。该图高 300 像素(从 100 开始到 400 结束)。体重最高的80kg画在400,体重最低的40kg画在100,中间的体重要适当画。

我试过了,但是没用:

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

}

最佳答案

您的大部分内容都是正确的。但是为了计算实际重量 theWeight 的像素,您必须减去应该绘制的最小重量。如果重量在绘制的范围内,您可能必须测试重量,如果不在范围内则使用react。像这样:

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

}

关于iphone - 如何计算图表上某人体重的 y 像素? (数学+编程题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2912142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com