ameliabooking
/
vendor
/
bacon
/
bacon-qr-code
/
src
/
Renderer
/
Module
/
EdgeIterator
Last commit date
Edge.php
9 months ago
EdgeIterator.php
9 months ago
Edge.php
101 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | |
| 4 | namespace BaconQrCode\Renderer\Module\EdgeIterator; |
| 5 | |
| 6 | final class Edge |
| 7 | { |
| 8 | /** |
| 9 | * @var bool |
| 10 | */ |
| 11 | private $positive; |
| 12 | |
| 13 | /** |
| 14 | * @var array<int[]> |
| 15 | */ |
| 16 | private $points = []; |
| 17 | |
| 18 | /** |
| 19 | * @var array<int[]>|null |
| 20 | */ |
| 21 | private $simplifiedPoints; |
| 22 | |
| 23 | /** |
| 24 | * @var int |
| 25 | */ |
| 26 | private $minX = PHP_INT_MAX; |
| 27 | |
| 28 | /** |
| 29 | * @var int |
| 30 | */ |
| 31 | private $minY = PHP_INT_MAX; |
| 32 | |
| 33 | /** |
| 34 | * @var int |
| 35 | */ |
| 36 | private $maxX = -1; |
| 37 | |
| 38 | /** |
| 39 | * @var int |
| 40 | */ |
| 41 | private $maxY = -1; |
| 42 | |
| 43 | public function __construct(bool $positive) |
| 44 | { |
| 45 | $this->positive = $positive; |
| 46 | } |
| 47 | |
| 48 | public function addPoint(int $x, int $y) : void |
| 49 | { |
| 50 | $this->points[] = [$x, $y]; |
| 51 | $this->minX = min($this->minX, $x); |
| 52 | $this->minY = min($this->minY, $y); |
| 53 | $this->maxX = max($this->maxX, $x); |
| 54 | $this->maxY = max($this->maxY, $y); |
| 55 | } |
| 56 | |
| 57 | public function isPositive() : bool |
| 58 | { |
| 59 | return $this->positive; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return array<int[]> |
| 64 | */ |
| 65 | public function getPoints() : array |
| 66 | { |
| 67 | return $this->points; |
| 68 | } |
| 69 | |
| 70 | public function getMaxX() : int |
| 71 | { |
| 72 | return $this->maxX; |
| 73 | } |
| 74 | |
| 75 | public function getSimplifiedPoints() : array |
| 76 | { |
| 77 | if (null !== $this->simplifiedPoints) { |
| 78 | return $this->simplifiedPoints; |
| 79 | } |
| 80 | |
| 81 | $points = []; |
| 82 | $length = count($this->points); |
| 83 | |
| 84 | for ($i = 0; $i < $length; ++$i) { |
| 85 | $previousPoint = $this->points[(0 === $i ? $length : $i) - 1]; |
| 86 | $nextPoint = $this->points[($length - 1 === $i ? -1 : $i) + 1]; |
| 87 | $currentPoint = $this->points[$i]; |
| 88 | |
| 89 | if (($previousPoint[0] === $currentPoint[0] && $currentPoint[0] === $nextPoint[0]) |
| 90 | || ($previousPoint[1] === $currentPoint[1] && $currentPoint[1] === $nextPoint[1]) |
| 91 | ) { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | $points[] = $currentPoint; |
| 96 | } |
| 97 | |
| 98 | return $this->simplifiedPoints = $points; |
| 99 | } |
| 100 | } |
| 101 |