風の角度(つまり、180度がS(南の場合)に変わる)がCardinal Wind Direction(N、NNE、SSE、SEなど)に変換されるObjective Cのサンプルコードを持っている人はいますか?
質問する
4784 次
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)のみが必要であると判断した場合は、関数の他の部分を変更せずに、3 文字の方向を削除できます。
同様に、32 巻すべてが必要な場合は、関数の他の部分を変更せずに 1/4 巻 (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 に答える