| Conditions | 7 |
| Paths | 4 |
| Total Lines | 36 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 |