-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdistance.php
More file actions
38 lines (25 loc) · 879 Bytes
/
distance.php
File metadata and controls
38 lines (25 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
$d = distance([1,1], [4,4]);
echo 'distance is : ' .$d;
function distance( $p1=array(), $p2=array())
{
/**
The distance formula is derived from the Pythagorean theorem.
To find the distance between two points (x1,y1) and (x2,y2),
all that you need to do is use the coordinates of these ordered pairs and apply the formula below.
_________________
Distance = √(x2−x1)2 + (y2−y1)2
To find distance from (1,1) and (4,4) using the function below?
$p1 =[1,1];
$p2 =[4,4];
distance($p1, $p2);
OUTPUT : 4.2426406871193
@Author: Prince Adeyemi
*/
$x1 = $p1[0];
$x2 = $p2[0];
$y1 = $p1[1];
$y2 = $p2[1];
$distance = sqrt(($x2 - $x1)**2 + ( $y2 - $y1)**2);
return $distance;
}