forked from: forked from: forked from: forked from: How can I draw a circle with dashed line/stroke?

by aobyrne forked from forked from: forked from: forked from: How can I draw a circle with dashed line/stroke? (diff: 44)
Hi! Why don't you use the DrawArc class below?

How can I draw a circle with dashed line/stroke?
Using a for cycle and using the equation of the circle wouldn't it be too slow?
♥0 | Line 121 | Modified 2011-11-19 21:05:09 | MIT License
play

ActionScript3 source code

/**
 * Copyright aobyrne ( http://wonderfl.net/user/aobyrne )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kQxp
 */

// forked from aobyrne's forked from: forked from: forked from: How can I draw a circle with dashed line/stroke?
// forked from SoWhat1983's forked from: How can I draw a circle with dashed line/stroke?
// Hi! Why don't you use the DrawArc class below?

// forked from maesy's How can I draw a circle with dashed line/stroke?
//How can I draw a circle with dashed line/stroke?
//Using a for cycle and using the equation of the circle wouldn't it be too slow?
package {
    import flash.geom.Point;
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.LineScaleMode;
    import flash.display.CapsStyle;
    import flash.display.JointStyle;

    public class Root extends Sprite {
        public function Root() {
           circle();
        }
        
        private function circle():void {
            var point:Point=new Point(120,120);
            addChild( new DashedCircle(50, point,7));
            graphics.lineStyle(0);
            graphics.drawCircle(point.x, point.y, 50);
                
            var dr:Number = 5;
            var ri:Number = 20;
            var rr:Number = ri;
            var posr:Number = 0;
            for (var jk:int = 0; jk < 6 ; jk++)
            {
                posr += 2*(ri+(jk+0.5)*dr);        
                rr += dr;
                point = new Point(posr, 400);
                addChild( new DashedCircle(rr, point,7));
                graphics.lineStyle(0);
                graphics.drawCircle(point.x, point.y, ri);
            }
        }
    }
}


import flash.display.*;
import flash.geom.Point;
class drawArc extends Sprite
{ 
    public function drawArc(baseX:Number, baseY:Number, radius:Number, startAngle:Number, arcAngle:Number, fillColor:uint,lineThickness:Number,lineColor:uint):void {
        
        var yRadius:Number = radius;
        
        var segAngle:Number, theta:Number, angle:Number, angleMid:Number, segs:Number, ax:Number, ay:Number, bx:Number, by:Number, cx:Number, cy:Number, return_arr:Array;
        
        segs = Math.ceil(Math.abs(arcAngle)/45);
        segAngle = arcAngle/segs;
        
        theta = -(segAngle/180)*Math.PI;
        angle = -(startAngle/180)*Math.PI;
        
        // angle += theta;
        
        ax = baseX;
        ay = baseY;
        angleMid = angle-(theta/2);
        bx = ax+Math.cos(angle)*radius;
        by = ay+Math.sin(angle)*yRadius;
        
        with(graphics){
            beginFill(fillColor);
            lineStyle(lineThickness,lineColor);
//            moveTo(0,0); 
//            lineTo(bx, by);
            moveTo(bx, by);
        }
        
        
        if (segs>0) {
            for (var i:int = 0; i<segs; i++) { 
                angle += theta;
                angleMid = angle-(theta/2);
                bx = ax+Math.cos(angle)*radius;
                by = ay+Math.sin(angle)*yRadius;
                cx = ax+Math.cos(angleMid)*(radius/Math.cos(theta/2));
                cy = ay+Math.sin(angleMid)*(yRadius/Math.cos(theta/2));
                graphics.curveTo(cx, cy, bx, by);
            }
        }
//        graphics.lineTo(0,0); 
    };
}

class DashedCircle extends Sprite {
    private var _radius:Number;
    private var _c:Point;
    private var dashSize:Number;
    private var color:uint;
    private var thickness:Number;
    public function DashedCircle(radius:Number ,c:Point,dashSize:Number=-1,color:uint=0,thickness:Number=3) {
       this.thickness = thickness;
       this.color = color;
       this.dashSize = dashSize;
       this.radius = radius;
       this.c = c;
       circle();
    }
    
    private function circle():void {
        var shape:Shape = new Shape();
        shape.graphics.beginFill(0xFFcccc, 0.1);
        shape.graphics.drawCircle(c.x, c.y, radius);
        shape.graphics.endFill();
        var perimeter:Number = Math.PI * 2 * radius;
        var d:Number = perimeter / dashSize;

        
        var dashedCircle:Sprite = new Sprite();
        var arcAngle:Number            =  4;
        if (dashSize!=-1) 
        {
            arcAngle = 360 / d;
        }
        var dashedLineColor:uint       =  color;
        var dashedLineThickness:Number =  thickness;
        var dfsdf:Number = 360 / (arcAngle * 2);
        dashedCircle.addChild(shape);
        for (var i:Number = 0; i < dfsdf; i++)
        {
            var a:drawArc = new drawArc(c.x, c.y, radius, (i * (arcAngle * 2)), arcAngle, 0x000000, dashedLineThickness, dashedLineColor);
            dashedCircle.addChild(a);
        }
        addChild(dashedCircle);
    }
    
    public function get radius():Number 
    {
        return _radius;
    }
    
    public function set radius(value:Number):void 
    {
        _radius = value;
    }
    
    public function get c():Point 
    {
        return _c;
    }
    
    public function set c(value:Point):void 
    {
        _c = value;
    }
}