Abstand (Unterprogramme)
Um die Länge der Luftlinie zwischen zwei Punkten auf der Landkarte zu berechnen, wird ein Programm benötigt. Schreiben Sie eine Subroutine, um den Abstand zweier Punkte in der Ebene zu berechnen.
P1 = (x1, y1), P2 = (x2, y2).
Berechnen Sie damit unter anderem den (Kilometer-)Abstand der Orte Zell (704.4, 256.2) und Neuburg (693.3, 261.4), die hier in Schweizerischen Landeskoordinaten (CH1903) gegeben sind.
1 Kommentare
8 Lösung(en)
import java.util.Scanner;
public class Abstand {
public static void main(String[] args) {
new Abstand().top();
}
void top() {
double xStart, yStart, xEnd, yEnd;
xStart = einlesen("x Start");
yStart = einlesen("y Start");
xEnd = einlesen("x End");
yEnd = einlesen("y End");
double abstand;
abstand = abstand(xStart, yStart, xEnd, yEnd);
ausgabe(abstand);
}
void ausgabe(double abstand) {
System.out.print("Der Abstand beträgt: ");
System.out.println(abstand);
}
double abstand(double xStart, double yStart, double xEnd, double yEnd) {
double dx = xEnd - xStart;
double dy = yEnd - yStart;
double abstand;
abstand = Math.sqrt(dx * dx + dy * dy);
return abstand;
}
Scanner sc = new Scanner(System.in);
double einlesen(String meldung) {
System.out.println(meldung + ": ");
return sc.nextDouble();
}
} // end of class Abstand
hauptprogramm();
//////////////////////////
function hauptprogramm() {
x1 = window.prompt("Geben sie eine zahl für X1 ein")*1;
x2 = window.prompt("Geben sie eine zahl für X2 ein")*1;
y1 = window.prompt("Geben sie eine zahl für Y1 ein")*1;
y2 = window.prompt("Geben sie eine zahl für Y2 ein")*1;
window.alert(Abstand(x1,x2,y1,y2));
}
function Abstand(x1,x2,y1,y2) {
dy = y2 - y1;
dx = x2 - x1;
abst = Math.sqrt((dy*dy)+(dx*dx));
return abst;
}
Lösung von: Raphael Hirsiger (Credit Suisse)
O
lol reingeschaut
Lösung von: Name nicht veröffentlicht
def abstand(x1,x2,y1,y2):
dy = y2 - y1
dx = x2 - x1
abst = ((dy*dy)+(dx*dx)) **(1/2)
return abst
x1=704.4; y1=256.2 # Zell
x2=693.3; y2=261.4 # Neuburg
print(abstand(x1,x2,y1,y2))
Lösung von: Alex Groeg (Freies Lernen)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main(){
float x1,x2,y1,y2,abstand;
printf("Erster Punkt:\nx-Koordinate: ");
scanf("%f",&x1);
printf("y-Koordinate: ");
scanf("%f",&y1);
printf("Zweiter Punkt:\nx-Koordinate: ");
scanf("%f",&x2);
printf("y-Koordinate: ");
scanf("%f",&y2);
abstand = sqrt(pow(x1-x2,2)+pow(y1-y2,2));
printf("Der Abstand betraegt %.3fLE",abstand);
}
Lösung von: Fynn Koch (keine)
function getDistance(p1, p2) {
return Math.sqrt( ((p2[1] - p1[1]) **2) + ((p2[0] - p1[0]) ** 2) );
}
console.log(getDistance( [- 4 , 2 ], [- 1 , 6 ] ));
console.log(getDistance( [- 1 , 6 ], [ 4 , 18 ] ));
console.log(getDistance( [ 704.4, 256.2], [ 693.3, 261.4] ).toPrecision(3));
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// C++ 14 | VS-2022
#include <iostream>
#include <iomanip>
#include <vector>
constexpr auto M_PI{ 3.14159265358979323846 };
constexpr auto AVG_DIST{ 111.3 };
struct Location {
double latitude{ 0 };
double longitude{ 0 };
};
struct LocationSet {
std::string name1;
Location loc1 {0, 0};
std::string name2;
Location loc2{ 0, 0 };
};
double get_distance(Location loc1, Location loc2) {
const auto imp{ cos((loc1.latitude + loc2.latitude) / 2 * M_PI / 180) }; // Längenverbesserungswert
const auto x{ AVG_DIST * imp * (loc1.longitude - loc2.longitude) };
const auto y{ AVG_DIST * (loc1.latitude - loc2.latitude) };
return sqrt(x * x + y * y);
}
int main() {
std::vector<LocationSet>locSets{
{ "Zell", {47.445061, 8.798550}, "Zurich", {47.376591, 8.540294} },
{ "Berlin", {52.51424, 13.39243}, "Hamburg", {53.55545, 10.03282} },
{ "Prag", {50.09484, 14.40519}, "Oslo", {59.91478, 10.73087} }
};
for (const auto& ls : locSets)
std::cout << ls.name1 << " -> " << ls.name2 << ": " << std::fixed << std::setprecision(2) << get_distance(ls.loc1, ls.loc2) << " km\n";
}
Lösung von: Jens Kelm (@JKooP)
#include <stdio.h>
#include <math.h>
#define M_PI 3.14159265358979323846
#define AVG_DIST 111.3
typedef struct _Location_ {
double latitude;
double longitude;
}Location;
typedef struct _LocationSet_ {
char *name1;
Location loc1;
char *name2;
Location loc2;
}LocationSet;
double get_distance(Location loc1, Location loc2) {
// Längenverbesserungswert:
double imp = cos((loc1.latitude + loc2.latitude) / 2 * M_PI / 180);
double x = AVG_DIST * imp * (loc1.longitude - loc2.longitude);
double y = AVG_DIST * (loc1.latitude - loc2.latitude);
return sqrt(x * x + y * y);
}
int main() {
LocationSet locSets[] = {
{ "Zell", {47.445061, 8.798550}, "Zurich", {47.376591, 8.540294} },
{ "Berlin", {52.51424, 13.39243}, "Hamburg", {53.55545, 10.03282} },
{ "Prag", {50.09484, 14.40519}, "Oslo", {59.91478, 10.73087} }
};
size_t size = sizeof(locSets) / sizeof(locSets[0]);
for (size_t i = 0; i < size; i++){
LocationSet ls = locSets[i];
printf("%-10s%-5s%-10s%-5s%8.2f km%2s\n",
ls.name1,
"|",
ls.name2,
"|",
get_distance(ls.loc1, ls.loc2),
"|");
printf("-------------------------------------------\n");
}
return 0;
}
/*
Zell | Zurich | 20.89 km |
-------------------------------------------
Berlin | Hamburg | 252.96 km |
-------------------------------------------
Prag | Oslo | 1117.84 km |
-------------------------------------------
*/
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
P1 = (-4, 2); P2 = (-1, 6) -> Abstand = 5
P1 = (-1, 6); P2 = (4, 18) -> Abstand = 13
Zell und Neuburg liegen 12.26 Kilometer auseinander.
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 0.25 |
Schwierigkeit: | k.A. |
Webcode: | tybn-j6zi |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |
Kommentare (1)