EpsImageBackEnd.php
8 months ago
ImageBackEndInterface.php
8 months ago
ImagickImageBackEnd.php
8 months ago
SvgImageBackEnd.php
8 months ago
TransformationMatrix.php
8 months ago
TransformationMatrix.php
69 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | |
| 4 | namespace BaconQrCode\Renderer\Image; |
| 5 | |
| 6 | final class TransformationMatrix |
| 7 | { |
| 8 | /** |
| 9 | * @var float[] |
| 10 | */ |
| 11 | private $values; |
| 12 | |
| 13 | public function __construct() |
| 14 | { |
| 15 | $this->values = [1, 0, 0, 1, 0, 0]; |
| 16 | } |
| 17 | |
| 18 | public function multiply(self $other) : self |
| 19 | { |
| 20 | $matrix = new self(); |
| 21 | $matrix->values[0] = $this->values[0] * $other->values[0] + $this->values[2] * $other->values[1]; |
| 22 | $matrix->values[1] = $this->values[1] * $other->values[0] + $this->values[3] * $other->values[1]; |
| 23 | $matrix->values[2] = $this->values[0] * $other->values[2] + $this->values[2] * $other->values[3]; |
| 24 | $matrix->values[3] = $this->values[1] * $other->values[2] + $this->values[3] * $other->values[3]; |
| 25 | $matrix->values[4] = $this->values[0] * $other->values[4] + $this->values[2] * $other->values[5] |
| 26 | + $this->values[4]; |
| 27 | $matrix->values[5] = $this->values[1] * $other->values[4] + $this->values[3] * $other->values[5] |
| 28 | + $this->values[5]; |
| 29 | |
| 30 | return $matrix; |
| 31 | } |
| 32 | |
| 33 | public static function scale(float $size) : self |
| 34 | { |
| 35 | $matrix = new self(); |
| 36 | $matrix->values = [$size, 0, 0, $size, 0, 0]; |
| 37 | return $matrix; |
| 38 | } |
| 39 | |
| 40 | public static function translate(float $x, float $y) : self |
| 41 | { |
| 42 | $matrix = new self(); |
| 43 | $matrix->values = [1, 0, 0, 1, $x, $y]; |
| 44 | return $matrix; |
| 45 | } |
| 46 | |
| 47 | public static function rotate(int $degrees) : self |
| 48 | { |
| 49 | $matrix = new self(); |
| 50 | $rad = deg2rad($degrees); |
| 51 | $matrix->values = [cos($rad), sin($rad), -sin($rad), cos($rad), 0, 0]; |
| 52 | return $matrix; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * Applies this matrix onto a point and returns the resulting viewport point. |
| 58 | * |
| 59 | * @return float[] |
| 60 | */ |
| 61 | public function apply(float $x, float $y) : array |
| 62 | { |
| 63 | return [ |
| 64 | $x * $this->values[0] + $y * $this->values[2] + $this->values[4], |
| 65 | $x * $this->values[1] + $y * $this->values[3] + $this->values[5], |
| 66 | ]; |
| 67 | } |
| 68 | } |
| 69 |