#!/usr/bin/perl -w # # Example 9-6. Drawing a circle with cubic Bezier curves use strict; use SWF::Shape; SWF::setScale(1.0); my $PI = 3.1415926535; # pi my $r = 50; # the radius my $start = 0; # start angle my $end = 0; # end angle my $segments = 6; # number of segments # Create a new Shape object my $s = new SWF::Shape(); $s->setLineStyle(1, 255, 0, 0); $s->movePenTo($r,0); for (my $end=360/$segments; $end<=360; $end+=360/$segments) { # Calculate the midpoint angle. The control point # lies on this line my $midpoint = $start+($end-$start)/2; # draw_arc() draws a circular arc between 2 points draw_arc($s, $r*cos(radians($start)),$r*sin(radians($start)), $r*cos(radians($end)), $r*sin(radians($end)), radians($midpoint)); $start=$end; } # Create a Movie to hold the Shape my $m = new SWF::Movie(); $m->setDimension(300, 300); my $item = $m->add($s); $item->moveTo(100,100); $m->nextFrame(); $m->save('circle.swf'); exit; sub draw_arc { # Take a shape, a start coordinate, end coordinate and # pre-computed mid-point angle as arguments my ($s, $x1, $y1, $x2, $y2, $angle) = @_; my $cx = 2*($r*cos($angle)-.25*$x1-.25*$x2); my $cy = 2*($r*sin($angle)-.25*$y1-.25*$y2); # Draw the curve on the Shape $s->drawCurveTo($cx, $cy, $x2, $y2); } sub radians { return ($_[0]/180)*$PI; # Convert to radians }