code on 2009-1-15
package {
import flash.display.*;
public class DrawStar extends Sprite {
public function DrawStar() {
super();
var sp:Sprite= addChild( new Sprite() ) as Sprite;
sp.x = 200;
sp.y = 200;
var drawer:Drawer = new Star( 5, 2, 0xFF6600 );
drawer.draw( sp.graphics );
}
}
}
import flash.display.*;
import flash.utils.*;
import flash.geom.*;
class Star implements Drawer {
private static const RADIAN:Number = 2*Math.PI;
private var _polygon:uint = 5;
private var _lineWeight:uint = 0;
private var _lineColor:uint = 0;
private var _pointList:Array = [];
public function Star( polygon:uint = 5, lineWeight:uint=0, lineColor:uint = 0xFF0000 ) {
_polygon = polygon;
_lineWeight = lineWeight;
_lineColor = lineColor;
var angle:Number= RADIAN/(_polygon*2);// 1辺あたりの角度
var offsetAngle:Number = -RADIAN*0.25;//始点を上(90度)に持ってくる
for( var i:int=0,l:uint=_polygon*2; i<l; ++i ) {
var px:Number = Math.cos( angle*i +offsetAngle );
var py:Number = Math.sin( angle*i +offsetAngle );
_pointList.push( new Point( px, py ) );
}
}
public function draw( canvas:Graphics ):void {
//半径2種類
var longR:uint = 100;
var shortR:uint = 39;
// 図形描画
var fp:Point= _pointList[0] as Point;
canvas.lineStyle( _lineWeight, _lineColor);
canvas.moveTo( fp.x*longR, fp.y*longR);
var i:uint = 0, p:Point, r:uint, isOdd:Boolean;
var intervalId :uint = setInterval( function():void {
if( ( p = _pointList.shift() ) ) {
isOdd = ( i++%2==0 );
r = ( isOdd )?longR: shortR;
canvas.lineTo( p.x*r, p.y*r );
}
else {
// 図形を閉じる
canvas.lineTo( fp.x*longR, fp.y*longR) ;
clearInterval( intervalId );
}
}, 250 );
}
}
interface Drawer {
function draw( canvas:Graphics ):void;
}