php-ml/src/Phpml/Clustering/KMeans/Point.php
2016-05-01 23:36:33 +02:00

125 lines
2.3 KiB
PHP

<?php
declare (strict_types = 1);
namespace Phpml\Clustering\KMeans;
use ArrayAccess;
class Point implements ArrayAccess
{
/**
* @var int
*/
protected $dimension;
/**
* @var array
*/
protected $coordinates;
/**
* @param array $coordinates
*/
public function __construct(array $coordinates)
{
$this->dimension = count($coordinates);
$this->coordinates = $coordinates;
}
/**
* @return array
*/
public function toArray()
{
return $this->coordinates;
}
/**
* @param Point $point
* @param bool $precise
*
* @return int|mixed
*/
public function getDistanceWith(self $point, $precise = true)
{
$distance = 0;
for ($n = 0; $n < $this->dimension; ++$n) {
$difference = $this->coordinates[$n] - $point->coordinates[$n];
$distance += $difference * $difference;
}
return $precise ? sqrt($distance) : $distance;
}
/**
* @param $points
*
* @return mixed
*/
public function getClosest($points)
{
foreach ($points as $point) {
$distance = $this->getDistanceWith($point, false);
if (!isset($minDistance)) {
$minDistance = $distance;
$minPoint = $point;
continue;
}
if ($distance < $minDistance) {
$minDistance = $distance;
$minPoint = $point;
}
}
return $minPoint;
}
/**
* @return array
*/
public function getCoordinates()
{
return $this->coordinates;
}
/**
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->coordinates[$offset]);
}
/**
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset)
{
return $this->coordinates[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->coordinates[$offset] = $value;
}
/**
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->coordinates[$offset]);
}
}