Windrichtung bestimmen (Selektionen)
Im Kreis einer Windrose werden die Himmelsrichtungen durch die vier Hauptrichtungen Nord (N), Ost (O), Süd (S) und West (W) bestimmt.
Diese Viererteilung kann durch eine Halbierung der Winkel, also eine Einteilung in 45°-Abteilungen präzisiert werden. Die so erhaltenen 8 Richtungen sind N, NO, O, SO, S, SW, W, NW.
Diese wiederum halbiert, teilen den Kreis in Abteilungen von 22½°- ein, 16 an der Zahl: N, NNO, NO, ONO, O, OSO, SO, SSO, S, SSW, SW, WSW, W, WNW, NW, NNW.
Auf diese Weise ein weiteres Mal präzisiert, erhält man insgesamt 32 Windrichtungen als Teilung des Kreises in Winkeln von 11¼°, also N, NzO (sprich: Nord zu Ost), NNO, NOzN, NO, NOzO, ONO, OzN, O, OzS, OSO, SOzO, SO, SOzS, SSO, SzO, S, SzW, SW, SWzW, WSW, WzS, W, WzN, WNW, NWzW, NW, NWzN, NNW, NzW.
Ihre Aufgabe:
Verfassen Sie einen Algorithmus, der eine Gradzahl und eine Kreiseinteilung (in 4, 8, 16 oder 32 Abschnitte) entgegennimmt, und die entsprechende Himmelsrichtung zurückgibt. Ist keine Kreisteilung angegeben, soll als Standard die Einteilung in 16 Teilungen verwendet werden.
0 Kommentare
8 Lösung(en)
public class Main {
public static List<String> nosw = new ArrayList<String>();
public static Scanner sc;
public static void main(String[] args) {
addRichtungen();
getWindrichtung();
}
public static void addRichtungen() {
// 4 Himmelsrichtungen
nosw.add("0°4°N");
nosw.add("90°4°O");
nosw.add("180°4°S");
nosw.add("270°4°W");
nosw.add("360°4°N");
for (int i = 0; i < 4; i++) {
// GetDatas
String[] s = nosw.get(i).split("°");
int grad = Integer.parseInt(s[0]);
String now = s[2];
String next = "";
if (i == 3) {
next = nosw.get(0).split("°")[2];
} else {
next = nosw.get(i + 1).split("°")[2];
}
// 8 Himmelsrichtungen
nosw.add((grad + 45) + "°8°" + swapIfNeeded(now + next));
// 16 Himmelsrichtungen
nosw.add((grad + 22.5) + "°16°" + swapIfNeeded2(now + next + now));
nosw.add((grad + 67.5) + "°16°" + swapIfNeeded2(next + now + next));
// 32 Himmelsrichtungen
nosw.add((grad + 11.25) + "°32°" + now + "z" + next);
nosw.add((grad + 33.75) + "°32°" + swapIfNeeded(now + next) + "z" + now);
nosw.add((grad + 56.25) + "°32°" + swapIfNeeded(now + next) + "z" + next);
nosw.add((grad + 78.75) + "°32°" + next + "z" + now);
}
}
public static String swapIfNeeded(String str) {
if (str.startsWith("O") || str.startsWith("W")) {
char[] c = str.toCharArray();
char temp = c[0];
c[0] = c[1];
c[1] = temp;
return new String(c);
} else {
return str;
}
}
public static String swapIfNeeded2(String str) {
if (str.startsWith("N") || str.startsWith("S")) {
char[] c = str.toCharArray();
char temp = c[1];
c[1] = c[0];
c[2] = temp;
return new String(c);
} else {
return str;
}
}
public static void getWindrichtung() {
System.out.println(System.lineSeparator() + "Windrichtung: " + getSearched(getKreiseinteilung(), getGrad()));
}
public static int getKreiseinteilung() {
try {
sc = new Scanner(System.in);
System.out.println("Kreiseinteilung in 4, 8, 16 oder 32 angeben!");
System.out.print("Kreiseinteilung: ");
return Integer.parseInt(sc.nextLine());
} catch (Exception e) {
System.out.print(System.lineSeparator() + "Die Kreiseinteilung wurde automatisch auf 16 gesetzt!" + System.lineSeparator());
return 16;
}
}
public static double getGrad() {
try {
sc = new Scanner(System.in);
System.out.print("Grad: ");
return Double.parseDouble(sc.nextLine());
} catch (Exception e) {
System.out.println(System.lineSeparator() + "Der Grad wurde automatisch auf 0 gesetzt!" + System.lineSeparator());
return 0;
}
}
public static String getSearched(int k, double g) {
HashMap<Double, String> unsorted = new HashMap<Double, String>();
for (int i = 0; i < nosw.size(); i++) {
String[] s = nosw.get(i).split("°");
if (k >= Integer.parseInt(s[1])) {
unsorted.put(Double.parseDouble(s[0]), s[2]);
}
}
List<Double> array = new ArrayList<Double>(unsorted.keySet());
if (!array.contains(g)) {
array.add(g);
} else {
return unsorted.get(g);
}
double[] sorted = convertListToDoubles(array);
Arrays.sort(sorted);
int searched = Arrays.binarySearch(sorted, g);
return unsorted.get(getLowest(sorted, getTemp1(sorted, searched), getTemp2(sorted, searched), searched));
}
public static double[] convertListToDoubles(List<Double> array) {
double[] list = new double[array.size()];
for (int i = 0; i < list.length; i++) {
list[i] = array.get(i).doubleValue();
}
return list;
}
public static double getTemp1(double[] array, int searched) {
try {
if ((array[searched - 1]) > array[searched]) {
return (array[searched - 1]) - array[searched];
} else {
return array[searched] - (array[searched - 1]);
}
} catch (IndexOutOfBoundsException e) {
return 1000;
}
}
public static double getTemp2(double[] array, int searched) {
if ((array[searched + 1]) > array[searched]) {
return (array[searched + 1]) - array[searched];
} else {
return array[searched] - (array[searched + 1]);
}
}
public static double getLowest(double[] array, double a, double b, int searched) {
double min = Math.min(a, b);
if (min == a) {
return array[searched - 1];
} else if (min == b) {
return array[searched + 1];
}
return -1;
}
}
Lösung von: Name nicht veröffentlicht
function getHeading(degrees, precision) {
var precision = precision || 16,
directions = [],
direction = 0,
step = 360 / precision,
i = step / 2;
switch (precision) {
case 4: directions = "N O S W".split(" "); break;
case 8: directions = "N NO O SO S SW W NW".split(" "); break;
case 16: directions = ("N NNO NO ONO O OSO SO " +
"SSO S SSW SW WSW W WNW NW NNW").split(" ");
break;
case 32: directions = ("N NzO NNO NOzN NO NOzO ONO OzN O OzS OSO " +
"SOzO SO SOzS SSO SzO S SzW SSW SWzS SW SWzW WSW WzS W WzN " +
"WNW NWzW NW NWzN NNW NzW").split(" ");
break;
default: throw("Invalid precision argument.");
}
if (degrees < 0 || degrees > 360) throw("Invalid degrees argument.");
if (degrees <= i || degrees >= 360 - i) return "N";
while (i <= degrees) {
direction++;
i += step;
}
return directions[direction];
}
console.log(getHeading(240, 4));
console.log(getHeading(240, 8));
console.log(getHeading(240));
console.log(getHeading(240, 32)); // lissalanda@gmx.at
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
windrichtungen = ["N", "NzO", "NNO", "NOzN", "NO", "NOzO", "ONO", "OzN", "O", "OzS", "OSO", "SOzO", "SO", "SOzS", "SSO", "SzO", "S", "SzW", "SSW", "SWzS","SW", "SWzW", "WSW", "WzS", "W", "WzN", "WNW", "NWzW", "NW", "NWzN", "NNW", "NzW"]
gradzahl = float(input("Gradzahl: ") or "240")
kreiseinteilung = int(input("Kreiseinteilung: ") or "16")
windrichtungen = [windrichtungen[i] for i in range(0, 32, 32//kreiseinteilung)]
index = round(((gradzahl % 360) / 360) * kreiseinteilung) % kreiseinteilung
print(windrichtungen[index])
Lösung von: Jan Ayoub B. (TU Berlin)
Option Explicit
' eigentlich VBA (Excel)
Function getWindrichtung(Grad As Double, Kreiseinteilung As Integer) As String
Dim Index As Integer
Dim Windrichtung
Windrichtung = Array("N", "NzO", "NNO", "NOzN", "NO", "NOzO", "ONO", "OzN", "O", "OzS", "OSO", "SOzO", "SO", "SOzS", "SSO", "SzO", "S", "SzW", "SSW", "SWzS", "SW", "SWzW", "WSW", "WzS", "W", "WzN", "WNW", "NWzW", "NW", "NWzN", "NNW", "NzW", "N")
' Grad(360) nach Grad(100)
'gh = Grad / 3.6
' Index berechnen
Index = Round((Grad / 3.6) / (100 / Kreiseinteilung), 0)
If Kreiseinteilung < 32 Then
Index = Index * (32 / Kreiseinteilung)
End If
getWindrichtung = Windrichtung(Index)
End Function
Lösung von: Manfred Mertens (FH Aachen)
# Windrichtung in FHEM als userReadings für StateFormat mit der festen Kreiseinteilung 16
wind_direction {
# Umrechnung windir [Winkel] in wind_direction[Windrose]
use Math::Round qw/round/;
my @wd = ("N","NNO","NO","ONO","O","OSO","SO","SSO","S","SSW","SW","WSW","W","WNW","NW","NNW","N");
my $w = ReadingsVal($name,"observations_01_winddir",0);
my $d=$wd[round (($w / 3.6) / (100 / 16))];;
$d
}
Lösung von: Manfred Mertens (FH Aachen)
// NET 6.x | C# 10.x | VS-2022
Console.WriteLine(GetDirection(240, Precision.P4));
Console.WriteLine(GetDirection(240, Precision.P8));
Console.WriteLine(GetDirection(240));
Console.WriteLine(GetDirection(240, Precision.P32));
static string GetDirection(double degree, Precision precision = Precision.P16) =>
Enumerable.Range(0, 32).Where(x => x % (32 / (int)precision) == 0)
.Select(x => Enum.GetName(typeof(Direction), x) ?? "")
.ToList()[(int)Math.Round(degree % 360 / 360 * (int)precision, 0) % (int)precision];
enum Precision { P4 = 4, P8 = 8, P16 = 16, P32 = 32 };
enum Direction { N, NzO, NNO, NOzN, NO, NOzO, ONO, OzN, O, OzS, OSO, SOzO, SO, SOzS, SSO, SzO, S, SzW, SSW, SwzS, SW, SWzW, WSW, WzS, W, WzN, WNW, NWzW, NW, NWzN, NNW, NzW }
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
#include <vector>
#include <cmath>
enum class Precision { P4 = 4, P8 = 8, P16 = 16, P32 = 32 };
const std::vector<std::string> Directions{ "N", "NzO", "NNO", "NOzN", "NO", "NOzO", "ONO", "OzN", "O", "OzS", "OSO", "SOzO", "SO", "SOzS", "SSO", "SzO", "S", "SzW", "SSW", "SwzS", "SW", "SWzW", "WSW", "WzS", "W", "WzN", "WNW", "NWzW", "NW", "NWzN", "NNW", "NzW" };
std::string get_direction(double degree, Precision precision = Precision::P16) {
std::vector<std::string>v{};
for (auto i{ 0 }; i < 32; i++)
if (i % (32 / (int)precision) == 0)
v.push_back(Directions[i]);
return v[floor(std::fmod(std::fmod(degree, 360) / 360 * (int)precision, (int)precision) + 0.5)];
}
int main() {
std::cout << get_direction(240, Precision::P4) << "\n";
std::cout << get_direction(240, Precision::P8) << "\n";
std::cout << get_direction(240) << "\n";
std::cout << get_direction(240, Precision::P32) << "\n";
}
Lösung von: Jens Kelm (@JKooP)
/*
C++ 20 | VS - 2022
std:c++20 MSVC only!
*/
#include <iostream>
#include <vector>
#include <array>
#include <format>
enum class precision { P4 = 4, P8 = 8, P16 = 16, P32 = 32 };
const std::array<std::string, 32> directions{
"N", "NzO", "NNO", "NOzN", "NO", "NOzO",
"ONO", "OzN", "O", "OzS", "OSO",
"SOzO", "SO", "SOzS", "SSO", "SzO", "S", "SzW", "SSW", "SwzS", "SW", "SWzW",
"WSW", "WzS", "W", "WzN", "WNW", "NWzW", "NW", "NWzN", "NNW", "NzW" };
struct data {
double deg;
precision prec{ precision::P16 };
};
const static auto get_direction(const data& data_) {
const auto prec{ (int)data_.prec };
std::vector<std::string>v{};
for (auto i{ 0 }; i < 32; ++i)
if (i % (32 / prec) == 0)
v.push_back(directions.at(i));
return v[floor(std::fmod(std::fmod(data_.deg, 360) / 360 * prec, prec) + 0.5)];
}
static const std::ostream& operator<<(std::ostream& os_, const data& data_) noexcept {
os_ << std::format("Degree: {}\n", data_.deg);
os_ << std::format("Precision: P{}\n", int(data_.prec));
os_ << std::format("Direction: {}\n\n", get_direction(data_));
return os_;
}
int main() {
std::cout << data{ 240, precision::P4 };
std::cout << data{ 240, precision::P8 };
std::cout << data{ 240 };
std::cout << data{ 240, precision::P32 };
}
/*
Ausgabe:
Degree: 240
Precision: P4
Direction: W
Degree: 240
Precision: P8
Direction: SW
Degree: 240
Precision: P16
Direction: WSW
Degree: 240
Precision: P32
Direction: SWzW
*/
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
Grad | Präzision (Kreiseinteilung) |
Windrichtung |
240 | 4 | W |
240 | 8 | SW |
240 | 16 | WSW |
240 | 32 | SWzW |
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 0.5 |
Schwierigkeit: | Leicht |
Webcode: | ibbn-2e7d |
Autor: | () |