1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
|
5
|
|
|
Copyright (c) 2006-2008 Ulrich Mierendorff |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a |
8
|
|
|
copy of this software and associated documentation files (the "Software"), |
9
|
|
|
to deal in the Software without restriction, including without limitation |
10
|
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
11
|
|
|
and/or sell copies of the Software, and to permit persons to whom the |
12
|
|
|
Software is furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in |
15
|
|
|
all copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
20
|
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
22
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
23
|
|
|
DEALINGS IN THE SOFTWARE. |
24
|
|
|
|
25
|
|
|
Changelog: |
26
|
|
|
version 1.1 |
27
|
|
|
- improved the rendering speed by ~20% |
28
|
|
|
|
29
|
|
|
- Thanks to Matthias Mächler for fixing some small errors: |
30
|
|
|
* uninitialized variables |
31
|
|
|
* deprecated passing of $img reference in imageSmoothArc () |
32
|
|
|
|
33
|
|
|
version 1.0 |
34
|
|
|
Release of rewritten script |
35
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
function imageSmoothArcDrawSegment(&$img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $start, $stop, $seg) |
39
|
|
|
{ |
40
|
|
|
// Originally written from scratch by Ulrich Mierendorff, 06/2006 |
41
|
|
|
// Rewritten and improved, 04/2007, 07/2007 |
|
|
|
|
42
|
|
|
|
43
|
|
|
// Please do not use THIS function directly. Scroll down to imageSmoothArc(...). |
44
|
|
|
|
45
|
|
|
$fillColor = imageColorExactAlpha($img, $color[0], $color[1], $color[2], $color[3]); |
46
|
|
|
|
47
|
|
|
$xStart = abs($a * cos($start)); |
48
|
|
|
$yStart = abs($b * sin($start)); |
49
|
|
|
$xStop = abs($a * cos($stop)); |
50
|
|
|
$yStop = abs($b * sin($stop)); |
51
|
|
|
$dxStart = 0; |
52
|
|
|
$dyStart = 0; |
53
|
|
|
$dxStop = 0; |
54
|
|
|
$dyStop = 0; |
55
|
|
|
if ($xStart != 0) { |
56
|
|
|
$dyStart = $yStart / $xStart; |
57
|
|
|
} |
58
|
|
|
if ($xStop != 0) { |
59
|
|
|
$dyStop = $yStop / $xStop; |
60
|
|
|
} |
61
|
|
|
if ($yStart != 0) { |
62
|
|
|
$dxStart = $xStart / $yStart; |
63
|
|
|
} |
64
|
|
|
if ($yStop != 0) { |
65
|
|
|
$dxStop = $xStop / $yStop; |
66
|
|
|
} |
67
|
|
|
if (abs($xStart) >= abs($yStart)) { |
68
|
|
|
$aaStartX = true; |
69
|
|
|
} else { |
70
|
|
|
$aaStartX = false; |
71
|
|
|
} |
72
|
|
|
if ($xStop >= $yStop) { |
73
|
|
|
$aaStopX = true; |
74
|
|
|
} else { |
75
|
|
|
$aaStopX = false; |
76
|
|
|
} |
77
|
|
|
//$xp = +1; $yp = -1; $xa = +1; $ya = 0; |
|
|
|
|
78
|
|
|
for ($x = 0; $x < $a; $x += 1) { |
79
|
|
|
/*$y = $b * sqrt( 1 - ($x*$x)/($a*$a) ); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$error = $y - (int)($y); |
82
|
|
|
$y = (int)($y); |
83
|
|
|
|
84
|
|
|
$diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );*/ |
85
|
|
|
|
86
|
|
|
$_y1 = $dyStop * $x; |
87
|
|
|
$_y2 = $dyStart * $x; |
88
|
|
View Code Duplication |
if ($xStart > $xStop) { |
|
|
|
|
89
|
|
|
$error1 = $_y1 - (int)($_y1); |
90
|
|
|
$error2 = 1 - $_y2 + (int)$_y2; |
91
|
|
|
$_y1 = $_y1 - $error1; |
92
|
|
|
$_y2 = $_y2 + $error2; |
93
|
|
|
} else { |
94
|
|
|
$error1 = 1 - $_y1 + (int)$_y1; |
95
|
|
|
$error2 = $_y2 - (int)($_y2); |
96
|
|
|
$_y1 = $_y1 + $error1; |
97
|
|
|
$_y2 = $_y2 - $error2; |
98
|
|
|
} |
99
|
|
|
/* |
|
|
|
|
100
|
|
|
if ($aaStopX) |
101
|
|
|
$diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 ); |
102
|
|
|
if ($aaStartX) |
103
|
|
|
$diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 ); |
104
|
|
|
*/ |
105
|
|
|
|
106
|
|
|
if ($seg == 0 || $seg == 2) { |
107
|
|
|
$i = $seg; |
108
|
|
|
if (!($start > $i * M_PI / 2 && $x > $xStart)) { |
109
|
|
|
if ($i == 0) { |
110
|
|
|
$xp = +1; |
111
|
|
|
$yp = -1; |
112
|
|
|
$xa = +1; |
113
|
|
|
$ya = 0; |
114
|
|
|
} else { |
115
|
|
|
$xp = -1; |
116
|
|
|
$yp = +1; |
117
|
|
|
$xa = 0; |
118
|
|
|
$ya = +1; |
119
|
|
|
} |
120
|
|
|
if ($stop < ($i + 1) * (M_PI / 2) && $x <= $xStop) { |
121
|
|
|
$diffColor1 = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error1); |
122
|
|
|
$y1 = $_y1; |
123
|
|
|
if ($aaStopX) { |
124
|
|
|
imageSetPixel($img, $cx + $xp * ($x) + $xa, $cy + $yp * ($y1 + 1) + $ya, $diffColor1); |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
$y = $b * sqrt(1 - ($x * $x) / ($a * $a)); |
128
|
|
|
$error = $y - (int)($y); |
129
|
|
|
$y = (int)($y); |
130
|
|
|
$diffColor = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error); |
131
|
|
|
$y1 = $y; |
132
|
|
|
if ($x < $aaAngleX) { |
133
|
|
|
imageSetPixel($img, $cx + $xp * $x + $xa, $cy + $yp * ($y1 + 1) + $ya, $diffColor); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
if ($start > $i * M_PI / 2 && $x <= $xStart) { |
137
|
|
|
$diffColor2 = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error2); |
138
|
|
|
$y2 = $_y2; |
139
|
|
|
if ($aaStartX) { |
140
|
|
|
imageSetPixel($img, $cx + $xp * $x + $xa, $cy + $yp * ($y2 - 1) + $ya, $diffColor2); |
141
|
|
|
} |
142
|
|
|
} else { |
143
|
|
|
$y2 = 0; |
144
|
|
|
} |
145
|
|
View Code Duplication |
if ($y2 <= $y1) { |
|
|
|
|
146
|
|
|
imageLine($img, $cx + $xp * $x + $xa, $cy + $yp * $y1 + $ya, $cx + $xp * $x + $xa, $cy + $yp * $y2 + $ya, $fillColor); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($seg == 1 || $seg == 3) { |
152
|
|
|
$i = $seg; |
153
|
|
|
if (!($stop < ($i + 1) * M_PI / 2 && $x > $xStop)) { |
154
|
|
|
if ($i == 1) { |
155
|
|
|
$xp = -1; |
156
|
|
|
$yp = -1; |
157
|
|
|
$xa = 0; |
158
|
|
|
$ya = 0; |
159
|
|
|
} else { |
160
|
|
|
$xp = +1; |
161
|
|
|
$yp = +1; |
162
|
|
|
$xa = 1; |
163
|
|
|
$ya = 1; |
164
|
|
|
} |
165
|
|
|
if ($start > $i * M_PI / 2 && $x < $xStart) { |
166
|
|
|
$diffColor2 = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error2); |
167
|
|
|
$y1 = $_y2; |
168
|
|
|
if ($aaStartX) { |
169
|
|
|
imageSetPixel($img, $cx + $xp * $x + $xa, $cy + $yp * ($y1 + 1) + $ya, $diffColor2); |
170
|
|
|
} |
171
|
|
|
} else { |
172
|
|
|
$y = $b * sqrt(1 - ($x * $x) / ($a * $a)); |
173
|
|
|
$error = $y - (int)($y); |
174
|
|
|
$y = (int) $y; |
175
|
|
|
$diffColor = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error); |
176
|
|
|
$y1 = $y; |
177
|
|
|
if ($x < $aaAngleX) { |
178
|
|
|
imageSetPixel($img, $cx + $xp * $x + $xa, $cy + $yp * ($y1 + 1) + $ya, $diffColor); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
if ($stop < ($i + 1) * M_PI / 2 && $x <= $xStop) { |
182
|
|
|
$diffColor1 = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error1); |
183
|
|
|
$y2 = $_y1; |
184
|
|
|
if ($aaStopX) { |
185
|
|
|
imageSetPixel($img, $cx + $xp * $x + $xa, $cy + $yp * ($y2 - 1) + $ya, $diffColor1); |
186
|
|
|
} |
187
|
|
|
} else { |
188
|
|
|
$y2 = 0; |
189
|
|
|
} |
190
|
|
View Code Duplication |
if ($y2 <= $y1) { |
|
|
|
|
191
|
|
|
imageLine($img, $cx + $xp * $x + $xa, $cy + $yp * $y1 + $ya, $cx + $xp * $x + $xa, $cy + $yp * $y2 + $ya, $fillColor); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
///YYYYY |
198
|
|
|
|
199
|
|
|
for ($y = 0; $y < $b; $y += 1) { |
200
|
|
|
/*$x = $a * sqrt( 1 - ($y*$y)/($b*$b) ); |
|
|
|
|
201
|
|
|
|
202
|
|
|
$error = $x - (int)($x); |
203
|
|
|
$x = (int)($x); |
204
|
|
|
|
205
|
|
|
$diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error ); |
206
|
|
|
*/ |
207
|
|
|
$_x1 = $dxStop * $y; |
208
|
|
|
$_x2 = $dxStart * $y; |
209
|
|
View Code Duplication |
if ($yStart > $yStop) { |
|
|
|
|
210
|
|
|
$error1 = $_x1 - (int)($_x1); |
211
|
|
|
$error2 = 1 - $_x2 + (int)$_x2; |
212
|
|
|
$_x1 = $_x1 - $error1; |
213
|
|
|
$_x2 = $_x2 + $error2; |
214
|
|
|
} else { |
215
|
|
|
$error1 = 1 - $_x1 + (int)$_x1; |
216
|
|
|
$error2 = $_x2 - (int)($_x2); |
217
|
|
|
$_x1 = $_x1 + $error1; |
218
|
|
|
$_x2 = $_x2 - $error2; |
219
|
|
|
} |
220
|
|
|
/* |
|
|
|
|
221
|
|
|
if (!$aaStopX) |
222
|
|
|
$diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 ); |
223
|
|
|
if (!$aaStartX) |
224
|
|
|
$diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 ); |
225
|
|
|
*/ |
226
|
|
|
|
227
|
|
View Code Duplication |
if ($seg == 0 || $seg == 2) { |
|
|
|
|
228
|
|
|
$i = $seg; |
229
|
|
|
if (!($start > $i * M_PI / 2 && $y > $yStop)) { |
230
|
|
|
if ($i == 0) { |
231
|
|
|
$xp = +1; |
232
|
|
|
$yp = -1; |
233
|
|
|
$xa = 1; |
234
|
|
|
$ya = 0; |
235
|
|
|
} else { |
236
|
|
|
$xp = -1; |
237
|
|
|
$yp = +1; |
238
|
|
|
$xa = 0; |
239
|
|
|
$ya = 1; |
240
|
|
|
} |
241
|
|
|
if ($stop < ($i + 1) * (M_PI / 2) && $y <= $yStop) { |
242
|
|
|
$diffColor1 = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error1); |
243
|
|
|
$x1 = $_x1; |
244
|
|
|
if (!$aaStopX) { |
245
|
|
|
imageSetPixel($img, $cx + $xp * ($x1 - 1) + $xa, $cy + $yp * ($y) + $ya, $diffColor1); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
if ($start > $i * M_PI / 2 && $y < $yStart) { |
249
|
|
|
$diffColor2 = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error2); |
250
|
|
|
$x2 = $_x2; |
251
|
|
|
if (!$aaStartX) { |
252
|
|
|
imageSetPixel($img, $cx + $xp * ($x2 + 1) + $xa, $cy + $yp * ($y) + $ya, $diffColor2); |
253
|
|
|
} |
254
|
|
|
} else { |
255
|
|
|
$x = $a * sqrt(1 - ($y * $y) / ($b * $b)); |
256
|
|
|
$error = $x - (int)($x); |
257
|
|
|
$x = (int)($x); |
258
|
|
|
$diffColor = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error); |
259
|
|
|
$x1 = $x; |
260
|
|
|
if ($y < $aaAngleY && $y <= $yStop) { |
261
|
|
|
imageSetPixel($img, $cx + $xp * ($x1 + 1) + $xa, $cy + $yp * $y + $ya, $diffColor); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
View Code Duplication |
if ($seg == 1 || $seg == 3) { |
|
|
|
|
268
|
|
|
$i = $seg; |
269
|
|
|
if (!($stop < ($i + 1) * M_PI / 2 && $y > $yStart)) { |
270
|
|
|
if ($i == 1) { |
271
|
|
|
$xp = -1; |
272
|
|
|
$yp = -1; |
273
|
|
|
$xa = 0; |
274
|
|
|
$ya = 0; |
275
|
|
|
} else { |
276
|
|
|
$xp = +1; |
277
|
|
|
$yp = +1; |
278
|
|
|
$xa = 1; |
279
|
|
|
$ya = 1; |
280
|
|
|
} |
281
|
|
|
if ($start > $i * M_PI / 2 && $y < $yStart) { |
282
|
|
|
$diffColor2 = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error2); |
283
|
|
|
$x1 = $_x2; |
284
|
|
|
if (!$aaStartX) { |
285
|
|
|
imageSetPixel($img, $cx + $xp * ($x1 - 1) + $xa, $cy + $yp * $y + $ya, $diffColor2); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
if ($stop < ($i + 1) * M_PI / 2 && $y <= $yStop) { |
289
|
|
|
$diffColor1 = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error1); |
290
|
|
|
$x2 = $_x1; |
291
|
|
|
if (!$aaStopX) { |
292
|
|
|
imageSetPixel($img, $cx + $xp * ($x2 + 1) + $xa, $cy + $yp * $y + $ya, $diffColor1); |
293
|
|
|
} |
294
|
|
|
} else { |
295
|
|
|
$x = $a * sqrt(1 - ($y * $y) / ($b * $b)); |
296
|
|
|
$error = $x - (int)($x); |
297
|
|
|
$x = (int)($x); |
298
|
|
|
$diffColor = imageColorExactAlpha($img, $color[0], $color[1], $color[2], 127 - (127 - $color[3]) * $error); |
299
|
|
|
$x1 = $x; |
300
|
|
|
if ($y < $aaAngleY && $y < $yStart) { |
301
|
|
|
imageSetPixel($img, $cx + $xp * ($x1 + 1) + $xa, $cy + $yp * $y + $ya, $diffColor); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
|
310
|
|
|
function imageSmoothArc(&$img, $cx, $cy, $w, $h, $color, $start, $stop) |
311
|
|
|
{ |
312
|
|
|
// Originally written from scratch by Ulrich Mierendorff, 06/2006 |
313
|
|
|
// Rewritten and improved, 04/2007, 07/2007 |
|
|
|
|
314
|
|
|
// compared to old version: |
315
|
|
|
// + Support for transparency added |
316
|
|
|
// + Improved quality of edges & antialiasing |
317
|
|
|
|
318
|
|
|
// note: This function does not represent the fastest way to draw elliptical |
319
|
|
|
// arcs. It was written without reading any papers on that subject. Better |
320
|
|
|
// algorithms may be twice as fast or even more. |
321
|
|
|
|
322
|
|
|
// what it cannot do: It does not support outlined arcs, only filled |
323
|
|
|
|
324
|
|
|
// Parameters: |
325
|
|
|
// $cx - Center of ellipse, X-coord |
326
|
|
|
// $cy - Center of ellipse, Y-coord |
327
|
|
|
// $w - Width of ellipse ($w >= 2) |
328
|
|
|
// $h - Height of ellipse ($h >= 2 ) |
329
|
|
|
// $color - Color of ellipse as a four component array with RGBA |
330
|
|
|
// $start - Starting angle of the arc, no limited range! |
331
|
|
|
// $stop - Stop angle of the arc, no limited range! |
332
|
|
|
// $start _can_ be greater than $stop! |
333
|
|
|
// If any value is not in the given range, results are undefined! |
334
|
|
|
|
335
|
|
|
// This script does not use any special algorithms, everything is completely |
336
|
|
|
// written from scratch; see http://de.wikipedia.org/wiki/Ellipse for formulas. |
337
|
|
|
|
338
|
|
|
while ($start < 0) { |
339
|
|
|
$start += 2 * M_PI; |
340
|
|
|
} |
341
|
|
|
while ($stop < 0) { |
342
|
|
|
$stop += 2 * M_PI; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
while ($start > 2 * M_PI) { |
346
|
|
|
$start -= 2 * M_PI; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
while ($stop > 2 * M_PI) { |
350
|
|
|
$stop -= 2 * M_PI; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
|
354
|
|
|
if ($start > $stop) { |
355
|
|
|
imageSmoothArc($img, $cx, $cy, $w, $h, $color, $start, 2 * M_PI); |
356
|
|
|
imageSmoothArc($img, $cx, $cy, $w, $h, $color, 0, $stop); |
357
|
|
|
return; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
$a = 1.0 * round($w / 2); |
361
|
|
|
$b = 1.0 * round($h / 2); |
362
|
|
|
$cx = 1.0 * round($cx); |
363
|
|
|
$cy = 1.0 * round($cy); |
364
|
|
|
|
365
|
|
|
$aaAngle = atan(($b * $b) / ($a * $a) * tan(0.25 * M_PI)); |
366
|
|
|
$aaAngleX = $a * cos($aaAngle); |
367
|
|
|
$aaAngleY = $b * sin($aaAngle); |
368
|
|
|
|
369
|
|
|
$a -= 0.5; // looks better... |
370
|
|
|
$b -= 0.5; |
371
|
|
|
|
372
|
|
|
for ($i=0; $i < 4;$i++) { |
373
|
|
|
if ($start < ($i + 1) * M_PI / 2) { |
374
|
|
|
if ($start > $i * M_PI / 2) { |
375
|
|
|
if ($stop > ($i + 1) * M_PI / 2) { |
376
|
|
|
imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $start, ($i + 1) * M_PI / 2, $i); |
377
|
|
|
} else { |
378
|
|
|
imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $start, $stop, $i); |
379
|
|
|
break; |
380
|
|
|
} |
381
|
|
|
} else { |
382
|
|
|
if ($stop > ($i + 1) * M_PI / 2) { |
383
|
|
|
imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $i * M_PI / 2, ($i + 1) * M_PI / 2, $i); |
384
|
|
|
} else { |
385
|
|
|
imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $i * M_PI / 2, $stop, $i); |
386
|
|
|
break; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.