1 | <?php |
||
2 | |||
3 | /** |
||
4 | * JPGraph v4.0.3 |
||
5 | */ |
||
6 | |||
7 | namespace Amenadiel\JpGraph\Util; |
||
8 | |||
9 | /** |
||
10 | * @class Bezier |
||
11 | * // Create a new data array from a number of control points |
||
12 | */ |
||
13 | class Bezier |
||
14 | { |
||
15 | /** |
||
16 | * @author Thomas Despoix, openXtrem company |
||
17 | * @license released under QPL |
||
18 | * @abstract Bezier interoplated point generation, |
||
19 | * computed from control points data sets, based on Paul Bourke algorithm : |
||
20 | * http://local.wasp.uwa.edu.au/~pbourke/geometry/bezier/index2.html |
||
21 | */ |
||
22 | private $datax = []; |
||
23 | private $datay = []; |
||
24 | private $n = 0; |
||
25 | |||
26 | 1 | public function __construct($datax, $datay, $attraction_factor = 1) |
|
27 | { |
||
28 | // Adding control point multiple time will raise their attraction power over the curve |
||
29 | 1 | $this->n = safe_count($datax); |
|
30 | 1 | if ($this->n !== safe_count($datay)) { |
|
31 | JpGraphError::RaiseL(19003); |
||
32 | //('Bezier: Number of X and Y coordinates must be the same'); |
||
33 | } |
||
34 | 1 | $idx = 0; |
|
35 | 1 | foreach ($datax as $datumx) { |
|
36 | 1 | for ($i = 0; $i < $attraction_factor; ++$i) { |
|
37 | 1 | $this->datax[$idx++] = $datumx; |
|
38 | } |
||
39 | } |
||
40 | 1 | $idx = 0; |
|
41 | 1 | foreach ($datay as $datumy) { |
|
42 | 1 | for ($i = 0; $i < $attraction_factor; ++$i) { |
|
43 | 1 | $this->datay[$idx++] = $datumy; |
|
44 | } |
||
45 | } |
||
46 | 1 | $this->n *= $attraction_factor; |
|
47 | 1 | } |
|
48 | |||
49 | /** |
||
50 | * Return a set of data points that specifies the bezier curve with $steps points. |
||
51 | * |
||
52 | * @param $steps Number of new points to return |
||
53 | * |
||
54 | * @return array($datax, $datay) |
||
55 | */ |
||
56 | 1 | public function Get($steps) |
|
57 | { |
||
58 | 1 | $datax = []; |
|
59 | 1 | $datay = []; |
|
60 | 1 | for ($i = 0; $i < $steps; ++$i) { |
|
61 | 1 | list($datumx, $datumy) = $this->GetPoint((float) $i / (float) $steps); |
|
62 | 1 | $datax[$i] = $datumx; |
|
63 | 1 | $datay[$i] = $datumy; |
|
64 | } |
||
65 | |||
66 | 1 | $datax[] = end($this->datax); |
|
67 | 1 | $datay[] = end($this->datay); |
|
68 | |||
69 | 1 | return [$datax, $datay]; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Return one point on the bezier curve. $mu is the position on the curve where $mu is in the |
||
74 | * range 0 $mu < 1 where 0 is tha start point and 1 is the end point. Note that every newly computed |
||
75 | * point depends on all the existing points. |
||
76 | * |
||
77 | * @param $mu Position on the bezier curve |
||
78 | * |
||
79 | * @return array($x, $y) |
||
80 | */ |
||
81 | 1 | public function GetPoint($mu) |
|
82 | { |
||
83 | 1 | $n = $this->n - 1; |
|
84 | 1 | $k = 0; |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
85 | 1 | $kn = 0; |
|
0 ignored issues
–
show
|
|||
86 | 1 | $nn = 0; |
|
0 ignored issues
–
show
|
|||
87 | 1 | $nkn = 0; |
|
0 ignored issues
–
show
|
|||
88 | 1 | $blend = 0.0; |
|
0 ignored issues
–
show
|
|||
89 | 1 | $newx = 0.0; |
|
90 | 1 | $newy = 0.0; |
|
91 | |||
92 | 1 | $muk = 1.0; |
|
93 | 1 | $munk = (float) pow(1 - $mu, (float) $n); |
|
94 | |||
95 | 1 | for ($k = 0; $k <= $n; ++$k) { |
|
96 | 1 | $nn = $n; |
|
97 | 1 | $kn = $k; |
|
98 | 1 | $nkn = $n - $k; |
|
99 | 1 | $blend = $muk * $munk; |
|
100 | 1 | $muk *= $mu; |
|
101 | 1 | $munk /= (1 - $mu); |
|
102 | 1 | while ($nn >= 1) { |
|
103 | 1 | $blend *= $nn; |
|
104 | 1 | --$nn; |
|
105 | 1 | if ($kn > 1) { |
|
106 | 1 | $blend /= (float) $kn; |
|
107 | 1 | --$kn; |
|
108 | } |
||
109 | 1 | if ($nkn > 1) { |
|
110 | 1 | $blend /= (float) $nkn; |
|
111 | 1 | --$nkn; |
|
112 | } |
||
113 | } |
||
114 | 1 | $newx += $this->datax[$k] * $blend; |
|
115 | 1 | $newy += $this->datay[$k] * $blend; |
|
116 | } |
||
117 | |||
118 | 1 | return [$newx, $newy]; |
|
119 | } |
||
120 | } |
||
121 |