Test Failed
Pull Request — master (#144)
by
unknown
05:06
created

RotImage::FilledRectangle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 2
nop 4
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * JPGraph v4.0.3
5
 */
6
7
namespace Amenadiel\JpGraph\Image;
8
9
/**
10
 * @class RotImage
11
 * // Description: Exactly as Image but draws the image at
12
 * // a specified angle around a specified rotation point.
13
 */
14
class RotImage extends Image
15
{
16
    public $a      = 0;
17
    public $dx     = 0;
18
    public $dy     = 0;
19
    public $transx = 0;
20
    public $transy = 0;
21
    private $m     = [];
22
23 21
    public function __construct($aWidth, $aHeight, $a = 0, $aFormat = DEFAULT_GFORMAT, $aSetAutoMargin = true)
24
    {
25 21
        parent::__construct($aWidth, $aHeight, $aFormat, $aSetAutoMargin);
26
        $this->dx = $this->left_margin + $this->plotwidth / 2;
27
        $this->dy = $this->top_margin + $this->plotheight / 2;
28
        $this->SetAngle($a);
29
    }
30
31
    public function SetCenter($dx, $dy)
32
    {
33
        $old_dx   = $this->dx;
34
        $old_dy   = $this->dy;
35
        $this->dx = $dx;
36
        $this->dy = $dy;
37
        $this->SetAngle($this->a);
38
39
        return [$old_dx, $old_dy];
40
    }
41
42
    public function SetTranslation($dx, $dy)
43
    {
44
        $old          = [$this->transx, $this->transy];
45
        $this->transx = $dx;
46
        $this->transy = $dy;
47
48
        return $old;
49
    }
50
51
    public function UpdateRotMatrice()
52
    {
53
        $a = $this->a;
54
        $a *= M_PI / 180;
55
        $sa = sin($a);
56
        $ca = cos($a);
57
        // Create the rotation matrix
58
        $this->m[0][0] = $ca;
59
        $this->m[0][1] = -$sa;
60
        $this->m[0][2] = $this->dx * (1 - $ca) + $sa * $this->dy;
61
        $this->m[1][0] = $sa;
62
        $this->m[1][1] = $ca;
63
        $this->m[1][2] = $this->dy * (1 - $ca) - $sa * $this->dx;
64
    }
65
66
    public function SetAngle($a)
67
    {
68
        $tmp     = $this->a;
69
        $this->a = $a;
70
        $this->UpdateRotMatrice();
71
72
        return $tmp;
73
    }
74
75
    public function Circle($xc, $yc, $r)
76
    {
77
        list($xc, $yc) = $this->Rotate($xc, $yc);
78
        parent::Circle($xc, $yc, $r);
79
    }
80
81
    public function FilledCircle($xc, $yc, $r)
82
    {
83
        list($xc, $yc) = $this->Rotate($xc, $yc);
84
        parent::FilledCircle($xc, $yc, $r);
85
    }
86
87
    public function Arc($xc, $yc, $w, $h, $s, $e)
88
    {
89
        list($xc, $yc) = $this->Rotate($xc, $yc);
90
        $s += $this->a;
91
        $e += $this->a;
92
        parent::Arc($xc, $yc, $w, $h, $s, $e);
93
    }
94
95
    public function FilledArc($xc, $yc, $w, $h, $s, $e, $style = '')
96
    {
97
        list($xc, $yc) = $this->Rotate($xc, $yc);
98
        $s += $this->a;
99
        $e += $this->a;
100
        parent::FilledArc($xc, $yc, $w, $h, $s, $e);
101
    }
102
103
    public function SetMargin($lm, $rm, $tm, $bm)
104
    {
105
        parent::SetMargin($lm, $rm, $tm, $bm);
106
        $this->dx = $this->left_margin + $this->plotwidth / 2;
107
        $this->dy = $this->top_margin + $this->plotheight / 2;
108
        $this->UpdateRotMatrice();
109
    }
110
111 21
    public function Rotate($x, $y)
112
    {
113
        // Optimization. Ignore rotation if Angle==0 || Angle==360
114 21
        if ($this->a == 0 || $this->a == 360) {
115 21
            return [$x + $this->transx, $y + $this->transy];
116
        }
117
        $x1 = round($this->m[0][0] * $x + $this->m[0][1] * $y, 1) + $this->m[0][2] + $this->transx;
118
        $y1 = round($this->m[1][0] * $x + $this->m[1][1] * $y, 1) + $this->m[1][2] + $this->transy;
119
120
        return [$x1, $y1];
121
    }
122
123
    public function CopyMerge($fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth = -1, $fromHeight = -1, $aMix = 100)
124
    {
125
        list($toX, $toY) = $this->Rotate($toX, $toY);
126
        parent::CopyMerge($fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth, $fromHeight, $aMix);
127
    }
128
129 21
    public function ArrRotate($pnts)
130
    {
131 21
        $n = safe_count($pnts) - 1;
132 21
        for ($i = 0; $i < $n; $i += 2) {
133 21
            list($x, $y)  = $this->Rotate($pnts[$i], $pnts[$i + 1]);
134 21
            $pnts[$i]     = $x;
135 21
            $pnts[$i + 1] = $y;
136
        }
137
138 21
        return $pnts;
139
    }
140
141
    public function DashedLine($x1, $y1, $x2, $y2, $dash_length = 1, $dash_space = 4)
142
    {
143
        list($x1, $y1) = $this->Rotate($x1, $y1);
144
        list($x2, $y2) = $this->Rotate($x2, $y2);
145
        parent::DashedLine($x1, $y1, $x2, $y2, $dash_length, $dash_space);
146
    }
147
148
    public function Line($x1, $y1, $x2, $y2)
149
    {
150
        list($x1, $y1) = $this->Rotate($x1, $y1);
151
        list($x2, $y2) = $this->Rotate($x2, $y2);
152
        parent::Line($x1, $y1, $x2, $y2);
153
    }
154
155
    public function Rectangle($x1, $y1, $x2, $y2)
156
    {
157
        // Rectangle uses Line() so it will be rotated through that call
158
        parent::Rectangle($x1, $y1, $x2, $y2);
159
    }
160
161 21
    public function FilledRectangle($x1, $y1, $x2, $y2)
162
    {
163 21
        if ($y1 == $y2 || $x1 == $x2) {
164
            $this->Line($x1, $y1, $x2, $y2);
165
        } else {
166 21
            $this->FilledPolygon([$x1, $y1, $x2, $y1, $x2, $y2, $x1, $y2]);
167
        }
168
    }
169
170
    public function Polygon($pnts, $closed = false, $fast = false)
171
    {
172
        // Polygon uses Line() so it will be rotated through that call unless
173
        // fast drawing routines are used in which case a rotate is needed
174
        if ($fast) {
175
            parent::Polygon($this->ArrRotate($pnts));
176
        } else {
177
            parent::Polygon($pnts, $closed, $fast);
178
        }
179
    }
180
181 21
    public function FilledPolygon($pnts)
182
    {
183 21
        parent::FilledPolygon($this->ArrRotate($pnts));
184
    }
185
186
    public function Point($x, $y)
187
    {
188
        list($xp, $yp) = $this->Rotate($x, $y);
189
        parent::Point($xp, $yp);
190
    }
191
192
    public function StrokeText($x, $y, $txt, $dir = 0, $paragraph_align = 'left', $debug = false)
193
    {
194
        list($xp, $yp) = $this->Rotate($x, $y);
195
196
        return parent::StrokeText($xp, $yp, $txt, $dir, $paragraph_align, $debug);
197
    }
198
}
199