Debian Codegraph @

src/Renderer/Path/EllipticArc.php

raw
1 <?php
2 declare(strict_types = 1);
3
4 namespace BaconQrCode\Renderer\Path;
5
6 final class EllipticArc implements OperationInterface
7 {
8 private const ZERO_TOLERANCE = 1e-05;
9
10 private float $xRadius;
11 private float $yRadius;
12 private float $xAxisAngle;
13
14 public function __construct(
15 float $xRadius,
16 float $yRadius,
17 float $xAxisAngle,
18 private readonly bool $largeArc,
19 private readonly bool $sweep,
20 private readonly float $x,
21 private readonly float $y
22 ) {
23 $this->xRadius = abs($xRadius);
24 $this->yRadius = abs($yRadius);
25 $this->xAxisAngle = $xAxisAngle % 360;
26 }
27
28 public function getXRadius() : float
29 {
30 return $this->xRadius;
31 }
32
33 public function getYRadius() : float
34 {
35 return $this->yRadius;
36 }
37
38 public function getXAxisAngle() : float
39 {
40 return $this->xAxisAngle;
41 }
42
43 public function isLargeArc() : bool
44 {
45 return $this->largeArc;
46 }
47
48 public function isSweep() : bool
49 {
50 return $this->sweep;
51 }
52
53 public function getX() : float
54 {
55 return $this->x;
56 }
57
58 public function getY() : float
59 {
60 return $this->y;
61 }
62
63 /**
64 * @return self
65 */
66 public function translate(float $x, float $y) : OperationInterface
67 {
68 return new self(
69 $this->xRadius,
70 $this->yRadius,
71 $this->xAxisAngle,
72 $this->largeArc,
73 $this->sweep,
74 $this->x + $x,
75 $this->y + $y
76 );
77 }
78
79 /**
80 * @return self
81 */
82 public function rotate(int $degrees) : OperationInterface
83 {
84 $radians = deg2rad($degrees);
85 $sin = sin($radians);
86 $cos = cos($radians);
87 $xr = $this->x * $cos - $this->y * $sin;
88 $yr = $this->x * $sin + $this->y * $cos;
89 return new self(
90 $this->xRadius,
91 $this->yRadius,
92 $this->xAxisAngle,
93 $this->largeArc,
94 $this->sweep,
95 $xr,
96 $yr
97 );
98 }
99
100 /**
101 * Converts the elliptic arc to multiple curves.
102 *
103 * Since not all image back ends support elliptic arcs, this method allows to convert the arc into multiple curves
104 * resembling the same result.
105 *
106 * @see https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/
107 * @return array<Curve|Line>
108 */
109 public function toCurves(float $fromX, float $fromY) : array
110 {
111 if (sqrt(($fromX - $this->x) ** 2 + ($fromY - $this->y) ** 2) < self::ZERO_TOLERANCE) {
112 return [];
113 }
114
115 if ($this->xRadius < self::ZERO_TOLERANCE || $this->yRadius < self::ZERO_TOLERANCE) {
116 return [new Line($this->x, $this->y)];
117 }
118
119 return $this->createCurves($fromX, $fromY);
120 }
121
122 /**
123 * @return Curve[]
124 */
125 private function createCurves(float $fromX, float $fromY) : array
126 {
127 $xAngle = deg2rad($this->xAxisAngle);
128 list($centerX, $centerY, $radiusX, $radiusY, $startAngle, $deltaAngle) =
129 $this->calculateCenterPointParameters($fromX, $fromY, $xAngle);
130
131 $s = $startAngle;
132 $e = $s + $deltaAngle;
133 $sign = ($e < $s) ? -1 : 1;
134 $remain = abs($e - $s);
135 $p1 = self::point($centerX, $centerY, $radiusX, $radiusY, $xAngle, $s);
136 $curves = [];
137
138 while ($remain > self::ZERO_TOLERANCE) {
139 $step = min($remain, pi() / 2);
140 $signStep = $step * $sign;
141 $p2 = self::point($centerX, $centerY, $radiusX, $radiusY, $xAngle, $s + $signStep);
142
143 $alphaT = tan($signStep / 2);
144 $alpha = sin($signStep) * (sqrt(4 + 3 * $alphaT ** 2) - 1) / 3;
145 $d1 = self::derivative($radiusX, $radiusY, $xAngle, $s);
146 $d2 = self::derivative($radiusX, $radiusY, $xAngle, $s + $signStep);
147
148 $curves[] = new Curve(
149 $p1[0] + $alpha * $d1[0],
150 $p1[1] + $alpha * $d1[1],
151 $p2[0] - $alpha * $d2[0],
152 $p2[1] - $alpha * $d2[1],
153 $p2[0],
154 $p2[1]
155 );
156
157 $s += $signStep;
158 $remain -= $step;
159 $p1 = $p2;
160 }
161
162 return $curves;
163 }
164
165 /**
166 * @return float[]
167 */
168 private function calculateCenterPointParameters(float $fromX, float $fromY, float $xAngle): array
169 {
170 $rX = $this->xRadius;
171 $rY = $this->yRadius;
172
173 // F.6.5.1
174 $dx2 = ($fromX - $this->x) / 2;
175 $dy2 = ($fromY - $this->y) / 2;
176 $x1p = cos($xAngle) * $dx2 + sin($xAngle) * $dy2;
177 $y1p = -sin($xAngle) * $dx2 + cos($xAngle) * $dy2;
178
179 // F.6.5.2
180 $rxs = $rX ** 2;
181 $rys = $rY ** 2;
182 $x1ps = $x1p ** 2;
183 $y1ps = $y1p ** 2;
184 $cr = $x1ps / $rxs + $y1ps / $rys;
185
186 if ($cr > 1) {
187 $s = sqrt($cr);
188 $rX *= $s;
189 $rY *= $s;
190 $rxs = $rX ** 2;
191 $rys = $rY ** 2;
192 }
193
194 $dq = ($rxs * $y1ps + $rys * $x1ps);
195 $pq = ($rxs * $rys - $dq) / $dq;
196 $q = sqrt(max(0, $pq));
197
198 if ($this->largeArc === $this->sweep) {
199 $q = -$q;
200 }
201
202 $cxp = $q * $rX * $y1p / $rY;
203 $cyp = -$q * $rY * $x1p / $rX;
204
205 // F.6.5.3
206 $cx = cos($xAngle) * $cxp - sin($xAngle) * $cyp + ($fromX + $this->x) / 2;
207 $cy = sin($xAngle) * $cxp + cos($xAngle) * $cyp + ($fromY + $this->y) / 2;
208
209 // F.6.5.5
210 $theta = self::angle(1, 0, ($x1p - $cxp) / $rX, ($y1p - $cyp) / $rY);
211
212 // F.6.5.6
213 $delta = self::angle(($x1p - $cxp) / $rX, ($y1p - $cyp) / $rY, (-$x1p - $cxp) / $rX, (-$y1p - $cyp) / $rY);
214 $delta = fmod($delta, pi() * 2);
215
216 if (! $this->sweep && $delta > 0) {
217 $delta -= 2 * pi();
218 } elseif ($this->sweep && $delta < 0) {
219 $delta += 2 * pi();
220 }
221
222 return [$cx, $cy, $rX, $rY, $theta, $delta];
223 }
224
225 private static function angle(float $ux, float $uy, float $vx, float $vy) : float
226 {
227 // F.6.5.4
228 $dot = $ux * $vx + $uy * $vy;
229 $length = sqrt($ux ** 2 + $uy ** 2) * sqrt($vx ** 2 + $vy ** 2);
230 $angle = acos(min(1, max(-1, $dot / $length)));
231
232 if (($ux * $vy - $uy * $vx) < 0) {
233 return -$angle;
234 }
235
236 return $angle;
237 }
238
239 /**
240 * @return float[]
241 */
242 private static function point(
243 float $centerX,
244 float $centerY,
245 float $radiusX,
246 float $radiusY,
247 float $xAngle,
248 float $angle
249 ) : array {
250 return [
251 $centerX + $radiusX * cos($xAngle) * cos($angle) - $radiusY * sin($xAngle) * sin($angle),
252 $centerY + $radiusX * sin($xAngle) * cos($angle) + $radiusY * cos($xAngle) * sin($angle),
253 ];
254 }
255
256 /**
257 * @return float[]
258 */
259 private static function derivative(float $radiusX, float $radiusY, float $xAngle, float $angle) : array
260 {
261 return [
262 -$radiusX * cos($xAngle) * sin($angle) - $radiusY * sin($xAngle) * cos($angle),
263 -$radiusX * sin($xAngle) * sin($angle) + $radiusY * cos($xAngle) * cos($angle),
264 ];
265 }
266 }