Debian Codegraph @

src/Renderer/Module/EdgeIterator/Edge.php

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