3

有没有人在目标 C 中有一些示例代码,其中风度(即 180 度变成 S(代表南方))转换为主要风向(N、NNE、SSE、SE 等)?

4

5 回答 5

18

关于什么

- (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不是负面的。)

于 2012-11-04T16:55:38.593 回答
3

斯威夫特 4

更新 Martin R 的回答:

    func windDirectionFromDegrees(degrees : Float) -> String {

    let directions = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]
    let i: Int = Int((degrees + 11.25)/22.5)
    return directions[i % 16]
}
于 2018-04-11T18:21:05.053 回答
2

这是一个例子:

    - (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]; //hour1WindD is a integer!
于 2012-11-04T16:20:35.780 回答
2

我会这样做:

static NSString *windDirectionForDegrees(double degrees) {
    static NSString *const Directions[] = {
        @"N", @"NNE",  @"NE", @"ENE", @"E", @"ESE", @"SE", @"SSE",
        @"S", @"SSW", @"SW", @"WSW", @"W", @"WNW", @"NW", @"NNW"
    };
    static const int DirectionsCount = sizeof Directions / sizeof *Directions;

    int wind = remainder(round((degrees / 360) * DirectionsCount), DirectionsCount);
    if (wind < 0) wind += DirectionsCount;
    return Directions[wind];
}

如果您决定只需要主风(S、NE、E、SE、S、SW、W 和 NW),则可以删除三个字母的方向,而无需更改函数的任何其他部分。

同样,如果您决定要全部 32 个风,则可以插入四分之一风(NbE、NEbE 等),而无需更改函数的任何其他部分。

于 2012-11-04T16:58:10.717 回答
0

这是 Swift 4 的示例

if 348.75 <= degrees, degrees <= 360 {
  windDirection = "N"
} else if 0 <= degrees,degrees <= 11.25 {
  windDirection = "N"
} else if 11.25 < degrees, degrees <= 33.75 {
  windDirection = "NNE"
} else if 33.75 < degrees, degrees <= 56.25 {
  windDirection = "NE"
} else if 56.25 < degrees, degrees <= 78.75 {
  windDirection = "ENE"
} else if 78.75 < degrees, degrees <= 101.25 {
  windDirection = "E"
} else if 101.25 < degrees, degrees <= 123.75 {
  windDirection = "ESE"
} else if 123.75 < degrees, degrees <= 146.25 {
  windDirection = "SE"
} else if 146.25 < degrees, degrees <= 168.75 {
  windDirection = "SSE"
} else if 168.75 < degrees, degrees <= 191.25 {
  windDirection = "S"
} else if 191.25 < degrees, degrees <= 213.75 {
  windDirection = "SSW"
} else if 213.75 < degrees, degrees <= 236.25 {
  windDirection = "SW"
} else if 236.25 < degrees, degrees <= 258.75 {
  windDirection = "WSW"
} else if 258.75 < degrees, degrees <= 281.25 {
  windDirection = "W"
} else if 281.25 < degrees, degrees <= 303.75 {
  windDirection = "WNW"
} else if 303.75 < degrees, degrees <= 326.25 {
  windDirection = "NW"
} else if 326.25 < degrees, degrees < 348.75 {
  windDirection = "NNW"
}
于 2018-03-25T14:14:07.137 回答