forked from: code on 2009-1-15
package {
import flash.display.*;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.getTimer;
public class DrawStar2 extends Sprite {
private var sprite:Sprite;
public function DrawStar2() {
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(e:Event):void
{
sprite = new Sprite();
sprite.x = stage.stageWidth * 0.5;
sprite.y = stage.stageHeight * 0.5;
addChild(sprite);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(e:Event):void
{
sprite.graphics.clear();
var r1:Number = 400 * Math.sin(getTimer() / 1800) + 200;
var r2:Number = 400 * Math.sin(Math.sin(getTimer() / 2700)*2*Math.PI) + 200;
for (var i:int = 0; i < 20; i++) {
drawStar(sprite.graphics, r1 * i * 0.2, r2 * i * 0.2, 0, 0, 6);
}
}
/**
*
* @param g
* @param radius1
* @param radius2
* @param cx
* @param cy
* @param num
*/
public function drawStar(g:Graphics, radius1:Number, radius2:Number, cx:Number = 0, cy:Number = 0, num:int = 5):void {
var data:Vector.<Number> = new Vector.<Number>();
var commands:Vector.<int> = new Vector.<int>();
var th:Number = 2 * Math.PI / num,
p1:Point = new Point(cx + 0, cy -radius1),
p2:Point = new Point(cx + 0, cy -radius2),
tmp:Number;
rotatePoint(p2, th * 0.5);
data.push(cx + p1.x, cy + p1.y, cx + p2.x, cy + p2.y);
commands.push(GraphicsPathCommand.MOVE_TO,GraphicsPathCommand.LINE_TO);
var i:int;
for ( i = 0; i < num -1; i++) {
rotatePoint(p1, th);
rotatePoint(p2, th);
data.push(cx + p1.x, cy + p1.y, cx + p2.x, cy + p2.y);
commands.push(GraphicsPathCommand.LINE_TO,GraphicsPathCommand.LINE_TO);
}
data.push(cx + 0, cy -radius1);
commands.push(GraphicsPathCommand.LINE_TO);
var fill:GraphicsSolidFill = new GraphicsSolidFill(0xffff00);
var stroke:GraphicsStroke = new GraphicsStroke(2, false, "normal", "none", "miter", 3, new GraphicsSolidFill(0x000000));
var path:GraphicsPath = new GraphicsPath(commands, data);
g.drawGraphicsData(Vector.<IGraphicsData>([stroke, path]));
}
private function rotatePoint(p:Point, radian:Number):void {
var tmp:Number,
sin:Number = Math.sin(radian),
cos:Number = Math.cos(radian);
tmp = cos * p.x - sin * p.y;
p.y = sin * p.x + cos * p.y;
p.x = tmp;
}
}
}