1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KDTree\ValueObject; |
4
|
|
|
|
5
|
|
|
use KDTree\Exceptions\{InvalidDimensionsCount, InvalidPointProvided, UnknownDimension}; |
6
|
|
|
use KDTree\Interfaces\PointInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Point |
10
|
|
|
* |
11
|
|
|
* @package KDTree\ValueObject |
12
|
|
|
*/ |
13
|
|
|
final class Point implements PointInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $name = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $dimensions; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var float[] |
27
|
|
|
*/ |
28
|
|
|
private $axises; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param float ...$axises |
32
|
|
|
* |
33
|
|
|
* @throws InvalidDimensionsCount |
34
|
|
|
*/ |
35
|
|
|
public function __construct(float ...$axises) |
36
|
|
|
{ |
37
|
|
|
$dimensions = count($axises); |
38
|
|
|
if ($dimensions < 1) { |
39
|
|
|
throw new InvalidDimensionsCount(); |
40
|
|
|
} |
41
|
|
|
$this->dimensions = $dimensions; |
42
|
|
|
$this->axises = $axises; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getName(): string |
49
|
|
|
{ |
50
|
|
|
return $this->name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $name |
55
|
|
|
* |
56
|
|
|
* @return PointInterface |
57
|
|
|
*/ |
58
|
|
|
public function setName(string $name): PointInterface |
59
|
|
|
{ |
60
|
|
|
$this->name = $name; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param PointInterface $point |
67
|
|
|
* |
68
|
|
|
* @return float |
69
|
|
|
* @throws InvalidPointProvided |
70
|
|
|
*/ |
71
|
|
|
public function distance(PointInterface $point): float |
72
|
|
|
{ |
73
|
|
|
if ($point->getDimensions() !== $this->getDimensions()) { |
74
|
|
|
throw new InvalidPointProvided(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$result = array_reduce( |
78
|
|
|
range(0, $this->dimensions - 1), |
79
|
|
|
function (float $result, int $dimension) use ($point): float { |
80
|
|
|
return $result + ($point->getDAxis($dimension) - $this->getDAxis($dimension)) ** 2; |
81
|
|
|
}, |
82
|
|
|
0.0 |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return sqrt($result); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
public function getDimensions(): int |
92
|
|
|
{ |
93
|
|
|
return $this->dimensions; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $dimension |
98
|
|
|
* |
99
|
|
|
* @return float |
100
|
|
|
* @throws UnknownDimension |
101
|
|
|
*/ |
102
|
|
|
public function getDAxis(int $dimension): float |
103
|
|
|
{ |
104
|
|
|
if (isset($this->axises[$dimension])) { |
105
|
|
|
return $this->axises[$dimension]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
throw new UnknownDimension(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
|
|
public function equals(PointInterface $point): bool |
115
|
|
|
{ |
116
|
|
|
return $this->getAxises() === $point->getAxises(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritDoc |
121
|
|
|
*/ |
122
|
|
|
public function getAxises(): array |
123
|
|
|
{ |
124
|
|
|
return $this->axises; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|