note 87683 added to function.swfshape.drawarc

From: Date: Tue, 16 Dec 2008 19:57:15 +0000
Subject: note 87683 added to function.swfshape.drawarc
Groups: php.notes 
Request: Send a blank email to php-notes+get-148228@lists.php.net to get a copy of this message
It seems that the ming library up to at least v0.4.2 has some rounding issues with rendering the arc. This is most evident as the radius of the arc gets smaller (try drawing a circle/arc with radius 1 using ming, better yet, try 0.5). The following code renders a much more accurate arc at smaller radii. It also does range checking/normalization on the angles passed in that ming doesn't do. <?php function drawArc( $shape, $r, $startAngle, $endAngle ) { // Normalize the angles $delta = $endAngle - $startAngle; if ( abs($delta) >= 360) $delta = 360; else if ($delta < 0) $delta += 360; else if ($delta == 0) return; $startAngle = fmod($startAngle, 360); /* first determine number of segments, 8 at most */ $nSegs = 1 + (int)round(7 * ($delta / 360)); /* subangle is half the angle of each segment */ $subangle = M_PI * $delta / $nSegs / 360; $angle = M_PI * $startAngle / 180; $x = $r * sin($angle); $y = -$r * cos($angle); $shape->movePen($x, $y); $controlRadius = $r / cos($subangle); for ( $i=0; $i<$nSegs; ++$i ) { $angle += $subangle; $controlx = $controlRadius * sin($angle); $controly = -$controlRadius * cos($angle); $angle += $subangle; $anchorx = ($r*sin($angle)); $anchory = (-$r*cos($angle)); $shape->drawCurve($controlx-$x, $controly-$y, $anchorx-$controlx, $anchory-$controly); $x = $anchorx; $y = $anchory; } } // SWFShape->drawCircle has the same issue with accuracy. It's a simple thing to use the function above to fix this: function drawCircle( $shape, $r ) { drawArc( $shape, $r, 0, 360 ); } // EXAMPLE USAGE: // Set ming defaults ming_setScale(20); ming_useswfversion(7); $movie = new SWFMovie(); $movie->setBackground( 0xff, 0xff, 0xff ); $movie->setRate(31); $sprite = new SWFSprite(); $shape = new SWFShape(); // Ming circles (in green) $shape->setLine( 0, 0, 0xff, 0 ); $shape->movePenTo( 50, 50 ); $shape->drawCircle( 5 ); $shape->movePenTo( 50, 50 ); $shape->drawCircle( 3 ); $shape->movePenTo( 50, 50 ); $shape->drawCircle( 1 ); $shape->movePenTo( 50, 50 ); $shape->drawCircle( 0.5 ); // Drawn with drawCircle function below (in black) $shape->setLine( 0, 0, 0, 0 ); $shape->movePenTo( 50, 50 ); drawCircle( $shape, 5 ); $shape->movePenTo( 50, 50 ); drawCircle( $shape, 3 ); $shape->movePenTo( 50, 50 ); drawCircle( $shape, 1 ); $shape->movePenTo( 50, 50 ); drawCircle( $shape, 0.5 ); $sprite->add($shape); $sprite->nextFrame(); $d = $movie->add($sprite); $movie->setDimension( 100, 100 ); $movie->save( "test.swf" ); ?> ---- Server IP: 208.69.120.35 Probable Submitter: 138.208.5.3 ---- Manual Page -- http://www.php.net/manual/en/function.swfshape.drawarc.php Edit -- https://master.php.net/note/edit/87683 Del: integrated -- https://master.php.net/note/delete/87683/integrated Del: useless -- https://master.php.net/note/delete/87683/useless Del: bad code -- https://master.php.net/note/delete/87683/bad+code Del: spam -- https://master.php.net/note/delete/87683/spam Del: non-english -- https://master.php.net/note/delete/87683/non-english Del: in docs -- https://master.php.net/note/delete/87683/in+docs Del: other reasons-- https://master.php.net/note/delete/87683 Reject -- https://master.php.net/note/reject/87683 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148228) next »