Cluster   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 63
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C addCoordinate() 0 36 7
1
<?php
2
namespace MatthiasMullie\Geo;
3
4
/**
5
 * Please report bugs on https://github.com/matthiasmullie/geo/issues
6
 *
7
 * @author Matthias Mullie <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2013, Matthias Mullie. All rights reserved.
10
 * @license MIT License
11
 */
12
class Cluster
13
{
14
    /**
15
     * @var Bounds
16
     */
17
    public $bounds;
18
19
    /**
20
     * @var Coordinate
21
     */
22
    public $center;
23
24
    /**
25
     * @var int
26
     */
27
    public $total = 0;
28
29
    /**
30
     * @var Coordinate[]
31
     */
32
    public $coordinates = array();
33
34
    /**
35
     * @param Coordinate $coord
36
     * @param bool       $save
37
     */
38
    public function addCoordinate(Coordinate $coord, $save)
39
    {
40
        if ($save) {
41
            $this->coordinates[] = $coord;
42
        }
43
44
        // initialize the cluster
45
        if ($this->total == 0) {
46
            $this->bounds = new Bounds($coord, $coord);
47
            $this->center = $coord;
48
            $this->total = 1;
49
50
            return;
51
        }
52
53
        // adjust cluster bounds to include this coordinate
54
        $this->bounds = new Bounds(
55
            new Coordinate(
56
                // these shorthand ifs are equivalent to min() and max(), but faster
57
                $this->bounds->ne->latitude > $coord->latitude ? $this->bounds->ne->latitude : $coord->latitude,
58
                $this->bounds->ne->longitude > $coord->longitude ? $this->bounds->ne->longitude : $coord->longitude
59
            ),
60
            new Coordinate(
61
                $this->bounds->sw->latitude < $coord->latitude ? $this->bounds->sw->latitude : $coord->latitude,
62
                $this->bounds->sw->longitude < $coord->longitude ? $this->bounds->sw->longitude : $coord->longitude
63
            )
64
        );
65
66
        // adjust weighed center
67
        $this->center = new Coordinate(
68
            (($this->center->latitude * $this->total) + $coord->latitude) / ($this->total + 1),
69
            (($this->center->longitude * $this->total) + $coord->longitude) / ($this->total + 1)
70
        );
71
72
        $this->total++;
73
    }
74
}
75