PHP :: Aufgabe #27

4 Lösungen Lösungen öffentlich

Abstand zweier Punkte

Anfänger - PHP 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 bibir (1870 Punkte) - 09.09.2014 um 12:05 Uhr
Quellcode ausblenden PHP-Code
<?php
function berechne(){
	if(!(isset($_POST['x1']) && isset($_POST['y1']) && isset($_POST['x2']) && isset($_POST['y2']) && 
		filter_var($_POST['x1'], FILTER_VALIDATE_FLOAT) && 
		filter_var($_POST['x2'], FILTER_VALIDATE_FLOAT) &&
		filter_var($_POST['y1'], FILTER_VALIDATE_FLOAT) &&
		filter_var($_POST['y2'], FILTER_VALIDATE_FLOAT))){
		return 'Nur Zahlenwerte angeben.';
	} else {
		$erg =sqrt(pow(($_POST['x1']-$_POST['x2']),2) + pow(($_POST['y1']-$_POST['y2']),2));		
		return 'Abstand zwischen ('.$_POST['x1'].', '.$_POST['y1'].') und ('.$_POST['x2'].', '.$_POST['y2'].'): '.$erg;
	}
}
?>
<!DOCTYPE html>
<html>
	<head>
		<title>Abstand berechnen</title>
	</head>
	<body>
		<form action="./abstand.php" name="abstand" method="POST">
			<table>
				<tr>
					<td>x1:</td>
					<td><input type="text" name="x1" size="2" /></td>
				</tr>
				<tr>
					<td>y1:</td>
					<td><input type="text" name="y1" size="2" /></td>
				</tr>
				<tr>
					<td>x2:</td>
					<td><input type="text" name="x2" size="2" /></td>
				</tr>
				<tr>
					<td>y2:</td>
					<td><input type="text" name="y2" size="2" /></td>
				</tr>
				<tr>
					<td colspan="2"><input type="submit" value="berechnen" /></td>
				</tr>
			</table>
		</form>
			<?php
				echo berechne();
			?>		
	</body>
</html>
vote_ok
von Exception (7090 Punkte) - 24.06.2018 um 16:46 Uhr
Quellcode ausblenden PHP-Code
<?php

function calcDistance($points)
{
	// Siehe https://en.wikipedia.org/wiki/Euclidean_distance#Two_dimensions
	$distance = sqrt(
					pow(($points[0]['x'] - $points[1]['x']), 2) +
					pow(($points[0]['y'] - $points[1]['y']), 2)
				);
	
	return $distance;
}


$points[0] = array('x' => 1, 'y' => 1);
$points[1] = array('x' => 2, 'y' => 2);

$distance  =  calcDistance($points);

echo 'Der Abstand zwischen den Punkten p1 ('.$points[0]["x"].'|'.$points[0]["y"].') und p2('.$points[1]["x"].'|'.$points[1]["y"].') beträgt: '.$distance.'<br />';

?>
vote_ok
von syneex (2670 Punkte) - 27.09.2018 um 14:26 Uhr
Quellcode ausblenden HTML-Code
<!DOCTYPE html>
<html>
<head>
	<title>Distance between two Points</title>
</head>
<body>
<form method="POST">
	x1:
	<input type="text" name="x1"><br><br>
	y1:
	<input type="text" name="y1"><br><br>
	x2:
	<input type="text" name="x2"><br><br>
	y2:
	<input type="text" name="y2"><br><br>
	<input type="submit" name="" value="Berechnen!">
</form>
</body>
</html>
Quellcode ausblenden PHP-Code
<?php
if(isset($_POST['x1'], $_POST['y1'], $_POST['x2'], $_POST['y2']))
{
	echo "<br>Die Distanz beträgt: " . calculateDistance($_POST['x1'], $_POST['y1'], $_POST['x2'], $_POST['y2']);
}

function calculateDistance($x1, $y1, $x2, $y2)
{
	$erg = sqrt(pow(($x1-$x2), 2) + pow(($y1-$y2), 2));
	return $erg;
}
?>
vote_ok
von paddlboot (3970 Punkte) - 08.08.2019 um 09:44 Uhr
Quellcode ausblenden PHP-Code
<?php
//beliebige Werte eingeben
$x1 = 1;
$y1 = 1;
$x2 = 2;
$y2 = 2;


echo 'x1: '.$x1.'<br/>y1: '.$y1.'<br/>x2: '.$x2.'<br/>y2: '.$y2;

$abstand = sqrt(abs($x1-$x2) + abs($y1-$y2));

echo '<br/><br/>Abstand: '.$abstand;
?>