1
|
|
|
<?php |
2
|
|
|
namespace Nubs\Geometron; |
3
|
|
|
|
4
|
|
|
use Nubs\Vectorix\Vector; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* This class represents an immutable point in euclidean geometry and its |
8
|
|
|
* associated operations. |
9
|
|
|
* |
10
|
|
|
* Instances of this class will not change state. Any operations on the point |
11
|
|
|
* will return a new point with the new state. |
12
|
|
|
*/ |
13
|
|
|
class Point implements Finite |
14
|
|
|
{ |
15
|
|
|
/** @type \Nubs\Vectorix\Vector A vector representing the point. */ |
16
|
|
|
protected $_vector; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Initalize the point from a vector. |
20
|
|
|
* |
21
|
|
|
* @api |
22
|
|
|
* @param \Nubs\Vectorix\Vector $vector The vector representing the point's terms. |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Vector $vector) |
25
|
|
|
{ |
26
|
|
|
$this->_vector = $vector; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates a point from its terms. |
31
|
|
|
* |
32
|
|
|
* @api |
33
|
|
|
* @param array<int|float> $terms The terms (x, y, z, etc.) of the point. |
34
|
|
|
* @return self The point with the given terms. |
35
|
|
|
*/ |
36
|
|
|
public static function createFromTerms(array $terms) |
37
|
|
|
{ |
38
|
|
|
return new static(new Vector($terms)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the vector representing the point's location. |
43
|
|
|
* |
44
|
|
|
* @api |
45
|
|
|
* @return \Nubs\Vectorix\Vector The vector representing the point. |
46
|
|
|
*/ |
47
|
|
|
public function vector() |
48
|
|
|
{ |
49
|
|
|
return $this->_vector; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the terms of the point. |
54
|
|
|
* |
55
|
|
|
* @api |
56
|
|
|
* @return array<int|float> The terms of the point. |
57
|
|
|
*/ |
58
|
|
|
public function terms() |
59
|
|
|
{ |
60
|
|
|
return $this->vector()->components(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the dimension of the point. |
65
|
|
|
* |
66
|
|
|
* @api |
67
|
|
|
* @return int The dimension of the point. |
68
|
|
|
*/ |
69
|
|
|
public function dimension() |
70
|
|
|
{ |
71
|
|
|
return $this->vector()->dimension(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check whether the given point is the same as this point. |
76
|
|
|
* |
77
|
|
|
* @api |
78
|
|
|
* @param self $b The point to check for equality. |
79
|
|
|
* @return bool True if the points are equal and false otherwise. |
80
|
|
|
*/ |
81
|
|
|
public function isEqual(self $b) |
82
|
|
|
{ |
83
|
|
|
return $this->vector()->isEqual($b->vector()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Check whether the given point belongs to the same geometric space as this |
88
|
|
|
* point. |
89
|
|
|
* |
90
|
|
|
* @api |
91
|
|
|
* @param self $b The point to check. |
92
|
|
|
* @return bool True if the points are in the same geometric space and false otherwise. |
93
|
|
|
*/ |
94
|
|
|
public function isSameSpace(self $b) |
95
|
|
|
{ |
96
|
|
|
return $this->vector()->isSameVectorSpace($b->vector()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The center of the point is just the point itself. |
101
|
|
|
* |
102
|
|
|
* @api |
103
|
|
|
* @return \Nubs\Geometron\Point This point. |
104
|
|
|
* @see \Nubs\Geometron\Finite::center() |
105
|
|
|
*/ |
106
|
|
|
public function center() |
107
|
|
|
{ |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|