Python :: Aufgabe #24
5 Lösungen

Abstand zweier Punkte
Anfänger - Python
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:

#!/usr/bin/python import math def Abstand(x1, y1, x2, y2): Abstand_x = x2 - x1 Abstand_y = y2 - y1 Abstand_zum_Quadrat = Abstand_x**2 + Abstand_y**2 Ergebnis = math.sqrt(Abstand_zum_Quadrat) return Ergebnis print Abstand(input("x1:"), input("y1:"), input("x2:"), input("y2:"))
Nocheinmal in stark verkürztem Code.

#!/usr/bin/python import math def Abstand(x1, y1, x2, y2): return math.sqrt(((x2-x1)**2)+((y2-y1)**2)) print Abstand(input("x1:"), input("y1:"), input("x2:"), input("y2:"))

import math x1 = float(input("x_1: ")) y1 = float(input("y_1: ")) x2 = float(input("x_2: ")) y2 = float(input("y_2: ")) print("Abstand:", math.sqrt(abs(x1-x2)**2 + abs(y1-y2)**2))

import sys import math def neurechnen (): wiederhole = "j" ende = "n" a = (input("Erneut rechnen? [j/n]")) while (True): if (a == wiederhole): print ("") print ("Ich freue mich eine neue Aufgabe für Sie rechnen zu dürfen!") print("") rechne() elif (a == ende): print ("") print ("Auf Wiedersehen!") sys.exit(0) else: print ("Bitte 'j' für Ja und 'n' für Nein eintippen.") neurechnen() def rechne(): print ("--------------------------------------------------------------------------------------") print ("--Ich bin ein einfaches Python-Programm welches den Abstand zweier Punkte ermittelt.--") print ("--------------------------------------------------------------------------------------") x1 = float(input("Geben Sie bitte den Wert für x1 ein.")) y1 = float(input("Geben Sie bitte den Wert für y1 ein.")) x2 = float(input("Geben Sie bitte den Wert für x2 ein.")) y2 = float(input("Geben Sie bitte den Wert für y2 ein.")) print ("") abstand = ((((x1 - x2)**2) + ((y1 - y2)**2))) abstand_ausgabe = math.sqrt (abstand) print ("x1: " , x1) print ("y1: " , y1) print ("x2: " , x2) print ("y2: " , y2) print ("") print ("Ihr Ergebnis: " , abstand_ausgabe) print ("") neurechnen() rechne()
Zusätzlich wird noch die lineare Funktion angegeben, die durch die beiden Punkte verläuft.
Python-Code

# punkteabstand.py import math print("Punkt 1:") x1 = float(input("X-Wert: ")) y1 = float(input("Y-Wert: ")) print("Punkt 2:") x2 = float(input("X-Wert: ")) y2 = float(input("Y-Wert: ")) m = (y2 - y1) / (x2 - x1) c = y1 - m * x1 betrag = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) print("Die Punkte liegen beide auf der linearen Funktion y = " + str(m) + " * x + " + str(c)) print("Sie sind " + str(betrag) + " Einheiten voneinander entfernt.")

# -*- coding: utf-8 -*- """ Created on Mon Aug 31 17:02:10 2020 @author: Robin """ from random import randint import math inp = 0 while not inp: try: a = int(input(">>> Untere Grenze angeben: ")) b = int(input(">>> Obere Grenze angeben: ")) if a > b: print("") print(">>> Bitte überprüfe deine Eingabe.") else: inp = 1 except: print("") print(">>> Bitte nur Zahlen verwenden !!!") x1 = randint(a, b) x2 = randint(a, b) y1 = randint(a, b) y2 = randint(a, b) p1 = (x1,x2) p2 = (y1,y2) klammer = (x1-x2)**2 + (y1-y2)**2 abstand = math.sqrt(klammer) print("") print(">>> Der Punkt",p1,"hat zum Punkt",p2,"den Abstand:",abstand)