5 Copyright (c) 2006-2008 Ulrich Mierendorff
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:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
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.
27 - improved the rendering speed by ~20%
29 - Thanks to Matthias Mächler for fixing some small errors:
30 * uninitialized variables
31 * deprecated passing of $img reference in imageSmoothArc ()
34 Release of rewritten script
38 function imageSmoothArcDrawSegment (&$img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $start, $stop, $seg)
40 // Originally written from scratch by Ulrich Mierendorff, 06/2006
41 // Rewritten and improved, 04/2007, 07/2007
43 // Please do not use THIS function directly. Scroll down to imageSmoothArc(...).
45 $fillColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], $color[3] );
47 $xStart = abs($a * cos($start));
48 $yStart = abs($b * sin($start));
49 $xStop = abs($a * cos($stop));
50 $yStop = abs($b * sin($stop));
56 $dyStart = $yStart/$xStart;
58 $dyStop = $yStop/$xStop;
60 $dxStart = $xStart/$yStart;
62 $dxStop = $xStop/$yStop;
63 if (abs($xStart) >= abs($yStart)) {
68 if ($xStop >= $yStop) {
73 //$xp = +1; $yp = -1; $xa = +1; $ya = 0;
74 for ( $x = 0; $x < $a; $x +
= 1 ) {
75 /*$y = $b * sqrt( 1 - ($x*$x)/($a*$a) );
77 $error = $y - (int)($y);
80 $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );*/
86 $error1 = $_y1 - (int)($_y1);
87 $error2 = 1 - $_y2 +
(int)$_y2;
93 $error1 = 1 - $_y1 +
(int)$_y1;
94 $error2 = $_y2 - (int)($_y2);
100 $diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 );
102 $diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 );
105 if ($seg == 0 ||
$seg == 2)
108 if (!($start > $i*M_PI
/2 && $x > $xStart)) {
110 $xp = +
1; $yp = -1; $xa = +
1; $ya = 0;
112 $xp = -1; $yp = +
1; $xa = 0; $ya = +
1;
114 if ( $stop < ($i+
1)*(M_PI
/2) && $x <= $xStop ) {
115 $diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 );
116 $y1 = $_y1; if ($aaStopX) imageSetPixel($img, $cx+
$xp*($x)+
$xa, $cy+
$yp*($y1+
1)+
$ya, $diffColor1);
119 $y = $b * sqrt( 1 - ($x*$x)/($a*$a) );
120 $error = $y - (int)($y);
122 $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );
123 $y1 = $y; if ($x < $aaAngleX ) imageSetPixel($img, $cx+
$xp*$x+
$xa, $cy+
$yp*($y1+
1)+
$ya, $diffColor);
125 if ($start > $i*M_PI
/2 && $x <= $xStart) {
126 $diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 );
127 $y2 = $_y2; if ($aaStartX) imageSetPixel($img, $cx+
$xp*$x+
$xa, $cy+
$yp*($y2-1)+
$ya, $diffColor2);
131 if ($y2 <= $y1) imageLine($img, $cx+
$xp*$x+
$xa, $cy+
$yp*$y1+
$ya , $cx+
$xp*$x+
$xa, $cy+
$yp*$y2+
$ya, $fillColor);
135 if ($seg == 1 ||
$seg == 3)
138 if (!($stop < ($i+
1)*M_PI
/2 && $x > $xStop)) {
140 $xp = -1; $yp = -1; $xa = 0; $ya = 0;
142 $xp = +
1; $yp = +
1; $xa = 1; $ya = 1;
144 if ( $start > $i*M_PI
/2 && $x < $xStart ) {
145 $diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 );
146 $y1 = $_y2; if ($aaStartX) imageSetPixel($img, $cx+
$xp*$x+
$xa, $cy+
$yp*($y1+
1)+
$ya, $diffColor2);
149 $y = $b * sqrt( 1 - ($x*$x)/($a*$a) );
150 $error = $y - (int)($y);
152 $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );
153 $y1 = $y; if ($x < $aaAngleX ) imageSetPixel($img, $cx+
$xp*$x+
$xa, $cy+
$yp*($y1+
1)+
$ya, $diffColor);
155 if ($stop < ($i+
1)*M_PI
/2 && $x <= $xStop) {
156 $diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 );
157 $y2 = $_y1; if ($aaStopX) imageSetPixel($img, $cx+
$xp*$x+
$xa, $cy+
$yp*($y2-1)+
$ya, $diffColor1);
161 if ($y2 <= $y1) imageLine($img, $cx+
$xp*$x+
$xa, $cy+
$yp*$y1+
$ya, $cx+
$xp*$x+
$xa, $cy+
$yp*$y2+
$ya, $fillColor);
168 for ( $y = 0; $y < $b; $y +
= 1 ) {
169 /*$x = $a * sqrt( 1 - ($y*$y)/($b*$b) );
171 $error = $x - (int)($x);
174 $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );
178 if ($yStart > $yStop)
180 $error1 = $_x1 - (int)($_x1);
181 $error2 = 1 - $_x2 +
(int)$_x2;
187 $error1 = 1 - $_x1 +
(int)$_x1;
188 $error2 = $_x2 - (int)($_x2);
194 $diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 );
196 $diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 );
199 if ($seg == 0 ||
$seg == 2)
202 if (!($start > $i*M_PI
/2 && $y > $yStop)) {
204 $xp = +
1; $yp = -1; $xa = 1; $ya = 0;
206 $xp = -1; $yp = +
1; $xa = 0; $ya = 1;
208 if ( $stop < ($i+
1)*(M_PI
/2) && $y <= $yStop ) {
209 $diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 );
210 $x1 = $_x1; if (!$aaStopX) imageSetPixel($img, $cx+
$xp*($x1-1)+
$xa, $cy+
$yp*($y)+
$ya, $diffColor1);
212 if ($start > $i*M_PI
/2 && $y < $yStart) {
213 $diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 );
214 $x2 = $_x2; if (!$aaStartX) imageSetPixel($img, $cx+
$xp*($x2+
1)+
$xa, $cy+
$yp*($y)+
$ya, $diffColor2);
216 $x = $a * sqrt( 1 - ($y*$y)/($b*$b) );
217 $error = $x - (int)($x);
219 $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );
220 $x1 = $x; if ($y < $aaAngleY && $y <= $yStop ) imageSetPixel($img, $cx+
$xp*($x1+
1)+
$xa, $cy+
$yp*$y+
$ya, $diffColor);
225 if ($seg == 1 ||
$seg == 3)
228 if (!($stop < ($i+
1)*M_PI
/2 && $y > $yStart)) {
230 $xp = -1; $yp = -1; $xa = 0; $ya = 0;
232 $xp = +
1; $yp = +
1; $xa = 1; $ya = 1;
234 if ( $start > $i*M_PI
/2 && $y < $yStart ) {
235 $diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 );
236 $x1 = $_x2; if (!$aaStartX) imageSetPixel($img, $cx+
$xp*($x1-1)+
$xa, $cy+
$yp*$y+
$ya, $diffColor2);
238 if ($stop < ($i+
1)*M_PI
/2 && $y <= $yStop) {
239 $diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 );
240 $x2 = $_x1; if (!$aaStopX) imageSetPixel($img, $cx+
$xp*($x2+
1)+
$xa, $cy+
$yp*$y+
$ya, $diffColor1);
242 $x = $a * sqrt( 1 - ($y*$y)/($b*$b) );
243 $error = $x - (int)($x);
245 $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );
246 $x1 = $x; if ($y < $aaAngleY && $y < $yStart) imageSetPixel($img,$cx+
$xp*($x1+
1)+
$xa, $cy+
$yp*$y+
$ya, $diffColor);
254 function imageSmoothArc ( &$img, $cx, $cy, $w, $h, $color, $start, $stop)
256 // Originally written from scratch by Ulrich Mierendorff, 06/2006
257 // Rewritten and improved, 04/2007, 07/2007
258 // compared to old version:
259 // + Support for transparency added
260 // + Improved quality of edges & antialiasing
262 // note: This function does not represent the fastest way to draw elliptical
263 // arcs. It was written without reading any papers on that subject. Better
264 // algorithms may be twice as fast or even more.
266 // what it cannot do: It does not support outlined arcs, only filled
269 // $cx - Center of ellipse, X-coord
270 // $cy - Center of ellipse, Y-coord
271 // $w - Width of ellipse ($w >= 2)
272 // $h - Height of ellipse ($h >= 2 )
273 // $color - Color of ellipse as a four component array with RGBA
274 // $start - Starting angle of the arc, no limited range!
275 // $stop - Stop angle of the arc, no limited range!
276 // $start _can_ be greater than $stop!
277 // If any value is not in the given range, results are undefined!
279 // This script does not use any special algorithms, everything is completely
280 // written from scratch; see http://de.wikipedia.org/wiki/Ellipse for formulas.
287 while ($start > 2*M_PI
)
290 while ($stop > 2*M_PI
)
296 imageSmoothArc ( $img, $cx, $cy, $w, $h, $color, $start, 2*M_PI
);
297 imageSmoothArc ( $img, $cx, $cy, $w, $h, $color, 0, $stop);
301 $a = 1.0*round ($w/2);
302 $b = 1.0*round ($h/2);
303 $cx = 1.0*round ($cx);
304 $cy = 1.0*round ($cy);
306 $aaAngle = atan(($b*$b)/($a*$a)*tan(0.25*M_PI
));
307 $aaAngleX = $a*cos($aaAngle);
308 $aaAngleY = $b*sin($aaAngle);
310 $a -= 0.5; // looks better...
313 for ($i=0; $i<4;$i++
)
315 if ($start < ($i+
1)*M_PI
/2)
317 if ($start > $i*M_PI
/2)
319 if ($stop > ($i+
1)*M_PI
/2)
321 imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY , $color, $start, ($i+
1)*M_PI
/2, $i);
325 imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $start, $stop, $i);
331 if ($stop > ($i+
1)*M_PI
/2)
333 imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $i*M_PI
/2, ($i+
1)*M_PI
/2, $i);
337 imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $i*M_PI
/2, $stop, $i);