FinderPattern   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 67
rs 10
c 1
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSquaredDistance() 0 2 1
A distance() 0 2 1
A getCount() 0 2 1
A combineEstimate() 0 8 1
A __construct() 0 4 1
A getDistance() 0 2 1
A squaredDistance() 0 5 1
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
 * 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.
20
 *
21
 * @author Sean Owen
22
 */
23
final class FinderPattern extends ResultPoint{
24
25
	private int $count;
26
27
	/**
28
	 *
29
	 */
30
	public function __construct(float $posX, float $posY, float $estimatedModuleSize, int $count = null){
31
		parent::__construct($posX, $posY, $estimatedModuleSize);
32
33
		$this->count = ($count ?? 1);
34
	}
35
36
	/**
37
	 *
38
	 */
39
	public function getCount():int{
40
		return $this->count;
41
	}
42
43
	/**
44
	 * @param \chillerlan\QRCode\Detector\FinderPattern $b second pattern
45
	 *
46
	 * @return float distance between two points
47
	 */
48
	public function getDistance(FinderPattern $b):float{
49
		return self::distance($this->x, $this->y, $b->x, $b->y);
50
	}
51
52
	/**
53
	 * Get square of distance between a and b.
54
	 */
55
	public function getSquaredDistance(FinderPattern $b):float{
56
		return self::squaredDistance($this->x, $this->y, $b->x, $b->y);
57
	}
58
59
	/**
60
	 * Combines this object's current estimate of a finder pattern position and module size
61
	 * with a new estimate. It returns a new FinderPattern containing a weighted average
62
	 * based on count.
63
	 */
64
	public function combineEstimate(float $i, float $j, float $newModuleSize):self{
65
		$combinedCount = ($this->count + 1);
66
67
		return new self(
68
			($this->count * $this->x + $j) / $combinedCount,
69
			($this->count * $this->y + $i) / $combinedCount,
70
			($this->count * $this->estimatedModuleSize + $newModuleSize) / $combinedCount,
71
			$combinedCount
72
		);
73
	}
74
75
	/**
76
	 *
77
	 */
78
	private static function squaredDistance(float $aX, float $aY, float $bX, float $bY):float{
79
		$xDiff = ($aX - $bX);
80
		$yDiff = ($aY - $bY);
81
82
		return ($xDiff * $xDiff + $yDiff * $yDiff);
83
	}
84
85
	/**
86
	 *
87
	 */
88
	public static function distance(float $aX, float $aY, float $bX, float $bY):float{
89
		return sqrt(self::squaredDistance($aX, $aY, $bX, $bY));
90
	}
91
92
}
93