forked from vipsace/ace_prog1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.php
67 lines (43 loc) · 1.46 KB
/
distance.php
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
class COORD {
public $x , $y;
public function x($x) {
$this->x = $x;
}
public function y($y) {
$this->y = $y;
}
public function __construct($x=0,$y=0) {
$this->x = (float) $x;
$this->y = (float) $y;
}
}
function distance($x1 , $y1 , $x2 , $y2 ) {
return sqrt( pow($x2-$x1,2) + pow($y2-$y1,2) );
}
function __path( $a , $b , $c , $d ) { //$a=$b=$c=$d = Object { x -> coordinate , y -> coordinate }
$dist = distance( $a->x , $a->y , $b->x , $b->y ); //distance btw pt a & b - X
$dist += distance( $b->x , $b->y , $c->x , $c->y); //distance btw pt b & c - Y
$dist += distance( $c->x , $c->y , $d->x , $d->y); //distance btw pt c & d - Z
//$dist = X + Y + Z (Distance btw a&b + b&c + c&d)
return $dist;
}
function path($path) {
$dist = 0;
$c = count($path);
for($i=0;$i<$c-1;$i++) {
$dist += distance( $path[$i]->x , $path[$i]->y , $path[$i+1]->x , $path[$i+1]->y );
}
return $dist;
}
function paths( $inp ) { //$inp = Array( array($a,$b,$c,$d) , array($e,$f,$g,$h) .... ); WHERE $a,b,c,d,e,f,g,h = Object { x -> coordinate , y -> coordinate }
$distances = array();
foreach($inp as $key=>$path)
$distances[$key] = path($path);
asort($distances);
$ret = array();
foreach($distances as $key=>$dist) {
$ret[] = array("distance" =>$dist , "coords" => $inp[$key] );
}
return $ret;
}