note 35803 deleted from function.hypot by didou
| From: | didou@php.net | Date: | Tue, 31 Aug 2004 01:32:15 +0000 |
| Subject: | note 35803 deleted from function.hypot by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75702@lists.php.net to get a copy of this message | ||
Note Submitter: mauddib at gmx dot net
----
Too bad this function is not made generic:
<?php
/* Precondition: first and second argument are coordinates
in the same euclidian space.
Postcondition: function return value is equal to the
distance of the first and the second vector or -1 if the
two arguments are not in the same euclidian space.
*/
function gen_hypot(&$x, &$y) {
/* check number of coordinates */
if (count($x) != count($y)) {
/* no match, return an impossible value, -1 */
$r = -1;
} else {
for ($i = 0; $i < count($x); $i++) {
$s = $x[$i] - $y[$i];
$r += $s * $s;
}
}
return sqrt($r);
}
/* example call */
print gen_hypot(array(0, 0, 2), array(2, 3, 8)); /* output: 7 */
?>
like this. This will work for any euclidian coordinate system, not only for R^2.