find the midpoint between two points in php

To find the midpoint between two points in PHP, you can use the following function:

main.php
<?php
function midpoint($p1, $p2) {
  $x = ($p1['x'] + $p2['x']) / 2;
  $y = ($p1['y'] + $p2['y']) / 2;
  return array('x' => $x, 'y' => $y);
}
?>
147 chars
8 lines

This function takes in two points, $p1 and $p2, as associative arrays with 'x' and 'y' keys. It then calculates the midpoint between the two points using the midpoint formula:

main.php
(x1 + x2) / 2, (y1 + y2) / 2
29 chars
2 lines

Finally, it returns the midpoint as an associative array with 'x' and 'y' keys.

Here is an example usage of the function:

main.php
<?php
$p1 = array('x' => 1, 'y' => 2);
$p2 = array('x' => 5, 'y' => 6);
$midpoint = midpoint($p1, $p2);
echo "The midpoint between (".$p1['x'].", ".$p1['y'].") and (".$p2['x'].", ".$p2['y'].") is (".$midpoint['x'].", ".$midpoint['y'].")";
// Output: The midpoint between (1, 2) and (5, 6) is (3, 4)
?>
302 chars
8 lines

gistlibby LogSnag