Passed
Push — v5 ( 6b9eea...33c1e2 )
by smiley
09:46
created

FinderPattern::getCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * Class FinderPattern
4
 *
5
 * @created      17.01.2021
6
 * @author       ZXing Authors
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2021 Smiley
9
 * @license      Apache-2.0
10
 */
11
12
namespace chillerlan\QRCode\Detector;
13
14
use function sqrt;
15
16
/**
17
 * <p>Encapsulates a finder pattern, which are the three square patterns found in
18
 * the corners of QR Codes. It also encapsulates a count of similar finder patterns,
19
 * as a convenience to the finder's bookkeeping.</p>
20
 *
21
 * @author Sean Owen
22
 */
23
final class FinderPattern extends ResultPoint{
24
25
	private int $count;
26
27
	public function __construct(float $posX, float $posY, float $estimatedModuleSize, int $count = null){
28
		parent::__construct($posX, $posY, $estimatedModuleSize);
29
30
		$this->count = $count ?? 1;
31
	}
32
33
	public function getCount():int{
34
		return $this->count;
35
	}
36
37
	/**
38
	 * @param \chillerlan\QRCode\Detector\FinderPattern $b second pattern
39
	 *
40
	 * @return float distance between two points
41
	 */
42
	public function getDistance(FinderPattern $b):float{
43
		return self::distance($this->x, $this->y, $b->x, $b->y);
44
	}
45
46
	/**
47
	 * Get square of distance between a and b.
48
	 */
49
	public function getSquaredDistance(FinderPattern $b):float{
50
		return self::squaredDistance($this->x, $this->y, $b->x, $b->y);
51
	}
52
53
	/**
54
	 * Combines this object's current estimate of a finder pattern position and module size
55
	 * with a new estimate. It returns a new {@code FinderPattern} containing a weighted average
56
	 * based on count.
57
	 */
58
	public function combineEstimate(float $i, float $j, float $newModuleSize):FinderPattern{
59
		$combinedCount = $this->count + 1;
60
61
		return new self(
62
			($this->count * $this->x + $j) / $combinedCount,
63
			($this->count * $this->y + $i) / $combinedCount,
64
			($this->count * $this->estimatedModuleSize + $newModuleSize) / $combinedCount,
65
			$combinedCount
66
		);
67
	}
68
69
	private static function squaredDistance(float $aX, float $aY, float $bX, float $bY):float{
70
		$xDiff = $aX - $bX;
71
		$yDiff = $aY - $bY;
72
73
		return $xDiff * $xDiff + $yDiff * $yDiff;
74
	}
75
76
	public static function distance(float $aX, float $aY, float $bX, float $bY):float{
77
		return sqrt(self::squaredDistance($aX, $aY, $bX, $bY));
78
	}
79
80
}
81