C# :: Aufgabe #34 :: Lösung #1
9 Lösungen

#34
Abstand zweier Punkte
Anfänger - C#
von Dome
- 03.01.2013 um 01:09 Uhr
Schreiben Sie ein Programm, welches den Abstand zweier Punkte berechnet. Zuvor müssen die Koordinaten beider Punkte abgefragt werden.
Konsolenausgabe:
x1:1
y1:1
x2:2
y2:2
1.4142135623730951
#1

von pocki (4190 Punkte)
- 07.01.2013 um 17:55 Uhr
Für die Eingabewerte verwende ich eine Liste um einfach alle Werte nach int zu parsen und erspare mir so Variablen und Schreibarbeit.
C#-Code

void main() { List<string> input = new List<string>(); Console.Write("x1: "); input.Add(Console.ReadLine()); Console.Write("y1: "); input.Add(Console.ReadLine()); Console.Write("x2: "); input.Add(Console.ReadLine()); Console.Write("y2: "); input.Add(Console.ReadLine()); List<int> punkte = input.Select (i => int.Parse(i)).ToList(); double abstand = GetAbstand(punkte[0], punkte[1], punkte[2], punkte[3]); Console.WriteLine("Abstand: {0}", abstand); } private static double GetAbstand(int x1, int y1, int x2, int y2) { int laenge = x1 > x2 ? x1-x2 : x2-x1; int breite = y1 > y2 ? y1-y2 : y2-y1; return Math.Sqrt(laenge*laenge+breite*breite); }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1