C# :: Aufgabe #34

9 Lösungen Lösungen öffentlich

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

Lösungen:

vote_ok
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.
Quellcode ausblenden 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);
}
vote_ok
von Process1 (1180 Punkte) - 15.01.2013 um 05:16 Uhr
Quellcode ausblenden C#-Code
static void Main(string[] args) {
            Console.Write("x1:");
            int x1 = int.Parse(Console.ReadLine());
            Console.Write("y1:");
            int y1 = int.Parse(Console.ReadLine());
            Console.Write("x2:");
            int x2 = int.Parse(Console.ReadLine());
            Console.Write("y2:");
            int y2 = int.Parse(Console.ReadLine());
            double d = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            Console.Write("\n"+d);

            Console.ReadKey();
        }
1x
vote_ok
von Donni (210 Punkte) - 31.03.2013 um 14:01 Uhr
Quellcode ausblenden C#-Code
using System;

namespace Abstand_zweier_Punkte
{
    class Program
    {
        static void Main(string[] args)
        {
            // Fenster Titel
            Console.Title = "Abstand zweier Punkte";

            // Variablen
            int x1, x2, y1, y2, dx, dy;
            double distance;

            // Werte einlesen
            Console.Write("x1: ");
            x1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("y1: ");
            y1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("x2: ");
            x2 = Convert.ToInt32(Console.ReadLine());
            Console.Write("y2: ");
            y2 = Convert.ToInt32(Console.ReadLine());

            // Delta x, Delta y
            dx = x2 - x1;
            dy = y2 - y1;

            // Betrag des Vektors dx dy
            distance = Math.Sqrt(Math.Pow(dx,2) + Math.Pow(dy,2));

            // Ausgabe
            Console.WriteLine("\r\nAbstand: " + distance);
            Console.ReadKey();
        }
    }
}
vote_ok
von wladi-g (1310 Punkte) - 03.06.2014 um 12:18 Uhr
Quellcode ausblenden C#-Code
using System;

namespace PunkteAbstand
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] punkte = new int[2, 2];

            for (int i = 0; i < punkte.GetLength(0); i++)
                for (int j = 0; j < punkte.GetLength(1); j++)
                    do
                    {
                        if (j == 0)
                            Console.Write("x");
                        else
                            Console.Write("y");
                        if (i == 0)
                            Console.Write("1:");
                        else
                            Console.Write("2:");
                    }
                    while (!int.TryParse(Console.ReadLine(), out punkte[i, j]));
            Console.WriteLine(Math.Sqrt(Math.Pow(punkte[0, 1] - punkte[1, 1], 2) + Math.Pow(punkte[0, 0] - punkte[1, 0], 2)));
        }
    }
}
vote_ok
von niknik (1230 Punkte) - 13.08.2015 um 10:49 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstandZweierPunkte
{
    class Program
    {
        static void Main(string[] args)
        {
            bool valid = false;
            double x1,x2,y1,y2;

            // x1 einlesen
            do{
                Console.Clear();
                Console.WriteLine("x1: ");
                valid = double.TryParse(Console.ReadLine(), out x1);
            }while(valid != true);
            valid = false;
            // y1 einlesen
            do{
                Console.Clear();
                Console.WriteLine("y1: ");
                valid = double.TryParse(Console.ReadLine(), out y1);
            }while(valid != true);
            valid = false;
            // x2 einlesen
            do{
                Console.Clear();
                Console.WriteLine("x2: ");
                valid = double.TryParse(Console.ReadLine(), out x2);
            }while(valid != true);
            valid = false;
            // y2 einlesen
            do{
                Console.Clear();
                Console.WriteLine("y2: ");
                valid = double.TryParse(Console.ReadLine(), out y2);
            }while(valid != true);
            valid = false;

            double result = Math.Sqrt(Math.Pow(x1-x2,2) + Math.Pow(y1-y2,2));

            Console.Clear();
            Console.WriteLine("Erster Punkt:  ({0,-7} , {1,7})",x1,y1);
            Console.WriteLine("Zweiter Punkt: ({0,-7} , {1,7})",x2,y2);
            Console.WriteLine("Abstand:  {0}", result);
            Console.ReadLine();
        }
    }
}
vote_ok
von n.rohde (400 Punkte) - 25.08.2015 um 16:54 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstandZweierPunkte
{
    class Program
    {
        static void Main(string[] args)
        {
            int x1, x2, y1, y2;
            double abstand;

            Console.Write("x1: ");
            x1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("y1: ");
            y1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("x2: ");
            x2 = Convert.ToInt32(Console.ReadLine());
            Console.Write("y2: ");
            y2 = Convert.ToInt32(Console.ReadLine());

            abstand = Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

            Console.WriteLine("Abstand: "+Convert.ToString(Math.Round(abstand, 2)));
            Console.Read();

        }
    }
}
vote_ok
von kjaenke (1140 Punkte) - 08.11.2017 um 10:43 Uhr
Quellcode ausblenden C#-Code
using System;

namespace Exercise_34
{
    static class Program
    {
        static void Main()
        {
            Console.Write("x1: ");
            var x1 = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());
            Console.Write("y1: ");
            var y1 = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());
            Console.Write("x2: ");
            var x2 = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());
            Console.Write("y2: ");
            var y2 = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());

            Console.WriteLine($"Die Entfernung beträgt {Math.Sqrt(Math.Pow((x2-x1),2)+Math.Pow((y2-y1),2))}");
            Console.Read();
        }
    }
}

vote_ok
von stbehl (1640 Punkte) - 05.02.2018 um 12:16 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrainYourProgrammer34
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("x1: ");
            double xEins = Convert.ToDouble(Console.ReadLine());
            Console.Write("y1: ");
            double yEins = Convert.ToDouble(Console.ReadLine());
            Console.Write("x2: ");
            double xZwei = Convert.ToDouble(Console.ReadLine());
            Console.Write("y2: ");
            double yZwei = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("\n" + Math.Sqrt(Math.Pow((xZwei - xEins), 2) + Math.Pow((yZwei - yEins), 2)));
            Console.ReadKey();
        }
    }
}
vote_ok
von stcalvin (970 Punkte) - 05.02.2018 um 14:55 Uhr
Quellcode ausblenden C#-Code
        static void Aufgabe_34()
        {
            double x1, x2, y1, y2, a, b;
            double c;

            Console.Write("x1:");
            x1 = Convert.ToInt16(Console.ReadLine());
            Console.Write("y1:");
            y1 = Convert.ToInt16(Console.ReadLine());
            Console.Write("x2:");
            x2 = Convert.ToInt16(Console.ReadLine());
            Console.Write("y2:");
            y2 = Convert.ToInt16(Console.ReadLine());

            a = Math.Abs(x1 - x2);
            b = Math.Abs(y1 - y2);
            c = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));

            Console.WriteLine(c);
        }