|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CGI-Calc package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Milos Tomic <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Cgi\Calc\Point; |
|
13
|
|
|
|
|
14
|
|
|
use Cgi\Calc\Point; |
|
15
|
|
|
|
|
16
|
|
|
class PointSet extends \SplObjectStorage |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param PointSet $other |
|
20
|
|
|
* |
|
21
|
|
|
* @return PointSet |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function duplicate(PointSet $other) |
|
24
|
|
|
{ |
|
25
|
|
|
$result = new self(); |
|
26
|
|
|
$result->addAll($other); |
|
27
|
|
|
|
|
28
|
|
|
return $result; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Point[] $points |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(array $points = []) |
|
35
|
|
|
{ |
|
36
|
|
|
foreach ($points as $point) { |
|
37
|
|
|
if ($point instanceof Point) { |
|
38
|
|
|
$this->attach($point); |
|
39
|
|
|
} else { |
|
40
|
|
|
throw new \InvalidArgumentException('PointSet can index instances of Point only'); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param PointSet $other |
|
47
|
|
|
* |
|
48
|
|
|
* @return PointSet |
|
49
|
|
|
*/ |
|
50
|
|
|
public function intersect(PointSet $other) |
|
51
|
|
|
{ |
|
52
|
|
|
$result = new self(); |
|
53
|
|
|
$result->addAll($this); |
|
54
|
|
|
$result->removeAllExcept($other); |
|
55
|
|
|
|
|
56
|
|
|
return $result; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param PointSet $other |
|
61
|
|
|
* |
|
62
|
|
|
* @return PointSet |
|
63
|
|
|
*/ |
|
64
|
|
|
public function union(PointSet $other) |
|
65
|
|
|
{ |
|
66
|
|
|
$result = new self(); |
|
67
|
|
|
$result->addAll($this); |
|
68
|
|
|
$result->addAll($other); |
|
69
|
|
|
|
|
70
|
|
|
return $result; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param PointSet $other |
|
75
|
|
|
* |
|
76
|
|
|
* @return PointSet |
|
77
|
|
|
*/ |
|
78
|
|
|
public function diff(PointSet $other) |
|
79
|
|
|
{ |
|
80
|
|
|
$result = new self(); |
|
81
|
|
|
$result->addAll($this); |
|
82
|
|
|
$result->removeAll($other); |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getHash($object) |
|
88
|
|
|
{ |
|
89
|
|
|
if ($object instanceof Point) { |
|
90
|
|
|
return $object->hash(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
throw new \InvalidArgumentException('PointSet can index instances of Point only'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param callable $callback |
|
98
|
|
|
* |
|
99
|
|
|
* @return PointSet |
|
100
|
|
|
*/ |
|
101
|
|
|
public function map($callback) |
|
102
|
|
|
{ |
|
103
|
|
|
$result = new self(); |
|
104
|
|
|
foreach ($this as $point) { |
|
105
|
|
|
$value = $this[$point]; |
|
106
|
|
|
if (call_user_func($callback, $point, $value)) { |
|
107
|
|
|
$result->attach($point, $value); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $result; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param callable $callback |
|
116
|
|
|
* @param mixed $initial |
|
117
|
|
|
* |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
public function reduce($callback, $initial) |
|
121
|
|
|
{ |
|
122
|
|
|
foreach ($this as $point) { |
|
123
|
|
|
$initial = call_user_func($callback, $point, $this[$point]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $initial; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return Line |
|
131
|
|
|
*/ |
|
132
|
|
|
public function smallestRectangularEnvelope() |
|
133
|
|
|
{ |
|
134
|
|
|
$minX = $minY = $maxX = $maxY = null; |
|
135
|
|
|
/** @var Point $point */ |
|
136
|
|
|
foreach ($this as $point) { |
|
137
|
|
|
if ($minX === null || $minX > $point->getX()) { |
|
138
|
|
|
$minX = $point->getX(); |
|
139
|
|
|
} |
|
140
|
|
|
if ($minY === null || $minY > $point->getY()) { |
|
141
|
|
|
$minY = $point->getY(); |
|
142
|
|
|
} |
|
143
|
|
|
if ($maxX === null || $maxX < $point->getX()) { |
|
144
|
|
|
$maxX = $point->getX(); |
|
145
|
|
|
} |
|
146
|
|
|
if ($maxY === null || $maxY < $point->getY()) { |
|
147
|
|
|
$maxY = $point->getY(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return new Line(new Point($minX, $minY), new Point($maxX, $maxY)); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|