2 mrsseven7 MrsSeven7 于 2012.11.05 10:53 提问

将风向的角度转化为基本风向

有没有人能提一些代码,可以把风向的角度(比如从180度到South南方)转变为基本风向显示:(N,NNE,SSE,SE等等)。

2个回答

g989_1314125
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
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];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!