MrsSeven7 于 2012.11.05 10:53 提问

2个回答

g989_1314125   2012.11.05 13:09

``````- (NSString *)windDirectionFromDegrees:(float)degrees
{
static NSArray *directions;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Initialize array on first call.
directions = @[@"N", @"NNE", @"NE", @"ENE", @"E", @"ESE", @"SE", @"SSE",
@"S", @"SSW", @"SW", @"WSW", @"W", @"WNW", @"NW", @"NNW"];
});

int i = (degrees + 11.25)/22.5;
return directions[i % 16];
}
``````

degrees 不是负数的情况。

prettYYoyic   2012.11.05 13:48
``````    - (NSString*)windDirectionFromDegrees:(float)degrees {
NSString *hour1WindDirection;
if (348.75 <= Degrees && Degrees <= 360) {
hour1WindDirection = @"N";
} else if (0 <= Degrees && Degrees <= 11.25) {
hour1WindDirection = @"N";
} else if (11.25 < Degrees && Degrees <= 33.75) {
hour1WindDirection = @"NNE";
} else if (33.75 < Degree && Degrees <= 56.25) {
hour1WindDirection = @"NE";
} else if (56.25 < Degrees && Degrees <= 78.75) {
hour1WindDirection = @"ENE";
} else if (78.75 < Degrees && Degrees <= 101.25) {
hour1WindDirection = @"E";
} else if (101.25 < Degrees && Degrees <= 123.75) {
hour1WindDirection = @"ESE";
} else if (123.75 < Degrees && Degrees <= 146.25) {
hour1WindDirection = @"SE";
} else if (146.25 < Degrees && Degrees <= 168.75) {
hour1WindDirection = @"SSE";
} else if (168.75 < Degrees && Degrees <= 191.25) {
hour1WindDirection = @"S";
} else if (191.25 < Degrees && Degrees <= 213.75) {
hour1WindDirection = @"SSW";
} else if (213.75 < Degrees && Degrees <= 236.25) {
hour1WindDirection = @"SW";
} else if (236.25 < Degrees && Degrees <= 258.75) {
hour1WindDirection = @"WSW";
} else if (258.75 < Degrees && Degrees <= 281.25) {
hour1WindDirection = @"W";
} else if (281.25 < Degrees && Degrees <= 303.75) {
hour1WindDirection = @"WNW";
} else if (303.75 < Degrees && Degrees <= 326.25) {
hour1WindDirection = @"NW";
} else if (326.25 < Degrees && Degrees < 348.75) {
hour1WindDirection = @"NNW";
} else {
hour1WindDirection = nil;
}
return hour1WindDirection;
}
``````

``````NSString *hour1WindDirection = [self windDirectionFromDegrees:hour1WindD];
``````