0
我試圖在規模來計算基於距離我的用戶抽獎。計算距離Forumla同時使用的秤
我的用戶可以通過畫線網格上捕捉到其他點在遊戲中。
網格是數值,它們可以選擇8,16,24,或32
規模可以由用戶選擇的一個整數(1-10)和部分被改變數字(0,0.5,0.75或0.125)。
用戶可以選擇以度量或經驗單位顯示距離。
一旦刻度被改變,我就很難吐出距離。
任何人都可以告訴我我的數學出錯了嗎?
- (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;
}
提示:使用''的'hypot(x,y)'而不是'sqrt(x * x + y * y)'。 –
2013-05-08 20:15:12
@ H2CO3我對Objective-C不熟悉;用'hypot(x,y)'代替'sqrt(x * x + y * x)'的優點是什麼? – andand 2013-05-08 20:31:36
@andand可讀性和精確性。 (它只是普通的C) – 2013-05-08 21:02:25