Ios 使用比例时计算距离公式
我试图根据用户绘制的比例计算距离 我的用户可以通过在网格上画一条线捕捉到游戏中的其他点 栅格是一个数值,他们可以选择8、16、24或32 用户可以通过选择整数1-10和分数0、0.5、0.75或0.125来更改刻度 用户可以选择以公制或经验单位显示距离 一旦刻度改变,我很难说出距离 谁能告诉我我的数学哪里出了错Ios 使用比例时计算距离公式,ios,objective-c,math,formula,Ios,Objective C,Math,Formula,我试图根据用户绘制的比例计算距离 我的用户可以通过在网格上画一条线捕捉到游戏中的其他点 栅格是一个数值,他们可以选择8、16、24或32 用户可以通过选择整数1-10和分数0、0.5、0.75或0.125来更改刻度 用户可以选择以公制或经验单位显示距离 一旦刻度改变,我很难说出距离 谁能告诉我我的数学哪里出了错 - (double) distanceFormula : (float) x1 : (float) y1 : (float) x2 : (float) y2 { // 1 met
- (double) distanceFormula : (float) x1 : (float) y1 : (float) x2 : (float) y2 {
// 1 meter * 3.280839895 feet => feet
// 1 foot * 1 meter/3.280839895 feet => meter
/* Use Pythagora's theorem to calculate distance */
double dx = (x2-x1);
double dy = (y2-y1);
double dist = sqrt(dx*dx + dy*dy);
NSLog(@"Raw Distance %f", dist) ;
return dist;
}
- (NSString *) returnDistanceAsString : (double) distance {
NSString * string;
double d = distance / [self returnGridSize];
double scale = [self returnScaleWhole] + [self returnScaleSub];
if ([self returnUseMetric]) {
//METRIC
int tempCentim = (d * kCMConst) / 2;
if (tempCentim < 1) {
string = [NSString stringWithFormat:@"%d mm", tempCentim];
} else if (tempCentim > 1) {
string = [NSString stringWithFormat:@"%d mm", tempCentim];
} else if (tempCentim > 100) {
//eventually going to add cm mm
}
} else {
//EMPERICAL
int RL = d * scale;
int feet = RL / 12.0;
int inches = (int)RL % 12;
string = [NSString stringWithFormat:@"%i' %i\"", feet, inches];
}
return string;
}
我注意到的主要问题是,在公制中,你忽略了乘以比例,但是你在乘以kCMConst/2。您没有解释kCMConst是什么,但它似乎不太可能是需要除以2的值。提示:使用hypotx,y from代替sqrtx*x+y*y。@H2CO3我不熟悉Objective-C;使用hypotx,y代替sqrtx*x+y*x有什么好处?@and以及可读性和精度。这只是简单的C。我希望你们现在加上的两个刻度值会相乘。此外,公制也不使用刻度。