Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Matrix
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
8 / 8
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 identity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 multiply
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 translate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 scale
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 rotate
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 transformPoint
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Geometry;
6
7/**
8 * Affine transformation matrix [a b c d e f].
9 * Transforms a point: x' = a*x + c*y + e, y' = b*x + d*y + f
10 */
11final class Matrix
12{
13    public function __construct(
14        public readonly float $a = 1.0,
15        public readonly float $b = 0.0,
16        public readonly float $c = 0.0,
17        public readonly float $d = 1.0,
18        public readonly float $e = 0.0,
19        public readonly float $f = 0.0,
20    ) {}
21
22    public static function identity(): self
23    {
24        return new self();
25    }
26
27    /** @return array<int, float> */
28    public function toArray(): array
29    {
30        return [$this->a, $this->b, $this->c, $this->d, $this->e, $this->f];
31    }
32
33    public function multiply(self $m): self
34    {
35        return new self(
36            $this->a * $m->a + $this->b * $m->c,
37            $this->a * $m->b + $this->b * $m->d,
38            $this->c * $m->a + $this->d * $m->c,
39            $this->c * $m->b + $this->d * $m->d,
40            $this->e * $m->a + $this->f * $m->c + $m->e,
41            $this->e * $m->b + $this->f * $m->d + $m->f,
42        );
43    }
44
45    public function translate(float $tx, float $ty): self
46    {
47        return $this->multiply(new self(1, 0, 0, 1, $tx, $ty));
48    }
49
50    public function scale(float $sx, float $sy): self
51    {
52        return $this->multiply(new self($sx, 0, 0, $sy, 0, 0));
53    }
54
55    public function rotate(float $degrees): self
56    {
57        $r = deg2rad($degrees);
58        $cos = cos($r);
59        $sin = sin($r);
60        return $this->multiply(new self($cos, $sin, -$sin, $cos, 0, 0));
61    }
62
63    public function transformPoint(Point $p): Point
64    {
65        return new Point(
66            $this->a * $p->x + $this->c * $p->y + $this->e,
67            $this->b * $p->x + $this->d * $p->y + $this->f,
68        );
69    }
70}