source: trunk/xgraph/jpgraph/imageSmoothArc.php

Last change on this file was 42, checked in by marrucho, 10 years ago
File size: 13.8 KB
Line 
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
38function 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    if ($xStop != 0)
58        $dyStop = $yStop/$xStop;
59    if ($yStart != 0)
60        $dxStart = $xStart/$yStart;
61    if ($yStop != 0)
62        $dxStop = $xStop/$yStop;
63    if (abs($xStart) >= abs($yStart)) {
64        $aaStartX = true;
65    } else {
66        $aaStartX = false;
67    }
68    if ($xStop >= $yStop) {
69        $aaStopX = true;
70    } else {
71        $aaStopX = false;
72    }
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) );
76       
77        $error = $y - (int)($y);
78        $y = (int)($y);
79       
80        $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );*/
81       
82        $_y1 = $dyStop*$x;
83        $_y2 = $dyStart*$x;
84        if ($xStart > $xStop)
85        {
86            $error1 = $_y1 - (int)($_y1);
87            $error2 = 1 - $_y2 + (int)$_y2;
88            $_y1 = $_y1-$error1;
89            $_y2 = $_y2+$error2;
90        }
91        else
92        {
93            $error1 = 1 - $_y1 + (int)$_y1;
94            $error2 = $_y2 - (int)($_y2);
95            $_y1 = $_y1+$error1;
96            $_y2 = $_y2-$error2;
97        }
98        /*
99        if ($aaStopX)
100            $diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 );
101        if ($aaStartX)
102            $diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 );
103        */
104       
105        if ($seg == 0 || $seg == 2)
106        {
107            $i = $seg;
108            if (!($start > $i*M_PI/2 && $x > $xStart)) {
109                if ($i == 0) {
110                    $xp = +1; $yp = -1; $xa = +1; $ya = 0;
111                } else {
112                    $xp = -1; $yp = +1; $xa = 0; $ya = +1;
113                }
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);
117                   
118                } else {
119                    $y = $b * sqrt( 1 - ($x*$x)/($a*$a) );
120                    $error = $y - (int)($y);
121                    $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);
124                }
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);
128                } else {
129                    $y2 = 0;
130                }
131                if ($y2 <= $y1) imageLine($img, $cx+$xp*$x+$xa, $cy+$yp*$y1+$ya , $cx+$xp*$x+$xa, $cy+$yp*$y2+$ya, $fillColor);
132            }
133        }
134       
135        if ($seg == 1 || $seg == 3)
136        {
137            $i = $seg;
138            if (!($stop < ($i+1)*M_PI/2 && $x > $xStop)) {
139                if ($i == 1) {
140                    $xp = -1; $yp = -1; $xa = 0; $ya = 0;
141                } else {
142                    $xp = +1; $yp = +1; $xa = 1; $ya = 1;
143                }
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);
147                   
148                } else {
149                    $y = $b * sqrt( 1 - ($x*$x)/($a*$a) );
150                    $error = $y - (int)($y);
151                    $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);
154                }
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);
158                } else {
159                    $y2 = 0;
160                }
161                if ($y2 <= $y1) imageLine($img, $cx+$xp*$x+$xa, $cy+$yp*$y1+$ya, $cx+$xp*$x+$xa, $cy+$yp*$y2+$ya, $fillColor);
162            }
163        }
164    }
165   
166    ///YYYYY
167   
168    for ( $y = 0; $y < $b; $y += 1 ) {
169        /*$x = $a * sqrt( 1 - ($y*$y)/($b*$b) );
170       
171        $error = $x - (int)($x);
172        $x = (int)($x);
173       
174        $diffColor = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error );
175        */
176        $_x1 = $dxStop*$y;
177        $_x2 = $dxStart*$y;
178        if ($yStart > $yStop)
179        {
180            $error1 = $_x1 - (int)($_x1);
181            $error2 = 1 - $_x2 + (int)$_x2;
182            $_x1 = $_x1-$error1;
183            $_x2 = $_x2+$error2;
184        }
185        else
186        {
187            $error1 = 1 - $_x1 + (int)$_x1;
188            $error2 = $_x2 - (int)($_x2);
189            $_x1 = $_x1+$error1;
190            $_x2 = $_x2-$error2;
191        }
192/*
193        if (!$aaStopX)
194            $diffColor1 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error1 );
195        if (!$aaStartX)
196            $diffColor2 = imageColorExactAlpha( $img, $color[0], $color[1], $color[2], 127-(127-$color[3])*$error2 );
197*/
198       
199        if ($seg == 0 || $seg == 2)
200        {
201            $i = $seg;
202            if (!($start > $i*M_PI/2 && $y > $yStop)) {
203                if ($i == 0) {
204                    $xp = +1; $yp = -1; $xa = 1; $ya = 0;
205                } else {
206                    $xp = -1; $yp = +1; $xa = 0; $ya = 1;
207                }
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);
211                } 
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);
215                } else {
216                    $x = $a * sqrt( 1 - ($y*$y)/($b*$b) );
217                    $error = $x - (int)($x);
218                    $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);
221                }
222            }
223        }
224       
225        if ($seg == 1 || $seg == 3)
226        {
227            $i = $seg;
228            if (!($stop < ($i+1)*M_PI/2 && $y > $yStart)) {
229                if ($i == 1) {
230                    $xp = -1; $yp = -1; $xa = 0; $ya = 0;
231                } else {
232                    $xp = +1; $yp = +1; $xa = 1; $ya = 1;
233                }
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);
237                } 
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);
241                } else {
242                    $x = $a * sqrt( 1 - ($y*$y)/($b*$b) );
243                    $error = $x - (int)($x);
244                    $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);
247                }
248            }
249        }
250    }
251}
252
253
254function imageSmoothArc ( &$img, $cx, $cy, $w, $h, $color, $start, $stop)
255{
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
261   
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.
265   
266    // what it cannot do: It does not support outlined arcs, only filled
267   
268    // Parameters:
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!
278   
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.
281   
282    while ($start < 0)
283        $start += 2*M_PI;
284    while ($stop < 0)
285        $stop += 2*M_PI;
286   
287    while ($start > 2*M_PI)
288        $start -= 2*M_PI;
289   
290    while ($stop > 2*M_PI)
291        $stop -= 2*M_PI;
292   
293   
294    if ($start > $stop)
295    {
296        imageSmoothArc ( $img, $cx, $cy, $w, $h, $color, $start, 2*M_PI);
297        imageSmoothArc ( $img, $cx, $cy, $w, $h, $color, 0, $stop);
298        return;
299    }
300   
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);
305   
306    $aaAngle = atan(($b*$b)/($a*$a)*tan(0.25*M_PI));
307    $aaAngleX = $a*cos($aaAngle);
308    $aaAngleY = $b*sin($aaAngle);
309   
310    $a -= 0.5; // looks better...
311    $b -= 0.5;
312   
313    for ($i=0; $i<4;$i++)
314    {
315        if ($start < ($i+1)*M_PI/2)
316        {
317            if ($start > $i*M_PI/2)
318            {
319                if ($stop > ($i+1)*M_PI/2)
320                {
321                    imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY , $color, $start, ($i+1)*M_PI/2, $i);
322                }
323                else
324                {
325                    imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $start, $stop, $i);
326                    break;
327                }
328            }
329            else
330            {
331                if ($stop > ($i+1)*M_PI/2)
332                {
333                    imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $i*M_PI/2, ($i+1)*M_PI/2, $i);
334                }
335                else
336                {
337                    imageSmoothArcDrawSegment($img, $cx, $cy, $a, $b, $aaAngleX, $aaAngleY, $color, $i*M_PI/2, $stop, $i);
338                    break;
339                }
340            }
341        }
342    }
343}
344?>
Note: See TracBrowser for help on using the repository browser.